Line data Source code
1 : import { tryOnScopeDispose } from '@vueuse/core'
2 :
3 : import type { DragAndScale } from '@/lib/litegraph/src/DragAndScale'
4 : import { onCanvasFrame } from '@/lib/litegraph/src/canvas/canvasFrame'
5 :
6 2356 : const EDGE_THRESHOLD = 50
7 2356 : const STALL_LIMIT_MS = 50
8 2356 : const REFERENCE_FRAME_MS = 1000 / 60
9 :
10 : interface AutoPanOptions {
11 : canvas: {
12 : canvas: HTMLCanvasElement
13 : ds: DragAndScale
14 : }
15 : maxPanSpeed: number
16 : onPan: (canvasDeltaX: number, canvasDeltaY: number) => void
17 : }
18 :
19 : /**
20 : * Calculates the pan speed for a single axis based on distance from the edge.
21 : * Returns negative speed for left/top edges, positive for right/bottom edges,
22 : * or 0 if the pointer is not near any edge. Pans at max speed when the
23 : * pointer is outside the bounds (e.g. dragged outside the window).
24 : */
25 7320 : export function calculateEdgePanSpeed(
26 : pointerPos: number,
27 : minBound: number,
28 : maxBound: number,
29 : scale: number,
30 : maxPanSpeed: number
31 : ): number {
32 7320 : if (maxPanSpeed <= 0) return 0
33 :
34 7146 : const distFromMin = pointerPos - minBound
35 7146 : const distFromMax = maxBound - pointerPos
36 :
37 7152 : if (distFromMin < 0) return -maxPanSpeed / scale
38 :
39 7152 : if (distFromMax < 0) return maxPanSpeed / scale
40 :
41 7152 : if (distFromMin < EDGE_THRESHOLD) {
42 149 : return (-maxPanSpeed * (1 - distFromMin / EDGE_THRESHOLD)) / scale
43 : }
44 :
45 7062 : if (distFromMax < EDGE_THRESHOLD) {
46 10 : return (maxPanSpeed * (1 - distFromMax / EDGE_THRESHOLD)) / scale
47 : }
48 :
49 6987 : return 0
50 : }
51 :
52 2356 : export class AutoPanController {
53 : private pointerX = 0
54 : private pointerY = 0
55 : private readonly canvas: AutoPanOptions['canvas']
56 : private readonly maxPanSpeed: number
57 : private readonly onPan: (dx: number, dy: number) => void
58 : private stopFrame: (() => void) | undefined
59 : private lastFrameTime = 0
60 :
61 : constructor(options: AutoPanOptions) {
62 254 : this.canvas = options.canvas
63 254 : this.maxPanSpeed = options.maxPanSpeed
64 254 : this.onPan = options.onPan
65 254 : tryOnScopeDispose(() => this.stop())
66 : }
67 :
68 : updatePointer(screenX: number, screenY: number) {
69 2578 : this.pointerX = screenX
70 2578 : this.pointerY = screenY
71 : }
72 :
73 : start() {
74 617 : if (this.stopFrame) return
75 249 : this.lastFrameTime = performance.now()
76 249 : this.stopFrame = onCanvasFrame(this.canvas, 'update', (frameIntervalMs) =>
77 : this.tick(frameIntervalMs)
78 : )
79 : }
80 :
81 : stop() {
82 256 : this.stopFrame?.()
83 256 : this.stopFrame = undefined
84 : }
85 :
86 : private tick(frameIntervalMs: number) {
87 3660 : const { canvas, ds } = this.canvas
88 3660 : const now = performance.now()
89 : const stallLimit = Math.max(STALL_LIMIT_MS, frameIntervalMs)
90 3660 : const elapsed = Math.min(stallLimit, now - this.lastFrameTime)
91 3660 : this.lastFrameTime = now
92 3660 : const maxPanSpeed = (this.maxPanSpeed * elapsed) / REFERENCE_FRAME_MS
93 3660 : const rect = canvas.getBoundingClientRect()
94 3660 : const scale = ds.scale
95 :
96 3660 : const panX = calculateEdgePanSpeed(
97 : this.pointerX,
98 : rect.left,
99 : rect.right,
100 : scale,
101 : maxPanSpeed
102 : )
103 3660 : const panY = calculateEdgePanSpeed(
104 : this.pointerY,
105 : rect.top,
106 : rect.bottom,
107 : scale,
108 : maxPanSpeed
109 : )
110 :
111 3660 : if (panX === 0 && panY === 0) return
112 :
113 90 : ds.offset[0] -= panX
114 90 : ds.offset[1] -= panY
115 :
116 90 : this.onPan(panX, panY)
117 : }
118 : }
|