Jump to content
Search Community

ScrollTrigger and Lottie: Previous frames not hiding during scrubbing

thewestharbour test
Moderator Tag

Recommended Posts

Hello,

 

We are having an issue where, when scrubbing through a lottie file, the previous frames in the sequence still show behind the current one.

 

We are enqueuing the following scripts at the top of the file:

 

wp_enqueue_script('jquery', 'https://code.jquery.com/jquery-3.7.0.slim.min.js');
wp_enqueue_script('lottie-player', 'https://unpkg.com/@lottiefiles/lottie-player@latest/dist/lottie-player.js');
wp_enqueue_script('lottie-ajax', 'https://cdnjs.cloudflare.com/ajax/libs/bodymovin/5.7.6/lottie.min.js');
wp_enqueue_script('lottieinteractivity', 'https://unpkg.com/@lottiefiles/lottie-interactivity@latest/dist/lottie-interactivity.min.js');
wp_enqueue_script('gsap-js', 'https://cdn.jsdelivr.net/npm/gsap@3.12.5/dist/gsap.min.js');
wp_enqueue_script('gsap-scrolltrigger', 'https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.2/ScrollTrigger.min.js');

 

<?php if($lotties == '2' && $file😞 ?>
<lottie-player renderer="canvas" class="js-lottie-scrub js-lottie-scrub-first-half" src="<?php echo $file['url']; ?>"></lottie-player>
<?php elseif($file😞 ?>
<lottie-player renderer="canvas" class='js-lottie-scrub' src="<?php echo $file['url']; ?>"></lottie-player>
<?php endif; ?>
<?php if($lotties == '2' && $file2😞 ?>
<lottie-player renderer="canvas" class='js-lottie-scrub js-lottie-scrub-second-half' src="<?php echo $file2['url']; ?>"></lottie-player>
<?php endif; ?>

 

Here is the JS being included in the header. Note, the bug is not happening if the 'half' conditional is met - only for lotties without 2 parts to the sequence.

Any help here is very much appreciated.

 

$(window).on('load', function() {
      
    
        
        const lottiesSectionsAnim = () => {
            gsap.registerPlugin(ScrollTrigger);
        
            const lottieSectionsTargets = document.querySelectorAll('.scrolly_outer');
        
            lottieSectionsTargets.forEach((outer) => {
                const lotties = Array.from(outer.querySelectorAll('.js-lottie-scrub'));
                if (!lotties.length) return;
        
                const players = lotties.map((lottie) => {
                    const { player } = LottieInteractivity.create({
                        player: lottie,
                    });
        
                    let half = null;
        
                    if (lottie.classList.contains('js-lottie-scrub-first-half')) {
                        half = 'first';
                    } else if (lottie.classList.contains('js-lottie-scrub-second-half')) {
                        half = 'second';
                    }

                    console.log('half? ', half);
        
                    tooltipsFadeAnimation(outer);
        
                    // Get correct count of frames
                    if (!player?.animationData?.layers?.length) return {};
        
                    const rangeStartMinVal = Math.min(
                       ...player.animationData.layers.map((layerData) => layerData.ip),
                    );
                    const rangeEndMaxVal = Math.max(
                       ...player.animationData.layers.map((layerData) => layerData.op),
                    );
                    const framesCount =
                        lottie.dataset.framesCount || rangeEndMaxVal - rangeStartMinVal;
        
                    // Debugging line: Log frames count
                    console.log("Frames Count:", framesCount);
        
                    return {
                        framesCount: Math.round(framesCount * player.frameRate),
                        player,
                        half,
                    };
                });
        
                let firstHalfLotties = lotties.filter((lottie) =>
                    lottie.classList.contains('js-lottie-scrub-first-half'),
                );
                let secondHalfLotties = lotties.filter((lottie) =>
                    lottie.classList.contains('js-lottie-scrub-second-half'),
                );
        
                const toggleHalfVisibility = (triggerProgress, firstTime) => {
                    if (triggerProgress <= 0.5 && firstHalfLotties.length) {
                        gsap.set(firstHalfLotties, { display: 'block' });
                        secondHalfLotties.length &&
                            gsap.set(secondHalfLotties, { display: 'none' });
                    } else if (triggerProgress > 0.5 && secondHalfLotties.length) {
                        firstHalfLotties.length &&
                            gsap.set(firstHalfLotties, { display: 'none' });
                        gsap.set(secondHalfLotties, { display: 'block' });
                    }
                };
        
                let animProgress = 0;
                let triggerProgress = 0;
        
                const trigger = ScrollTrigger.create({
                    trigger: outer,
                    start: 'top top',
                    end: 'bottom bottom',
                    onRefresh: (self) => {
                        toggleHalfVisibility(self.progress.toFixed(3), true);
                    },
                    onUpdate: (self) => {
                        triggerProgress = self.progress.toFixed(3);
                        toggleHalfVisibility(triggerProgress);
                        players.map((item) => {
                            if (!item.framesCount) return;
        
                            const { framesCount, player, half } = item;
                            if (half === 'first') {
                                animProgress = framesCount * Math.min(triggerProgress, 0.5) * 2;
                            } else if (half === 'second') {
                                animProgress =
                                    framesCount * (Math.max(triggerProgress, 0.5) - 0.5) * 2;
                            } else {
                                animProgress = framesCount * triggerProgress;
                            }
        
                            // Debugging line: Log animation progress
                            console.log("Animation Progress:", animProgress);
        
                            player.goToAndStop(Math.floor(animProgress), false);
                        });
                    },
                });
            });
        };

Link to comment
Share on other sites

Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependencies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...