DEV Community

Michael Vestergaard
Michael Vestergaard

Posted on

Using lerp and damp in javascript

Almost every project I do as a creative developer utilizes linear interpolation - also called "lerp". It's a simple way of easing from position A to B. Without diving into the math, there's another approach called damp (or smoothdamp), this is almost similar but has a smoother curve (more like a Quad and less like an Expo).

The classic way of using lerp is like this:

//speed between 0-1
_tweenedValue += (_actualValue - _tweenedValue) * _speed;
Enter fullscreen mode Exit fullscreen mode

This works very well. You can even tween the speed if you want the movement to start slowly:

_this._speed = 0;
gsap.to(_this, 1, {_speed:.2, ease:"linear"});
_tweenedValue += (_actualValue - _tweenedValue) * _this._speed;
Enter fullscreen mode Exit fullscreen mode

I'm using the above in custom cursors, carousels, elements that move based on mouse position and even for a smooth pagescroller (similar to Lenis).

Below is an example of both lerp and damp (blue and red), but also a fun little bouncy/elastic approach (green):

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay