@thibka/mouse-speed
v2.0.0
Published
Smoothed mouse speed
Readme
@thibka/mouse-speed
Smoothed mouse velocity tracking for the browser. This tiny utility listens to mousemove and exposes a damped velocity vector you can sample each animation frame.
Install
npm i @thibka/mouse-speedUsage
import MouseSpeed from '@thibka/mouse-speed';
const mouseSpeed = new MouseSpeed({
acceleration: .05,
deceleration: .1
});
function loop() {
requestAnimationFrame( loop );
mouseSpeed.update();
console.log(mouseSpeed.velocity.x, mouseSpeed.velocity.y);
}
loop();Options
acceleration
Default:.1
The rate at which velocity increases. A value between 0 and 1 provides a smoothed (lerped) acceleration. 1 is instant.deceleration
Default:.1
The rate at which velocity decreases back to zero. A value between 0 and 1 provides a smoothed (lerped) deceleration. 1 is instant.
Properties
velocity:{ x: number, y: number }Current smoothed velocity delta since last frame.
Methods
update()
Samples the latest mouse position and updatesvelocity. Call once per frame.destroy()
Removes the internalmousemovelistener. Call this when you no longer need the instance.
