I'm trying to build on top of this great script here which was answered by Phrogz.
The problem is that the animation won't play until all the images have cached in the browser. Therefore, on first load it won't play. Only on a reload.
The point of me trying to do this is I'm trying to provide an alternate solution to getting an animation to autoplay since that feature doesn't work in iOS browsers.
Here is the code I'm using:
function draw() {
var imgNumber = 1;
var lastImgNumber = 121;
var ctx = canvas.getContext('2d');
var img = new Image;
img.onload = function(){
ctx.clearRect( 0, 0, ctx.canvas.width, ctx.canvas.height );
ctx.drawImage( img, 0, 0 );
};
var timer = setInterval( function(){
if (imgNumber>lastImgNumber){
clearInterval( timer );
}
else{
img.src = "jpg_80/t5_"+( imgNumber++ )+".jpg";
}
}, 1000/24 ); //Draw at 24 frames per second
}
I've tried adding this before the closing body tag:
window.onload = draw;
However, I believe that the code would have to be changed within the draw() function. Somewhere in the setInterval function?