Line data Source code
1 : import type { CompactRect as Rect } from '@comfyorg/shared-frontend-utils/geometry'
2 : import type { Transform } from '#/node'
3 :
4 : export type Bitmap = HTMLCanvasElement | ImageBitmap | OffscreenCanvas
5 :
6 2 : const mipChains = new WeakMap<Bitmap, HTMLCanvasElement[]>()
7 :
8 0 : function halve(src: Bitmap | HTMLCanvasElement): HTMLCanvasElement | null {
9 0 : const w = Math.max(1, Math.floor(src.width / 2))
10 0 : const h = Math.max(1, Math.floor(src.height / 2))
11 0 : const c = document.createElement('canvas')
12 0 : c.width = w
13 0 : c.height = h
14 0 : const g = c.getContext('2d')
15 0 : if (!g) return null
16 0 : g.imageSmoothingEnabled = true
17 0 : g.imageSmoothingQuality = 'high'
18 0 : g.drawImage(src, 0, 0, w, h)
19 0 : return c
20 : }
21 :
22 8 : export function mipForScale(bitmap: Bitmap, scale: number): Bitmap {
23 8 : if (scale >= 0.5 || bitmap.width <= 1 || bitmap.height <= 1) return bitmap
24 0 : let levels = mipChains.get(bitmap)
25 0 : if (!levels) {
26 0 : levels = []
27 0 : mipChains.set(bitmap, levels)
28 : }
29 0 : let level = 0
30 0 : let remaining = scale
31 0 : while (remaining < 0.5) {
32 0 : const prev: Bitmap | HTMLCanvasElement =
33 : level === 0 ? bitmap : levels[level - 1]
34 0 : if (prev.width <= 1 || prev.height <= 1) break
35 0 : if (!levels[level]) {
36 0 : const next = halve(prev)
37 0 : if (!next) break
38 0 : levels[level] = next
39 : }
40 0 : remaining *= 2
41 0 : level += 1
42 : }
43 8 : return level === 0 ? bitmap : levels[level - 1]
44 : }
45 :
46 8 : export function placeBitmap(
47 : bitmap: Bitmap,
48 : transform: Transform,
49 : docWidth: number,
50 : docHeight: number,
51 : scratch?: HTMLCanvasElement,
52 : clipRect?: Rect | null,
53 : noMip = false
54 : ): HTMLCanvasElement | null {
55 8 : const canvas = scratch ?? document.createElement('canvas')
56 8 : const clip =
57 : clipRect &&
58 : scratch &&
59 : canvas.width === docWidth &&
60 : canvas.height === docHeight
61 : ? clipRect
62 : : null
63 8 : if (canvas.width !== docWidth) canvas.width = docWidth
64 8 : if (canvas.height !== docHeight) canvas.height = docHeight
65 8 : const ctx = canvas.getContext('2d')
66 8 : if (!ctx) return null
67 8 : const scale = Math.max(
68 : transform.w / Math.max(1, bitmap.width),
69 : transform.h / Math.max(1, bitmap.height)
70 : )
71 8 : const src = noMip ? bitmap : mipForScale(bitmap, scale)
72 8 : ctx.save()
73 8 : if (clip) {
74 0 : ctx.beginPath()
75 0 : ctx.rect(clip.x, clip.y, clip.w, clip.h)
76 0 : ctx.clip()
77 0 : ctx.clearRect(clip.x, clip.y, clip.w, clip.h)
78 : } else {
79 8 : ctx.clearRect(0, 0, docWidth, docHeight)
80 : }
81 8 : ctx.imageSmoothingEnabled = true
82 8 : ctx.imageSmoothingQuality = 'high'
83 8 : ctx.translate(transform.x + transform.w / 2, transform.y + transform.h / 2)
84 8 : ctx.rotate(transform.rotation)
85 8 : ctx.drawImage(
86 : src,
87 : -transform.w / 2,
88 : -transform.h / 2,
89 : transform.w,
90 : transform.h
91 : )
92 8 : ctx.restore()
93 8 : return canvas
94 : }
|