Your problem is that the onload
callback is asynchronous, so when the callback function is called, the loop may have ended, and the i
variable isn't defined anymore. Anyway, the this
keyword point to the image inside the callback :
var frames = [];
for (var i=0; i<assets.length; i++) {
frames[i] = new Image();
frames[i].onload = function() {
ctx.drawImage(this, 10, 10);
};
frames[i].src = assets[i];
}
The next problem you'll may have is that you won't control in which order the images are drawn on the canvas. See this JsFiddle.