Refresh . Based on the new information in the question, your problem (fixed) is that you want to either
- wait for all images to load first, and then start animating with them or
- Start the animation and use only the image, if available.
Both are described below.
1. Download many images and continue only after their completion.
Using this technique, we immediately download all the images, and when loading the latter, we launch our own callback.
2. Track all downloaded images
Using this technique, we immediately begin the animation, but only draw images after they are downloaded. Our circle is dynamically resizing based on how many images are uploaded so far.
var imagePaths = [...];
And if you want the images to rotate in a circle, and not just to move in a circle:
ctx.save(); ctx.translate(cx,cy);
The following is the original answer.
In general, you should wait for each image download to complete:
function animate(){ var img1 = new Image; img1.onload = function(){ context.drawImage(img1,x1,y1); }; img1.src = "/path"; var img2 = new Image; img2.onload = function(){ context.drawImage(img2,x2,y2); }; img2.src = "/path"; }
You can make this code drier with an object:
var imgLocs = { "/path1" : { x:17, y:42 }, "/path2" : { x:99, y:131 }, // as many as you want }; function animate(){ for (var path in imgLocs){ (function(imgPath){ var xy = imgLocs[imgPath]; var img = new Image; img.onload = function(){ context.drawImage( img, xy.x, xy.y ); } img.src = imgPath; })(path); } }