Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

play() after setFrame() resets the animation #174

Closed
Rapsssito opened this issue Apr 12, 2024 · 3 comments
Closed

play() after setFrame() resets the animation #174

Rapsssito opened this issue Apr 12, 2024 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@Rapsssito
Copy link

Rapsssito commented Apr 12, 2024

Overview

Using @lottiefiles/dotlottie-web, calling play() after setFrame() resets the currentFrame property and restarts the animation from the start. This does not match the documentation for play(): Begins playback from the current animation position.

import { DotLottie } from "https://cdn.jsdelivr.net/npm/@lottiefiles/dotlottie-web/+esm";
(() => {
// Get lottie player and set the animation to play from half way through when finished a loop
const canvas = document.getElementById('hero-lottie');
const player = new DotLottie({
autoplay: true,
loop: false,
canvas: canvas,
speed: 1,
src: canvas.getAttribute('data-src'),
});
// Get loopFrom attribute
const loopFrom = parseInt(canvas.getAttribute('loopFrom'));
player.addEventListener('complete', () => {
    player.setFrame(loopFrom);
    player.play(); // Here the animation resets to the frame 0
});
})();

A workaround is to call player.setFrame() after play().

@Rapsssito Rapsssito changed the title play() resets after setFrame() resets the animation play() after setFrame() resets the animation Apr 12, 2024
@theashraf
Copy link
Member

Thank you, @Rapsssito, for bringing up this issue, will address it in future releases.

But If you're looking to loop through a specific part of the animation, say from loopFrom to the end of the animation, you might consider using the setSegment method. Here's how you can implement it:

import { DotLottie } from "https://cdn.jsdelivr.net/npm/@lottiefiles/dotlottie-web/+esm";

(() => {
    const canvas = document.getElementById('hero-lottie');
    const player = new DotLottie({
        autoplay: false, // ensure autoplay is false, so the player doesn't start rendering on load 
        loop: true, // ensure loop is set to true
        canvas: canvas,
        speed: 1,
        src: canvas.getAttribute('data-src'),
    });

    const loopFrom = parseInt(canvas.getAttribute('loopFrom'));

    player.addEventListener("load", () => {
        // Set the animation segment to play from 'loopFrom' to the last frame
        player.setSegment(loopFrom, player.totalFrames);
        // Start playing the animation
        player.play();
    });
})();

@Rapsssito
Copy link
Author

@theashraf, thanks a lot for the suggestion! I did not know about the setSegment() method.

@theashraf
Copy link
Member

@Rapsssito This issue has been fixed in the latest dotlottie-web@0.21.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Development

No branches or pull requests

2 participants