I have a number of image frames that that I need to show one by one to create an animation. I achieved this by setting the background-image
property on each frame in my loop. I did this because of the other properties css provides that help me achieve the effect I wanted. Here is the code,
var el = document.getElementById("animation");
loop=()=>{
var now=performance.now();
var TimeCompleted=now - StartTime;
if(TimeCompleted>=this.Interval) {
StartTime=now-(TimeCompleted%this.Interval);
el.style.backgroundImage="url("+ images[frame].src + ")";
frame+=delta;
if(frame<=-1 || frame>=frames)
{
el.dispatchEvent(new Event("animationend"));
return;
}
}
requestAnimationFrame(loop);
}
and the css
#animation{
height:100%;
width:100%;
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
I never saw anyone make animations like this before so I was wondering if this is the best way of approaching this or if this method will cause any performance issues etc.