Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function onPointerDown(
event: MouseEvent | TouchEvent | PointerEvent,
info: EventInfo
) {
// If we have more than one touch, don't start detecting this gesture
if (isTouchEvent(event) && event.touches.length > 1) return
const initialInfo = transformPoint(info)
const { point } = initialInfo
const { timestamp } = getFrameData()
session.current = {
target: event.target,
pointHistory: [{ ...point, timestamp }],
}
const { onPanSessionStart } = handlersRef.current
onPanSessionStart && onPanSessionStart(event, getPanInfo(initialInfo))
removePointerEvents()
const removeOnPointerMove = addPointerEvent(
window,
"pointermove",
onPointerMove
)
cancelPan()
return
}
const info = getPanInfo(lastMoveEventInfo.current)
const panStarted = session.current.startEvent !== undefined
// Only start panning if the offset is larger than 3 pixels. If we make it
// any larger than this we'll want to reset the pointer history
// on the first update to avoid visual snapping to the cursoe.
const distancePastThreshold = distance(info.offset, { x: 0, y: 0 }) >= 3
if (!panStarted && !distancePastThreshold) return
const { point } = info
const { timestamp } = getFrameData()
session.current.pointHistory.push({ ...point, timestamp })
const { onPanStart, onPan } = handlersRef.current
if (!panStarted) {
onPanStart && onPanStart(lastMoveEvent.current, info)
session.current.startEvent = lastMoveEvent.current
}
onPan && onPan(lastMoveEvent.current, info)
}
return (v: number) => {
const currentFramestamp = getFrameData().timestamp;
const timeDelta =
currentFramestamp !== lastUpdated ? currentFramestamp - lastUpdated : 0;
const newValue = timeDelta
? smoothFrame(previousValue, v, timeDelta, strength)
: previousValue;
lastUpdated = currentFramestamp;
previousValue = newValue;
return newValue;
};
};
update(v: Value) {
super.update(v);
this.prev = this.current;
this.updateCurrent(v);
const { delta, timestamp } = getFrameData();
this.timeDelta = delta;
this.lastUpdated = timestamp;
sync.postRender(this.scheduleVelocityCheck);
}
return (v: number) => {
const currentFramestamp = getFrameData().timestamp;
const timeDelta =
currentFramestamp !== lastUpdated ? currentFramestamp - lastUpdated : 0;
const newValue = timeDelta
? calcSmoothing(v, previousValue, timeDelta, strength)
: previousValue;
lastUpdated = currentFramestamp;
previousValue = newValue;
return newValue;
};
};