Way to consistently get gifs/webps to play at the right time?

Question:
is there a way to consistently get gifs/webps to play at the right time when being shown?

Product:
I am using the standard Wix editor and Velo

What are you trying to achieve:
I have been trying to make a reactive animation that plays whenever the user scrolls on the page, and to that end I have 2 webps that can be played in sequence- one for idle time, and one for the actual animation. However, since wix loads gifs and webps in advance, when I try to show one and hide the other by scrolling, the webp appears playing at a random time, as it was loaded in mid-loop.

What have you already tried:
I tried to correct this by reassigning the source of the image every time I load it, but that caused a slight delay where the webp wouldn’t be loaded yet even as it was shown. Then I tried to implement a loop where the webp would be constantly reloaded until it is shown, and that seems to work sometimes, but inconsistently. The image would appear at the right time, and load in properly, but the webp would sometimes be playing mid loop even though it should have been reset.

Additional information:
Here is a relevant snippet of code from the latest attempt:

layer1 = 0;
$w.onReady(function () {
    setInterval(()=> {animate1();}, 10)
    }
);

//skipping irrelevant stuff....

   if (recent[0] != recent[1] && layer1 == 0) {
            console.log(recent[0]);
            layer1 = 1;
            setTimeout(() => {layer1 = 0}, 2700); //animation is 2700 ms long
        }
    }

function animate1(){
 if (layer1 == 1) {
            $w('#GrassLayer1Anim').show();
            $w('#GrassLayer1Idle').hide();
    
        } 
    else {
            $w('#GrassLayer1Idle').show();
            $w('#GrassLayer1Anim').hide(); 
            $w('#GrassLayer1Anim').src = '(image png url (could technically be anything but I thought a static png of the image would be good incase the animation didn't play))';
            $w('#GrassLayer1Anim').src = '(image webp url)';
        }
}

Hey DK1474,

What you’re describing makes sense. However the way you’re going about it may not be the best. Because of how animated GIFs in general work, as you correctly said if you wanted them to load and start the animation at the right point, then setting the .src and having them load immediately would be ideal. However, there will always be that slight delay as the source is set.

With your approach, if you really wanted to hack it, you could try loading a hidden image with that src on the page, which may cause the browser to cache that image. Then you can try setting the .src again, which should be faster. BUT, still a delay

A much better approach would be to have something that you can start and stop any time (resumable) and smaller size than a GIF/WEBP to load. Something like a Lottie Animation could be better for your case. Also How to add them to your site

Thanks for the reply! Sadly, I don’t know if a lottie animation will work for my purposes, as my animation is personalized and I don’t see anything similar to it with existing Lottie Animations.
I’ve requested 2 NPMs that might help with pausing gifs, I’ll see if they get authorized.

Got it ! Sorry for the late reply. Did you ever solve this?