tldraw/hooks/useZoomEvents.ts

86 lines
2.1 KiB
TypeScript
Raw Normal View History

2021-06-29 12:00:59 +00:00
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { useRef } from 'react'
2021-05-28 16:25:43 +00:00
import state from 'state'
import inputs from 'state/inputs'
import vec from 'utils/vec'
import { useGesture } from 'react-use-gesture'
import {
fastBrushSelect,
fastDrawUpdate,
fastPanUpdate,
fastPinchCamera,
fastTransform,
fastTranslate,
fastZoomUpdate,
} from 'state/hacks'
2021-05-09 13:04:42 +00:00
2021-05-09 21:22:25 +00:00
/**
* Capture zoom gestures (pinches, wheels and pans) and send to the state.
* @param ref
* @returns
*/
export default function useZoomEvents() {
2021-05-28 13:08:51 +00:00
const rPinchDa = useRef<number[] | undefined>(undefined)
const rPinchPoint = useRef<number[] | undefined>(undefined)
useGesture(
{
onWheel: ({ event, delta }) => {
if (event.ctrlKey) {
const { point } = inputs.wheel(event as WheelEvent)
fastZoomUpdate(point, delta[1])
return
}
fastPanUpdate(delta)
const info = inputs.pointer
if (state.isIn('draw.editing')) {
fastDrawUpdate(info)
} else if (state.isIn('brushSelecting')) {
fastBrushSelect(info.point)
} else if (state.isIn('translatingSelection')) {
fastTranslate(info)
} else if (state.isIn('transformingSelection')) {
fastTransform(info)
}
state.send('PANNED_CAMERA', {
delta,
...inputs.wheel(event as WheelEvent),
})
},
onPinch: ({ pinching, da, origin }) => {
if (!pinching) {
state.send('STOPPED_PINCHING')
rPinchDa.current = undefined
rPinchPoint.current = undefined
return
}
if (rPinchPoint.current === undefined) {
state.send('STARTED_PINCHING')
rPinchDa.current = da
rPinchPoint.current = origin
}
2021-06-21 21:35:28 +00:00
const [distanceDelta] = vec.sub(rPinchDa.current, da)
fastPinchCamera(
origin,
vec.sub(rPinchPoint.current, origin),
2021-06-21 21:35:28 +00:00
distanceDelta
)
rPinchDa.current = da
rPinchPoint.current = origin
},
},
{
domTarget: document.body,
eventOptions: { passive: false },
2021-05-28 13:08:51 +00:00
}
)
2021-05-09 13:04:42 +00:00
}