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

animation will pauses when the browser is minimized #690

Open
Scxppp opened this issue Sep 9, 2024 · 1 comment
Open

animation will pauses when the browser is minimized #690

Scxppp opened this issue Sep 9, 2024 · 1 comment

Comments

@Scxppp
Copy link

Scxppp commented Sep 9, 2024

Hello, I am a beginner. When using tween in Three.js, I found that when an animation starts executing and I minimize the browser(edge), the animation pauses. Is there any way to avoid this situation and ensure that the animation can continue to play and complete normally even when the browser is minimized?

@mk965
Copy link
Collaborator

mk965 commented Oct 9, 2024

In most browsers, when the window is minimized or not in focus, the rendering may pause to save resources. To work around this, you can use the requestAnimationFrame function and implement a way to track elapsed time manually.

One approach is to store the start time of the animation and update the animation based on the elapsed time instead of relying solely on frame updates. This way, even if the browser is minimized, the animation can continue based on the recorded time.

Here's a simplified example:

let startTime;
let duration = 2000; // Animation duration in milliseconds

function animate(time) {
    if (!startTime) startTime = time;
    let elapsed = time - startTime;

    // Calculate progress
    let progress = Math.min(elapsed / duration, 1);

    // Update your tween or animation based on progress
    tween.update(progress);

    if (progress < 1) {
        requestAnimationFrame(animate);
    }
}

// Start the animation
requestAnimationFrame(animate);

This way, the animation logic is based on the time elapsed, ensuring it continues smoothly even when the browser loses focus.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants