Line data Source code
1 : import type { Point2D } from '@comfyorg/shared-frontend-utils/geometry'
2 : export interface PanZoomEls {
3 : viewport: HTMLElement
4 : container: HTMLElement
5 : }
6 :
7 : export interface PanZoom {
8 : zoom: () => number
9 : setZoom: (ratio: number) => void
10 : invalidate: () => void
11 : fit: (artW: number, artH: number) => void
12 : setArtboardSize: (artW: number, artH: number) => void
13 : panBy: (dx: number, dy: number) => void
14 : handleWheel: (e: WheelEvent) => void
15 : screenToArtboard: (clientX: number, clientY: number) => Point2D
16 : onChange: (fn: () => void) => () => void
17 : }
18 :
19 2 : export function createPanZoom(getEls: () => PanZoomEls | null): PanZoom {
20 2 : let zoomRatio = 1
21 2 : let panX = 0
22 2 : let panY = 0
23 2 : let artW = 1024
24 2 : let artH = 1024
25 2 : const listeners = new Set<() => void>()
26 :
27 6 : function invalidate(): void {
28 6 : const els = getEls()
29 6 : if (!els) return
30 6 : Object.assign(els.container.style, {
31 : width: `${artW * zoomRatio}px`,
32 : height: `${artH * zoomRatio}px`,
33 : left: `${panX}px`,
34 : top: `${panY}px`
35 : })
36 6 : for (const fn of listeners) fn()
37 : }
38 :
39 6 : function fit(w: number, h: number): void {
40 6 : artW = w
41 6 : artH = h
42 6 : const els = getEls()
43 6 : if (!els) return
44 6 : const availW = els.viewport.clientWidth
45 6 : const availH = els.viewport.clientHeight
46 6 : if (availW <= 0 || availH <= 0) return
47 6 : const zoom = Math.min(availW / artW, availH / artH, 1) * 0.9
48 6 : zoomRatio = Math.max(0.01, zoom)
49 6 : panX = (availW - artW * zoomRatio) / 2
50 6 : panY = (availH - artH * zoomRatio) / 2
51 6 : invalidate()
52 : }
53 :
54 0 : function setArtboardSize(w: number, h: number): void {
55 0 : if (w === artW && h === artH) return
56 0 : artW = w
57 0 : artH = h
58 0 : invalidate()
59 : }
60 :
61 0 : function panBy(dx: number, dy: number): void {
62 0 : panX += dx
63 0 : panY += dy
64 0 : invalidate()
65 : }
66 :
67 0 : function setZoom(ratio: number): void {
68 0 : const els = getEls()
69 0 : if (!els) return
70 0 : const newZoom = Math.max(0.05, Math.min(20, ratio))
71 0 : if (newZoom === zoomRatio) return
72 0 : const cx = els.viewport.clientWidth / 2
73 0 : const cy = els.viewport.clientHeight / 2
74 0 : const scale = newZoom / zoomRatio
75 0 : panX = cx - (cx - panX) * scale
76 0 : panY = cy - (cy - panY) * scale
77 0 : zoomRatio = newZoom
78 0 : invalidate()
79 : }
80 :
81 0 : function handleWheel(e: WheelEvent): void {
82 : const els = getEls()
83 0 : if (!els) return
84 0 : const oldZoom = zoomRatio
85 0 : const factor = e.deltaY < 0 ? 1.1 : 1 / 1.1
86 0 : const newZoom = Math.max(0.05, Math.min(20, oldZoom * factor))
87 0 : zoomRatio = newZoom
88 0 : const mouseX = e.offsetX - panX
89 0 : const mouseY = e.offsetY - panY
90 0 : const scale = newZoom / oldZoom
91 0 : panX += mouseX - mouseX * scale
92 0 : panY += mouseY - mouseY * scale
93 0 : invalidate()
94 : }
95 :
96 0 : function screenToArtboard(clientX: number, clientY: number): Point2D {
97 0 : const els = getEls()
98 0 : if (!els) return { x: 0, y: 0 }
99 0 : const rect = els.container.getBoundingClientRect()
100 0 : if (rect.width <= 0 || rect.height <= 0) return { x: 0, y: 0 }
101 0 : return {
102 : x: ((clientX - rect.left) / rect.width) * artW,
103 : y: ((clientY - rect.top) / rect.height) * artH
104 : }
105 : }
106 :
107 2 : return {
108 : zoom: () => zoomRatio,
109 : setZoom,
110 : invalidate,
111 : fit,
112 : setArtboardSize,
113 : panBy,
114 : handleWheel,
115 : screenToArtboard,
116 : onChange(fn: () => void) {
117 2 : listeners.add(fn)
118 2 : return () => listeners.delete(fn)
119 : }
120 : }
121 : }
|