happening on safari mainly. The idea is this: animate 4 images one after the other but seconds one image can appear differs.
It works well in firefox but in safari it is very flaky and blinks a lot.
After images are preloaded, the animation should start. First, settimeout is called 0 so that the first image can be loaded asap. then the the ms parameter of settimeout takes in a millisecond value based on the index of the current image.
var timer = null;
var index = -1;
var frame = 0;
var loop = true;
var counter = 0;
var initialPause = 0;
var imagesDisplayFor = [1000, 2000];
async function preLoadImages() {
var frames = imagesDisplayFor.map(async(v, i) => {
let img = new Image()
img.onload = function() { return Promise.resolve() }
img.src = '../src/assets/' + i + '.png'
return img
})
await Promise.all(frames)
reset()
}
function pauseAnimationFor(fn) {
const wait = index === -1 ? initialPause : imagesDisplayFor[index];
clearTimeout(timer)
timer = setTimeout(fn, wait)
}
function reset(){
index = -1;
animate();
}
function animate() {
pauseAnimationFor(() => {
if (this.index < 1) {
index++
var src = '../src/assets/' + index + '.png';
document.getElementById("imagePlaceHolder").src = src;
animate()
}else{
reset();
}
})
}
preLoadImages().then(done=>{
animate();
})
<html lang="en">
<head>
</head>
<body>
<img id="imagePlaceHolder"/>
</body>
</html>