useVelocity
Commons
stable
Tracks touch-event samples per finger (identified by touch.identifier) in circular buffers and computes the instantaneous velocity vector at touchend via impulse-velocity analysis. Used by useTouch to decide whether a fling gesture is fast enough to open or close a drawer.
API
Signature
Full TypeScript signature sourced from the composable file.
useVelocity is a low-level primitive consumed internally by useTouch. The HISTORY constant controls the circular buffer size (number of samples kept) and HORIZON controls the time window (ms) over which velocity is computed. Direct use is rarely needed — prefer useTouch for drawer gestures.
API
Return values
Properties and methods exposed by the composable return object.
- addMovement
- Record a touchstart or touchmove event sample. Each touch point is tracked independently. Call this on every touchmove to build a time-stamped sample history.
- endTouch
- Clear the circular buffer for all changed touches. Call at touchstart before starting a new gesture to discard leftover samples from a previous interaction.
- getVelocity
- Compute the velocity vector for the given touch identifier from the recent sample history. Returns px/ms values for x and y axes and the dominant direction string. Throws if no samples are found for the id.
Usage
Examples
Ready-to-paste usage snippets.
Fling detection in a custom swipeable