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

Mass and momentum #427

Closed
antonioberetini opened this issue Dec 24, 2021 · 12 comments
Closed

Mass and momentum #427

antonioberetini opened this issue Dec 24, 2021 · 12 comments

Comments

@antonioberetini
Copy link

Is it possible to control mass and momentum? I feel that with a bit more mass and momentum, the scroll would feel more natural. For instance comparing it to iOS native scroll, on shorter swipes, scroll moves longer and it eases out before it stops.

@idiotWu
Copy link
Owner

idiotWu commented Dec 24, 2021

You can use the damping option to control the easing.

@antonioberetini
Copy link
Author

Yeah I tired several options- if I go super low 0.07 even down to 0.04 (which feels close to ios), I it feels very smooth but it's super jumpy. If I go higher with damping it's super stiff. Mass would do the magic here.

@idiotWu
Copy link
Owner

idiotWu commented Dec 24, 2021

(Not sure momentum is a proper word here, although people are calling it "momentum-based scrolling".)

As mass directly proportional to momentum, you can use the following plugin to multiply the momentum when releasing finger:

import Scrollbar, { ScrollbarPlugin } from 'smooth-scrollbar';
import OverscrollPlugin from 'smooth-scrollbar/plugins/overscroll'; // if you are using this

class MassPlugin extends ScrollbarPlugin {
  static pluginName = 'mass';

  static defaultOptions = {
    m: 1.0,
  };

  override transformDelta(delta: { x: number, y: number }, fromEvent: Event): { x: number, y: number } {
    if (fromEvent.type === 'touchend') {
      return {
        x: delta.x * this.options.m,
        y: delta.y * this.options.m,
      };
    }

    return delta;
  }
}

Scrollbar.use(MassPlugin, OverscrollPlugin);

Scrollbar.init(elem, {
  plugin: {
    mass: { m: 1.5 },
  },
});

@idiotWu
Copy link
Owner

idiotWu commented Dec 24, 2021

I just tweaked some calculations to make the touch scrolling more natural, especially with damping: 0.05 on mobile devices. Can you update to v8.7.1 to give it a try?

@antonioberetini
Copy link
Author

antonioberetini commented Dec 24, 2021 via email

@idiotWu
Copy link
Owner

idiotWu commented Dec 24, 2021

Yeah, but I didn't add the MassPlugin into it.

@antonioberetini
Copy link
Author

Dude you are the God! The new version on your demo site behaves prettymuch like ios now with the following settings

Screenshot_20211224-134630.png

@idiotWu idiotWu closed this as completed Dec 25, 2021
@antonioberetini
Copy link
Author

I just noticed that scroll is jittering after this update while slowing down

@idiotWu
Copy link
Owner

idiotWu commented Dec 25, 2021

I just noticed that scroll is jittering after this update while slowing down

Have you unchecked the renderByPixels option?

@antonioberetini
Copy link
Author

antonioberetini commented Dec 25, 2021 via email

@idiotWu
Copy link
Owner

idiotWu commented Dec 25, 2021

It behaves smoothly in my environments (MacBook Pro 13, 2019; Pixel 4 w/ Android 12). Maybe damping: 0.02 brings too many frames that slow down the rendering?

@antonioberetini
Copy link
Author

antonioberetini commented Dec 25, 2021 via email

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

No branches or pull requests

2 participants