react-spring icon indicating copy to clipboard operation
react-spring copied to clipboard

Inertia/momentum incorrect behavior, and unexpected value jumping

Open lourd opened this issue 6 years ago • 16 comments

🐛 Bug Report

This is a bug for tracking the overall not-quite-right behavior of 1:1 animation with momentum at the end. I started thinking about this after my issue on react-use-gesture last weekend. Here are some earlier reports and discussions from react-spring and react-use-gesture:

https://github.com/react-spring/react-spring/issues/887 https://github.com/react-spring/react-spring/issues/1010 https://github.com/react-spring/react-use-gesture/issues/132

This is a problem that comes up most commonly with gestures, but I believe the solution lies in the animation primitives. Mapping gestures 1:1 with the animation requires using the immediate parameter, but doing so ends up breaking the expected velocity/momentum for the end of the animation/gesture. You can get kind of close to the desired behavior by using a stiff spring config without the immediate parameter, but this isn't ideal. HCI research has shown that the desired behavior for these types of gestures is a 1:1 correspondence.

The previous suggested solutions for building this behavior have been custom hooks outside the library, but hopefully we can all agree that this is something that should be covered by the library. Previous solutions added a concept of decay, but I think the library already has all of the concepts/APIs we need, it's just a matter of fixing how velocity is calculated when using immediate.

Expected behavior with reproduction

This is a demo of some horizontal scroll areas nested in a vertical scrolling area. Both of them have "touch" scrolling enabled; the scrollLeft and scrollTop are being animated, in order to co-exist with native browser scrolling.

https://codesandbox.io/s/react-spring-inertial-scroll-challenge-qh4wr

The desired behavior is exemplified by the native scrolling behavior on macOS with a trackpad -- 1:1 mapping of scroll gesture to position, with momentum proportional to the spring config when ending the gesture with velocity. The animated value should not overshoot its target.

The touch scrolling demo implementation does have some momentum, but only a tiny bit; not at all like you'd expect. Here's a gif showing how much momentum I could get when trying my hardest to "sling" it

missing inertia

Environment

  • react-spring v9.0.0-rc.3
  • react v16.8.12

lourd avatar May 24 '20 19:05 lourd

I've the same issue (just to reporting that I saw the same bug)

brunoalano avatar Jun 24 '20 21:06 brunoalano

Hi @lourd

So what you're saying is, you should be able to click and drag a row and "sling it" so it animates with inertia to a stop?

And currently, as I can see it's pretty static af click and sling as opposed to doing a scroll on a mac trackpad – https://codesandbox.io/s/react-spring-touch-scrolling-example-forked-vc5q0?file=/src/App.js (updated to v9 deps to be sure).

Is that a fair summary?

joshuaellis avatar Apr 19 '21 21:04 joshuaellis

Hey @joshuaellis, thanks for digging into this!

Yes, that's a good summary. Looking for end behavior that's super similar to how Greensock's inertia plugins feel.

Heads up that the sandbox updated to v9 has a bug:

Screen Recording 2021-04-19 at 7 47 05 PM 2021-04-19 19_48_16

lourd avatar Apr 19 '21 23:04 lourd

RE. the bug, that looks like it might have something to do with react-use-gesture?

So does it actually make sense to have inertia when immedate = true? As immediate is defined in the docs as:

Prevents animation if true. Also valid as a function for individual keys: key => immediate

So having inertia would break that concept no?

joshuaellis avatar Apr 20 '21 07:04 joshuaellis

Just like momentum scrolling on mobile phones, you want to be able to have a 1:1/"immediate" animation as your dragging something, and then when you let go the item should keep animating/moving in accordance with the velocity. Setting immediate should not set the velocity of the spring to 0.

lourd avatar Apr 20 '21 16:04 lourd

Oh I see, thanks @lourd this makes a bit more sense to the issue we have!

Upon great inspection i don't think we need to use immediate we can use set & start.

onDrag({ down, movement: [x], first, last, vxvy: [vx] }) {
  console.log("onDrag with movement", { x, first, last, vx, down });
  if (first) isDragging.current = true;
  if (last) setTimeout(() => (isDragging.current = false), 0);
  if (down) {
    api.set({ x: -x });
  } else {
    api.start({ x: -x });
  }
}

where set sets the spring to the values and start should animate. It doesn't solve the issue, but my understanding of spring means the start value should be given the end goal.... 🤔

This example demonstrates this method and you can see the velocity is still 0 – https://codesandbox.io/s/react-spring-touch-scrolling-example-forked-vc5q0?file=/src/App.js

So maybe there's a problem with velocity updating at all....?

joshuaellis avatar Apr 20 '21 18:04 joshuaellis

This has been released on 9.2.0-beta.0 if you could try it that would be great ⭐

joshuaellis avatar Apr 27 '21 17:04 joshuaellis

Hey @joshuaellis, I just tried this out and am not seeing any change to the behavior. Here's an update of the original sandbox using 9.2.0-beta.1.

Here's the relevant code:

function HorizontalScrollView(props) {
  const ref = React.useRef();
  const [{ x }, spring] = useSpring(() => ({
    x: 0,
    config: { decay: 0.8, frequency: 5.5 }
  }));
  const bind = useGesture(
    {
      onDrag({ down, movement: [x], first, last, vxvy: [vx] }) {
        console.log("X onDrag with: ", { x, vx, first, last, down });
        spring.start({ x: -x, immediate: down });
      },
      onWheelStart() {
        // Stop any user-land scroll animation from confcliting with the browser
        spring.stop();
      }
    },
    {
      drag: {
        axis: "x",
        filterTaps: true,
        initial() {
          console.log("Initial x value", -ref.current.scrollLeft);
          return [-ref.current.scrollLeft, 0];
        }
      }
    }
  );

  return (
    <animated.div
      ref={ref}
      scrollLeft={x}
      className={props.className}
      {...bind()}
    >
      {props.children}
    </animated.div>
  );
}

Am I using the API incorrectly? I'm using start instead of set, with immediate set to the down value. I'm passing in a value for decay in the initial useSpring config. I also tried setting decay to true in the call to start and got the same behavior.

I am seeing correct-looking values for the x velocity being logged, but it doesn't seem like they're affecting the animation.

I also tried using set instead of start. That causes a TypeError with Cannot read property 'set' of undefined

lourd avatar Apr 28 '21 22:04 lourd

I've made some tweaks to the sandbox, you need to pass a velocity:

https://codesandbox.io/s/react-spring-920-beta-touch-scrolling-example-forked-2cdov?file=/src/App.js

I think this solves the issue? If so, please feel free to close this one 🙏🏼

joshuaellis avatar Apr 29 '21 20:04 joshuaellis

That works brilliantly, thanks @joshuaellis!!!

I played around with your tweaks a little more, and it looks like the vxvy Vector2 parameter to onDrag is already scaled with the direction, so that can be used instead of having to scale the velocity Vector2. I updated my 9.2.0 beta sandbox with that change. I also converted it to TypeScript and added a Leva range control for the decay config to play around with.

lourd avatar Apr 29 '21 21:04 lourd

Thats cool, if you were interested when it's done we could add it to react-spring.io as a guide or somewhere else!

joshuaellis avatar Apr 29 '21 21:04 joshuaellis

For sure! If I could get a pointer towards where to put it I'd be happy to. Do you think I should write it up as a guide or add it as a demo?

lourd avatar Apr 30 '21 01:04 lourd

Hmm, tried to reuse 99% exact same horizontal scroll view but every time velocity is supposed to kick in, scroll left jumps to 0 for a split second and back to where we left off, no momentum.

https://codesandbox.io/s/testing-123-58u9s?file=/src/App.js

Initial value and drag handler values are correct, here's what last config looks like after mouse release:

{
  immediate: false,
  x: 116.89642333984375,
  config: {
    velocity: 1.6103612199126909
  }
}

Edit: Didn't read that fix is in beta versions, not in latest solid. My bad.

gitcatrat avatar May 01 '21 12:05 gitcatrat

you have to define as a function ev.stopProgation();

Rohit544 avatar May 01 '21 13:05 Rohit544

If I'm not wrong, offset is also jumping, i.e it doesn't seem to take decay into account.

gitcatrat avatar May 31 '21 21:05 gitcatrat

is there any way to know where spring will rest, the final value, with decay and velocity?

ian-rafael avatar Jun 14 '21 19:06 ian-rafael