npm

npm’s 2019 JavaScript ecosystem survey analysis is now available! Get your copy here »

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.

install

npm i snap-lerp

Downloadsweekly downloads

2

version

1.1.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability