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

Spring animation sometimes has negative start offset #4468

Closed
rikschennink opened this issue Feb 26, 2020 · 1 comment · Fixed by #14541
Closed

Spring animation sometimes has negative start offset #4468

rikschennink opened this issue Feb 26, 2020 · 1 comment · Fixed by #14541

Comments

@rikschennink
Copy link

rikschennink commented Feb 26, 2020

#4531

Describe the bug
In some situations animating a spring causes the spring value to animate in the wrong direction for the first tick.

For example, animating 100 to 0, logging the values shows:
100.1 -> 98 -> 87 -> etc.

That 100.1 is problematic as it causes a slight jump.

To Reproduce
I've not been able to reproduce this in the REPL but I've debugged the spring.ts file.

It seems the value returned by the now() function that is set to the last_time variable at line 99 can be different from the now value in the loop, which causes the first tick of the spring to be in the past resulting in a negative start point.

I suspect setting now inside the animation loop will fix this.

if (value == null || opts.hard || (spring.stiffness >= 1 && spring.damping >= 1)) {
    cancel_task = true;
    last_time = null; // set to null instead of now() on line 88

    // ... rest of code

if (!task) {
    last_time = null; // set to null instead of now() on line 99
    cancel_task = false;

    task = loop(now => {

        // set now here, if it's not available, it's set to first frame of loop
        if (last_time === null) last_time = now;

        if (cancel_task) {
            cancel_task = false;

            // ... rest of code

Expected behavior
The value should not jump up before being lowered.

Information about your Svelte project:

  • Tested on Chrome latest and Firefox latest
  • MacOS Catalina
  • Svelte 3.6.1
  • Rollup

Severity
This is causing animations to slightly jump when they start, I'll probably create a custom spring.ts function to work around this.

@johanbissemattsson
Copy link

johanbissemattsson commented Apr 12, 2020

I ran into the same issue while making a draggable bottom sheet component where the spring used for snapping had a tendency to jump at start. Your fix inside the spring function resolved the issue so hope to see it merged. 👍

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