Line data Source code
1 : import type { Size2D } from '@comfyorg/shared-frontend-utils/geometry'
2 : interface CanvasViewport {
3 : readonly cssWidth: number
4 : readonly cssHeight: number
5 : readonly dpr: number
6 : readonly physicalWidth: number
7 : readonly physicalHeight: number
8 : }
9 :
10 : interface CanvasViewportConsumer {
11 : dpr: number
12 : ds: { setViewportSize(width: number, height: number): void }
13 : }
14 : type Size = Readonly<Size2D>
15 :
16 2356 : const appliedViewportByCanvas = new WeakMap<HTMLCanvasElement, CanvasViewport>()
17 2356 : const autoSizedStyleByCanvas = new WeakMap<
18 : HTMLCanvasElement,
19 : { width?: string; height?: string }
20 : >()
21 2356 : const sizeOwnershipByCanvas = new WeakMap<
22 : HTMLCanvasElement,
23 : { signature: string; ownership: SizeOwnership }
24 : >()
25 :
26 2445 : function probeCssSize(canvas: HTMLCanvasElement): Size {
27 2445 : const savedWidth = canvas.width
28 2445 : const savedHeight = canvas.height
29 2445 : try {
30 2445 : canvas.width = 0
31 2445 : canvas.height = 0
32 2445 : const { width, height } = canvas.getBoundingClientRect()
33 2445 : return { width, height }
34 : } finally {
35 2445 : canvas.width = savedWidth
36 2445 : canvas.height = savedHeight
37 2445 : const dpr = appliedViewportByCanvas.get(canvas)?.dpr ?? 1
38 2445 : canvas.getContext('2d')?.setTransform(dpr, 0, 0, dpr, 0, 0)
39 : }
40 : }
41 :
42 : interface SizeOwnership {
43 : readonly independentWidth: boolean
44 : readonly independentHeight: boolean
45 : readonly discardedBitmap: boolean
46 : }
47 :
48 2 : function readContentBox(canvas: HTMLCanvasElement): Size {
49 2 : const style = getComputedStyle(canvas)
50 2 : const horizontalInsets =
51 : Number.parseFloat(style.paddingLeft) +
52 : Number.parseFloat(style.paddingRight) +
53 : Number.parseFloat(style.borderLeftWidth) +
54 : Number.parseFloat(style.borderRightWidth)
55 2 : const verticalInsets =
56 : Number.parseFloat(style.paddingTop) +
57 : Number.parseFloat(style.paddingBottom) +
58 : Number.parseFloat(style.borderTopWidth) +
59 : Number.parseFloat(style.borderBottomWidth)
60 2 : return {
61 : width: canvas.offsetWidth - (horizontalInsets || 0),
62 : height: canvas.offsetHeight - (verticalInsets || 0)
63 : }
64 : }
65 :
66 4 : function isAxisSizedElsewhere(
67 : rectSize: number,
68 : requestedSize: number,
69 : backingSize: number,
70 : layoutSize: number,
71 : hasInlineSize: boolean
72 : ): boolean | undefined {
73 4 : if (!(rectSize > 0 && Math.abs(rectSize - requestedSize) < 1)) return false
74 4 : if (hasInlineSize || backingSize !== layoutSize) return true
75 : return undefined
76 : }
77 :
78 2 : function readSizeOwnership(
79 : canvas: HTMLCanvasElement,
80 : width: number,
81 : height: number
82 : ): SizeOwnership {
83 2 : const rect = canvas.getBoundingClientRect()
84 2 : const layout = readContentBox(canvas)
85 2 : const independentWidth = isAxisSizedElsewhere(
86 : rect.width,
87 : width,
88 : canvas.width,
89 : layout.width,
90 : Boolean(canvas.style.width)
91 : )
92 2 : const independentHeight = isAxisSizedElsewhere(
93 : rect.height,
94 : height,
95 : canvas.height,
96 : layout.height,
97 : Boolean(canvas.style.height)
98 : )
99 2 : if (independentWidth !== undefined && independentHeight !== undefined)
100 1 : return { independentWidth, independentHeight, discardedBitmap: false }
101 :
102 1 : const signature = [
103 : width,
104 : height,
105 : rect.width,
106 : rect.height,
107 : layout.width,
108 : layout.height,
109 : canvas.width,
110 : canvas.height,
111 : canvas.style.cssText,
112 : canvas.className
113 : ].join('|')
114 1 : const cached = sizeOwnershipByCanvas.get(canvas)
115 2 : if (cached?.signature === signature)
116 0 : return { ...cached.ownership, discardedBitmap: false }
117 :
118 1 : const probed = probeCssSize(canvas)
119 2 : const ownership = {
120 : independentWidth: independentWidth ?? probed.width > 0,
121 : independentHeight: independentHeight ?? probed.height > 0,
122 : discardedBitmap: true
123 : }
124 2 : sizeOwnershipByCanvas.set(canvas, { signature, ownership })
125 2 : return ownership
126 : }
127 :
128 : /**
129 : * Pins the canvas's logical CSS size, unless something else already owns it.
130 : *
131 : * @returns whether deciding that discarded the canvas bitmap. A caller that
132 : * skips work when nothing resized must still repaint when this is true.
133 : */
134 2 : function applyLogicalCanvasStyle(
135 : canvas: HTMLCanvasElement,
136 : width: number,
137 : height: number
138 : ): boolean {
139 2 : if (!(width > 0) || !(height > 0)) return false
140 :
141 2 : const { independentWidth, independentHeight, discardedBitmap } =
142 : readSizeOwnership(canvas, width, height)
143 2 : const { style } = canvas
144 2 : const previousStyle = autoSizedStyleByCanvas.get(canvas) ?? {}
145 2 : const nextStyle: { width?: string; height?: string } = { ...previousStyle }
146 2 : if (
147 : !independentWidth &&
148 : (!style.width || style.width === previousStyle.width)
149 : ) {
150 1 : style.width = `${width}px`
151 1 : nextStyle.width = style.width
152 : }
153 2 : if (
154 : !independentHeight &&
155 : (!style.height || style.height === previousStyle.height)
156 : ) {
157 1 : style.height = `${height}px`
158 1 : nextStyle.height = style.height
159 : }
160 2 : autoSizedStyleByCanvas.set(canvas, nextStyle)
161 2 : return discardedBitmap
162 : }
163 :
164 8697 : function normalizeDpr(rawDpr: number): number {
165 8697 : return rawDpr > 0 && Number.isFinite(rawDpr) ? Math.max(rawDpr, 1) : 1
166 : }
167 :
168 2357 : function readBrowserDpr(): number {
169 2357 : return typeof window === 'undefined'
170 : ? 1
171 : : normalizeDpr(window.devicePixelRatio)
172 : }
173 :
174 6340 : function measureViewport(
175 : cssWidth: number,
176 : cssHeight: number,
177 : rawDpr: number
178 : ): CanvasViewport {
179 : // Keep at least one backing pixel per CSS pixel. DPR values below one are
180 : // valid browser inputs, but using them directly makes the canvas blurry.
181 6340 : const dpr = normalizeDpr(rawDpr)
182 6340 : return Object.freeze({
183 : cssWidth,
184 : cssHeight,
185 : dpr,
186 : physicalWidth: Math.round(cssWidth * dpr),
187 : physicalHeight: Math.round(cssHeight * dpr)
188 : })
189 : }
190 :
191 6338 : function measureViewportFromElement(
192 : element: HTMLCanvasElement,
193 : rawDpr = window.devicePixelRatio
194 : ): CanvasViewport {
195 6338 : const initialRect = element.getBoundingClientRect()
196 6338 : if (initialRect.width === 0 || initialRect.height === 0) {
197 89 : return measureViewport(initialRect.width, initialRect.height, rawDpr)
198 : }
199 :
200 6249 : const previousViewport = appliedViewportByCanvas.get(element)
201 6338 : if (
202 : previousViewport &&
203 : initialRect.width === previousViewport.cssWidth &&
204 : initialRect.height === previousViewport.cssHeight
205 : ) {
206 3805 : return measureViewport(
207 : previousViewport.cssWidth,
208 : previousViewport.cssHeight,
209 : rawDpr
210 : )
211 : }
212 :
213 2444 : const cssSize = probeCssSize(element)
214 : const width = cssSize.width || previousViewport?.cssWidth || initialRect.width
215 : const height =
216 : cssSize.height || previousViewport?.cssHeight || initialRect.height
217 6338 : return measureViewport(width, height, rawDpr)
218 : }
219 :
220 12678 : function applySurfaceViewport(
221 : canvas: HTMLCanvasElement,
222 : viewport: CanvasViewport
223 : ): void {
224 12678 : const { physicalWidth, physicalHeight, dpr } = viewport
225 12678 : if (canvas.width !== physicalWidth) canvas.width = physicalWidth
226 12678 : if (canvas.height !== physicalHeight) canvas.height = physicalHeight
227 12678 : canvas.getContext('2d')?.setTransform(dpr, 0, 0, dpr, 0, 0)
228 : }
229 :
230 6339 : function applyViewport(
231 : viewport: CanvasViewport,
232 : fg: HTMLCanvasElement,
233 : bg: HTMLCanvasElement,
234 : consumer?: CanvasViewportConsumer
235 : ): CanvasViewport {
236 6339 : applySurfaceViewport(fg, viewport)
237 6339 : if (bg !== fg) applySurfaceViewport(bg, viewport)
238 :
239 6339 : appliedViewportByCanvas.set(fg, viewport)
240 6339 : appliedViewportByCanvas.set(bg, viewport)
241 6339 : if (consumer) {
242 6339 : consumer.dpr = viewport.dpr
243 6339 : consumer.ds.setViewportSize(viewport.cssWidth, viewport.cssHeight)
244 : }
245 6339 : return viewport
246 : }
247 :
248 : export {
249 : readBrowserDpr,
250 : measureViewport,
251 : measureViewportFromElement,
252 : applyLogicalCanvasStyle,
253 : applyViewport
254 : }
|