1.1.0 • Published 9 years ago

snap-lerp v1.1.0

Weekly downloads
4
License
MIT
Repository
github
Last release
9 years ago

snap-lerp

npm.io npm.io npm.io npm.io

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 + (b - 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.