Tiny and performant class for tracking the instantaneous velocity of a continuously changing value.
npm install kinetic-value
import KineticValue from "kinetic-value";
// Create a kinetic value
const kv = new KineticValue(0);
setInterval(() => {
// Increment by 10 every 20ms
kv.set(kv.get() + 50);
// Velocity will be about 500
console.log(kv.velocity());
}, 20);
Creates a new kinetic value.
timeout
is the number of milliseconds to wait until the value is considered stopped. Default 50.
Returns the current tracked value.
Updates the tracked value.
Returns the instantaneous velocity of the tracked value in units / second.
Resets the velocity to 0.