Skip to content

Commit

Permalink
Merge pull request #4 from malte-j/ref-norm-wheel-impl
Browse files Browse the repository at this point in the history
Add reference normalizeWheelEvent
  • Loading branch information
graham- authored May 30, 2023
2 parents 9995073 + 7099bbe commit 2f3cab1
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,39 @@ export const clientToSVGElementCoords = (el: SVGSVGElement, coords: Coords): Coo
return point.matrixTransform(screenToElement);
};

function limit(delta: number, max_delta: number) {
return Math.sign(delta) * Math.min(max_delta, Math.abs(delta));
}

function normalizeWheel(e: WheelEvent) {
const DELTA_LINE_MULTIPLIER = 8;
const DELTA_PAGE_MULTIPLIER = 24;
const MAX_WHEEL_DELTA = 24;

let dx = e.deltaX;
let dy = e.deltaY;

if (e.shiftKey && dx === 0) {
[dx, dy] = [dy, dx];
}

if (e.deltaMode === WheelEvent.DOM_DELTA_LINE) {
dx *= DELTA_LINE_MULTIPLIER;
dy *= DELTA_LINE_MULTIPLIER;
} else if (e.deltaMode === WheelEvent.DOM_DELTA_PAGE) {
dx *= DELTA_PAGE_MULTIPLIER;
dy *= DELTA_PAGE_MULTIPLIER;
}
return {
dx: limit(dx, MAX_WHEEL_DELTA),
dy: limit(dy, MAX_WHEEL_DELTA),
};
}

export const twoFingers = (
container: Element,
{ onGestureStart, onGestureChange, onGestureEnd }: GestureCallbacks = {},
normalizeWheelEvent: (event: WheelEvent) => NormalizedWheelEvent,
normalizeWheelEvent: (event: WheelEvent) => NormalizedWheelEvent = normalizeWheel,
): (() => void) => {
// TODO: we shouldn't be reusing gesture
let gesture: Gesture | undefined = undefined;
Expand Down

0 comments on commit 2f3cab1

Please sign in to comment.