Jump to content
Search Community

ScrollSmoother shortfall - velocity jumps on hard stops

SteveS test
Moderator Tag

Recommended Posts

Title says most of it. Overall, ScrollSmoother's getVelocity feature seems jittery compared to libraries I have previously used, almost as if it has a lower polling rate. It also really doesn't like hard stops as per the included video. It's not hard to imagine someone scrolling and stopping this fast on mobile. I couldn't find any other posts about it, and there doesn't seem much to do in the way of configuration or settings.
 

 

Link to comment
Share on other sites

I can't share a demo right now. What it looks like is that on desktop the smooth scroll terminates at the end of the page smoothly, but on mobile the smoothing is disabled and therefore the page slams into the end. Since the scrolling ends so abruptly, the velocity goes from whatever it's at to 0 instantly. Same for switching directions really quickly. There is one or two ticks where the velocity goes from high, to basically zero.

I guess the question is whether this is something that ScrollSmoother should handle, or if it's something I should be accounting for in my code.

Link to comment
Share on other sites

Ah, right, there's no smoothing happening on mobile so it's not a shock that is more abrupt. If you're trying to smooth that out, you could definitely use a gsap.quickTo() to introduce some gradual interpolation. In fact, I whipped together a helper function that'll return a FUNCTION that you can call anytime to get the smoothed-out velocity: 

 

function velocitySmoother(smoother, duration) {
  duration = duration || 0.5;
  let tracker = {v: 0},
      vLast = 0,
      tLast = Date.now(),
      velocityTo = gsap.quickTo(tracker, "v", { duration: duration });
  duration *= 1000;
  return () => {
    let v = smoother.getVelocity(),
        t = Date.now(),
        ratio = 1 - Math.min(1, (t - tLast) / duration);
    if (v !== vLast) {
      velocityTo(v);
      vLast = v;
      tLast = t;
    }
    return v - (v - tracker.v) * ratio;
  }
}

Usage:

let smoother = ScrollSmoother.create({
  ...
});

let getVelocity = velocitySmoother(smoother);

// log out the velocity every 100ms...
setInterval(() => console.log("velocity", getVelocity()), 100);

Does that help? 

Link to comment
Share on other sites

It's tough for me to advise without a minimal demo - there are two approaches:

  1. Use a gsap.quickTo() on the thing you're setting based on the velocity so that it smooths out that transition. In other words, you're not smoothing the velocity - you're smoothing the thing that's influenced by that velocity. 
  2. Use the "duration" parameter I gave you in that helper function if you'd like it to take longer to transition the velocity. Or you can set an ease in the gsap.quickTo() that's inside that helper function. The default is "power1.out", but you can set it to whatever you want. 

Enjoy!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...