Skip to content

Commit

Permalink
refactor: remove unused velocity data
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Nov 20, 2024
1 parent c794e08 commit f253f5d
Showing 1 changed file with 1 addition and 62 deletions.
63 changes: 1 addition & 62 deletions packages/utilities/dom-event/src/track-pointer-move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ interface Point {
y: number
}

interface TimestampedPoint extends Point {
/**
* The time when the point was recorded.
*/
timestamp: number
}

export interface PointerMoveDetails {
/**
* The current position of the pointer.
Expand All @@ -23,10 +16,6 @@ export interface PointerMoveDetails {
* The event that triggered the move.
*/
event: PointerEvent
/**
* The velocity of the pointer on the x and y axis.
*/
velocity: Point
}

export interface PointerMoveHandlers {
Expand All @@ -43,11 +32,8 @@ export interface PointerMoveHandlers {
export function trackPointerMove(doc: Document, handlers: PointerMoveHandlers) {
const { onPointerMove, onPointerUp } = handlers

const history: TimestampedPoint[] = []

const handleMove = (event: PointerEvent) => {
const point = getEventPoint(event)
history.push({ ...point, timestamp: performance.now() })

const distance = Math.sqrt(point.x ** 2 + point.y ** 2)
const moveBuffer = event.pointerType === "touch" ? 10 : 5
Expand All @@ -60,7 +46,7 @@ export function trackPointerMove(doc: Document, handlers: PointerMoveHandlers) {
return
}

onPointerMove({ point, event, velocity: getVelocity(history, 0.1) })
onPointerMove({ point, event })
}

const cleanups = [
Expand All @@ -73,52 +59,5 @@ export function trackPointerMove(doc: Document, handlers: PointerMoveHandlers) {

return () => {
cleanups.forEach((cleanup) => cleanup())
history.length = 0
}
}

function lastDevicePoint(history: TimestampedPoint[]): TimestampedPoint {
return history[history.length - 1]
}

function ms(seconds: number): number {
return seconds * 1000
}

function sec(milliseconds: number): number {
return milliseconds / 1000
}

function getVelocity(history: TimestampedPoint[], timeDelta: number): Point {
if (history.length < 2) return { x: 0, y: 0 }

let i = history.length - 1
let timestampedPoint: TimestampedPoint | null = null
const lastPoint = lastDevicePoint(history)

while (i >= 0) {
timestampedPoint = history[i]
if (lastPoint.timestamp - timestampedPoint.timestamp > ms(timeDelta)) {
break
}
i--
}

if (!timestampedPoint) return { x: 0, y: 0 }

const time = sec(lastPoint.timestamp - timestampedPoint.timestamp)
if (time === 0) return { x: 0, y: 0 }

const currentVelocity = {
x: (lastPoint.x - timestampedPoint.x) / time,
y: (lastPoint.y - timestampedPoint.y) / time,
}

if (currentVelocity.x === Infinity) currentVelocity.x = 0
if (currentVelocity.y === Infinity) currentVelocity.y = 0

return {
x: Math.abs(currentVelocity.x),
y: Math.abs(currentVelocity.y),
}
}

0 comments on commit f253f5d

Please sign in to comment.