UNPKG

@mui/x-internal-gestures

Version:

The core engine of GestureEvents, a modern and robust multi-pointer gesture detection library for JavaScript.

21 lines 617 B
/** * Calculate the velocity of movement between two points */ export function getVelocity(startPointer, endPointer) { const timeElapsed = (endPointer.timeStamp - startPointer.timeStamp) / 1000; // in seconds if (timeElapsed === 0) { return { velocityX: 0, velocityY: 0, velocity: 0 }; } const velocityX = (endPointer.clientX - startPointer.clientX) / timeElapsed; const velocityY = (endPointer.clientY - startPointer.clientY) / timeElapsed; const velocity = Math.sqrt(velocityX * velocityX + velocityY * velocityY); return { velocityX, velocityY, velocity }; }