I have a looping 120-frame 30fps animation, all PNG files, and all around 600x600px. These facts are unfortunately non-negotiable, so I have to figure something out to make it work.
My script looks like this:
var path = "[[my image path]]";
var nbImages = 120;
var frames = [];
function nextFrame() {
$("[[my image id]]").attr('src', frames[0]);
frames.push(frames[0]);
frames.shift();
}
$(document).ready(function(){
for (var x = 0; x <= nbImages; x++) {
frames.push(path + (x+1) + ".png");
}
setInterval(function(){ nextFrame(); }, 1000/30);
});
My issue is, of course, loading time. On the first page view, not only do the images take a million years to load, but the animation starts displaying the few loaded images as soon as they're available.
I've read up on preloading images, but I keep seeing conflicting solutions, even just for jQuery scripts. As for existing StackOverflow answers, the ones I've tried actually made the loading slower--and not just on the first visit. All the other ones didn't apply to image sequences.
- What preloading method is the most appropriate, in this case?
- What is the correct syntax to only start my animation once all images have been loaded?
Everything else works correctly.