Line data Source code
1 : import type {
2 : Point2D as Offset,
3 : Point2D as Point
4 : } from '@comfyorg/shared-frontend-utils/geometry'
5 :
6 29 : const ZOOM_MIN = 0.2
7 29 : const ZOOM_MAX = 10.0
8 :
9 : interface FitViewParams {
10 : rootWidth: number
11 : rootHeight: number
12 : imageWidth: number
13 : imageHeight: number
14 : toolPanelWidth: number
15 : sidePanelWidth: number
16 : }
17 :
18 : interface FitViewResult {
19 : zoomRatio: number
20 : panOffset: Offset
21 : fittedWidth: number
22 : fittedHeight: number
23 : }
24 :
25 29 : export function calculateFitView(params: FitViewParams): FitViewResult {
26 29 : const {
27 : rootWidth,
28 : rootHeight,
29 : imageWidth,
30 : imageHeight,
31 : toolPanelWidth,
32 : sidePanelWidth
33 : } = params
34 :
35 29 : const availableWidth = rootWidth - sidePanelWidth - toolPanelWidth
36 29 : const availableHeight = rootHeight
37 :
38 29 : const zoomRatioWidth = availableWidth / imageWidth
39 29 : const zoomRatioHeight = availableHeight / imageHeight
40 29 : const zoomRatio = Math.min(zoomRatioWidth, zoomRatioHeight)
41 :
42 29 : const aspectRatio = imageWidth / imageHeight
43 29 : const panOffset: Offset = { x: toolPanelWidth, y: 0 }
44 :
45 29 : let fittedWidth: number
46 29 : let fittedHeight: number
47 :
48 29 : if (zoomRatioHeight > zoomRatioWidth) {
49 0 : fittedWidth = availableWidth
50 0 : fittedHeight = fittedWidth / aspectRatio
51 0 : panOffset.y = (availableHeight - fittedHeight) / 2
52 : } else {
53 29 : fittedHeight = availableHeight
54 29 : fittedWidth = fittedHeight * aspectRatio
55 29 : panOffset.x = (availableWidth - fittedWidth) / 2 + toolPanelWidth
56 : }
57 :
58 29 : return { zoomRatio, panOffset, fittedWidth, fittedHeight }
59 : }
60 :
61 0 : export function clampZoom(zoom: number): number {
62 0 : return Math.max(ZOOM_MIN, Math.min(ZOOM_MAX, zoom))
63 : }
64 :
65 0 : export function getWheelZoomFactor(deltaY: number): number {
66 0 : return deltaY < 0 ? 1.1 : 0.9
67 : }
68 :
69 0 : export function calculateZoomAroundPoint(
70 : currentZoom: number,
71 : zoomFactor: number,
72 : panOffset: Offset,
73 : focalX: number,
74 : focalY: number
75 : ): { zoomRatio: number; panOffset: Offset } {
76 0 : const newZoom = clampZoom(currentZoom * zoomFactor)
77 0 : const scaleFactor = newZoom / currentZoom
78 :
79 0 : return {
80 : zoomRatio: newZoom,
81 : panOffset: {
82 : x: panOffset.x + focalX - focalX * scaleFactor,
83 : y: panOffset.y + focalY - focalY * scaleFactor
84 : }
85 : }
86 : }
87 :
88 10 : export function calculateDragPan(
89 : mouseDownPoint: Point,
90 : currentPoint: Point,
91 : initialPan: Offset
92 : ): Offset {
93 10 : return {
94 : x: initialPan.x - (mouseDownPoint.x - currentPoint.x),
95 : y: initialPan.y - (mouseDownPoint.y - currentPoint.y)
96 : }
97 : }
98 :
99 0 : export function calculateSingleTouchPan(
100 : lastPoint: Point,
101 : currentPoint: Point,
102 : panOffset: Offset
103 : ): Offset {
104 0 : return {
105 : x: panOffset.x + (currentPoint.x - lastPoint.x),
106 : y: panOffset.y + (currentPoint.y - lastPoint.y)
107 : }
108 : }
109 :
110 0 : export function getDistanceBetweenPoints(a: Point, b: Point): number {
111 0 : const dx = a.x - b.x
112 0 : const dy = a.y - b.y
113 0 : return Math.sqrt(dx * dx + dy * dy)
114 : }
115 :
116 0 : export function getMidpoint(a: Point, b: Point): Point {
117 0 : return {
118 : x: (a.x + b.x) / 2,
119 : y: (a.y + b.y) / 2
120 : }
121 : }
122 :
123 230 : export function getCursorPoint(clientPoint: Point, panOffset: Offset): Point {
124 230 : return {
125 : x: clientPoint.x - panOffset.x,
126 : y: clientPoint.y - panOffset.y
127 : }
128 : }
129 :
130 0 : export function isDoubleTap(
131 : currentTime: number,
132 : lastTapTime: number,
133 : delay: number
134 : ): boolean {
135 0 : return currentTime - lastTapTime < delay
136 : }
137 :
138 0 : export function easeOutCubic(progress: number): number {
139 0 : return 1 - Math.pow(1 - progress, 3)
140 : }
141 :
142 0 : export function interpolateView(
143 : startZoom: number,
144 : targetZoom: number,
145 : startPan: Offset,
146 : targetPan: Offset,
147 : easedProgress: number
148 : ): { zoomRatio: number; panOffset: Offset } {
149 0 : return {
150 : zoomRatio: startZoom + (targetZoom - startZoom) * easedProgress,
151 : panOffset: {
152 : x: startPan.x + (targetPan.x - startPan.x) * easedProgress,
153 : y: startPan.y + (targetPan.y - startPan.y) * easedProgress
154 : }
155 : }
156 : }
157 :
158 70 : export function calculatePanZoomStyles(
159 : imageWidth: number,
160 : imageHeight: number,
161 : zoomRatio: number,
162 : panOffset: Offset
163 : ): {
164 : rawWidth: number
165 : rawHeight: number
166 : containerLeft: string
167 : containerTop: string
168 : containerWidth: string
169 : containerHeight: string
170 : } {
171 70 : const rawWidth = imageWidth * zoomRatio
172 70 : const rawHeight = imageHeight * zoomRatio
173 70 : return {
174 : rawWidth,
175 : rawHeight,
176 : containerLeft: `${panOffset.x}px`,
177 : containerTop: `${panOffset.y}px`,
178 : containerWidth: `${rawWidth}px`,
179 : containerHeight: `${rawHeight}px`
180 : }
181 : }
|