feat(core): add useGetPointerPosition composable
Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import type { UseDragEvent } from './useDrag'
|
||||
import type { SnapGrid } from '~/types'
|
||||
|
||||
export function useGetPointerPosition() {
|
||||
const { viewport, snapGrid: globalSnapGrid, snapToGrid } = useVueFlow()
|
||||
|
||||
const hasSnapGrid = (sg?: SnapGrid) => (sg ?? snapToGrid ? globalSnapGrid : undefined)
|
||||
|
||||
// returns the pointer position projected to the RF coordinate system
|
||||
return ({ sourceEvent }: UseDragEvent, snapGrid?: SnapGrid) => {
|
||||
const currentSnapGrid = unref(hasSnapGrid(snapGrid || globalSnapGrid.value))
|
||||
|
||||
const x = sourceEvent.touches ? sourceEvent.touches[0].clientX : sourceEvent.clientX
|
||||
const y = sourceEvent.touches ? sourceEvent.touches[0].clientY : sourceEvent.clientY
|
||||
|
||||
const pointerPos = {
|
||||
x: (x - viewport.value.x) / viewport.value.zoom,
|
||||
y: (y - viewport.value.y) / viewport.value.zoom,
|
||||
}
|
||||
|
||||
// we need the snapped position in order to be able to skip unnecessary drag events
|
||||
return {
|
||||
xSnapped: currentSnapGrid ? currentSnapGrid[0] * Math.round(pointerPos.x / currentSnapGrid[0]) : pointerPos.x,
|
||||
ySnapped: currentSnapGrid ? currentSnapGrid[1] * Math.round(pointerPos.y / currentSnapGrid[1]) : pointerPos.y,
|
||||
...pointerPos,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user