Hey, I have a little problem here.
Question: how can I create unique variables for a function, so when called several times, the variables will contain what they should (not change)
Keep in mind that I have to keep it asynchronous as the loop will be large and will not work. async will hit performance hard.
I have a function that draws elements on a canvas. Then I run this function in a for loop to draw some canvases depending on some data in the array.
so simplified version:
function renderCanvas(canvas, dID) { var mName, bName, sName, tName; this.layerCounter = mainData[dID]['layerCount']; console.debug(designID + " has " + layerCounter + " layers"); tctx2.clearRect(0, 0, tc2.width, tc2.height); var imgPath = sName; imgObj = new Image(); imgObj.src = "img/" + imgPath; imgObj.onload = function () { tctx2.drawImage(imgObj, 0, 0, w, h, 0, 0, dw, dh); layerCounter--; console.debug(designID + " has " + layerCounter + " layers"); tctx3.clearRect(0, 0, tc2.width, tc2.height); var imgPath = tName; imgObj = new Image(); imgObj.src = "img/" + imgPath; imgObj.onload = function () { tctx3.drawImage(talphaObj, 0, 0, w, h, 0, 0, dw, dh); layerCounter--; console.debug(designID + " has " + layerCounter + " layers"); }; } } for (var i = 0; i < xArr.length; i++) { var cDID = xArr[i]; renderCanvas(contexts[i], cDID); }
Any suggestions? I'm new to programming, so I probably miss something very light.
javascript variables local-variables
Tomasz Golinski Jan 07 '14 at 15:36 2014-01-07 15:36
source share