Skip to content

hughsk/snap-lerp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Linearly interpolate two numbers, but snap to the closest value if the difference between them is small enough

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors