snap-lerp

1.1.0 • Public • Published

snap-lerp

Linearly interpolate two numbers, but snap to the closest value if the difference between them is small enough. Comes in handy if you're running an animation but don't want to keep making unnecessary updates to a value as it approaches JS's limits in numerical precision.

Usage

NPM

snapLerp(a, b, t, min, [forceSnapToB])

Linearly interpolate between a and b for a given range t:

+ (- a) * t

However, if Math.abs(a - b) <= min, snap to the closest value:

const snapLerp = require('snap-lerp')
 
15 === snapLerp(10, 20, 0.50, 1)
20 === snapLerp(10, 20, 0.50, 10)
10 === snapLerp(10, 20, 0.49, 10)

If required, you may also pass forceSnapToB as true to ensure that the value snapped will always be the second supplied:

const snapLerp = require('snap-lerp')
 
15 === snapLerp(10, 20, 0.50, 1, true)
20 === snapLerp(10, 20, 0.50, 10, true)
20 === snapLerp(10, 20, 0.49, 10, true)

License

MIT. See LICENSE.md for details.

Dependents (0)

Package Sidebar

Install

npm i snap-lerp

Weekly Downloads

1

Version

1.1.0

License

MIT

Last publish

Collaborators

  • hughsk