Line data Source code
1 : import type {
2 : Extents as DirtyRect,
3 : Point2D as Point
4 : } from '@comfyorg/shared-frontend-utils/geometry'
5 : import QuickLRU from '@alloc/quick-lru'
6 :
7 : import { hexToRgb } from '@comfyorg/shared-frontend-utils/colorUtil'
8 : import { parseToRgb } from './color'
9 : import { BrushShape } from './types'
10 : export type { Extents as DirtyRect } from '@comfyorg/shared-frontend-utils/geometry'
11 :
12 : type MaskColor = { r: number; g: number; b: number }
13 :
14 2355 : const brushTextureCache = new QuickLRU<string, HTMLCanvasElement>({
15 : maxSize: 20
16 : })
17 :
18 55 : export function resetDirtyRect(): DirtyRect {
19 55 : return { minX: Infinity, minY: Infinity, maxX: -Infinity, maxY: -Infinity }
20 : }
21 :
22 696 : export function updateDirtyRect(
23 : rect: DirtyRect,
24 : x: number,
25 : y: number,
26 : radius: number
27 : ): DirtyRect {
28 696 : const padding = 2
29 696 : return {
30 : minX: Math.min(rect.minX, x - radius - padding),
31 : minY: Math.min(rect.minY, y - radius - padding),
32 : maxX: Math.max(rect.maxX, x + radius + padding),
33 : maxY: Math.max(rect.maxY, y + radius + padding)
34 : }
35 : }
36 :
37 38 : function formatRgba(hex: string, alpha: number): string {
38 38 : const { r, g, b } = hexToRgb(hex)
39 38 : return `rgba(${r}, ${g}, ${b}, ${alpha})`
40 : }
41 :
42 0 : export function premultiplyData(data: Uint8ClampedArray): void {
43 0 : for (let i = 0; i < data.length; i += 4) {
44 0 : const a = data[i + 3] / 255
45 0 : data[i] = Math.round(data[i] * a)
46 0 : data[i + 1] = Math.round(data[i + 1] * a)
47 0 : data[i + 2] = Math.round(data[i + 2] * a)
48 : }
49 : }
50 :
51 696 : function drawShapeOnContext(
52 : ctx: CanvasRenderingContext2D,
53 : brushType: BrushShape,
54 : x: number,
55 : y: number,
56 : radius: number
57 : ): void {
58 696 : ctx.beginPath()
59 696 : if (brushType === BrushShape.Rect) {
60 0 : ctx.rect(x - radius, y - radius, radius * 2, radius * 2)
61 : } else {
62 696 : ctx.arc(x, y, radius, 0, Math.PI * 2, false)
63 : }
64 696 : ctx.fill()
65 : }
66 :
67 0 : function createBrushGradient(
68 : ctx: CanvasRenderingContext2D,
69 : x: number,
70 : y: number,
71 : radius: number,
72 : hardness: number,
73 : color: string,
74 : opacity: number,
75 : isErasing: boolean
76 : ): CanvasGradient {
77 0 : if (!Number.isFinite(x) || !Number.isFinite(y) || !Number.isFinite(radius)) {
78 0 : return ctx.createRadialGradient(0, 0, 0, 0, 0, 0)
79 : }
80 :
81 0 : const gradient = ctx.createRadialGradient(x, y, 0, x, y, radius)
82 :
83 0 : if (isErasing) {
84 0 : gradient.addColorStop(0, `rgba(255, 255, 255, ${opacity})`)
85 0 : gradient.addColorStop(hardness, `rgba(255, 255, 255, ${opacity})`)
86 0 : gradient.addColorStop(1, `rgba(255, 255, 255, 0)`)
87 : } else {
88 0 : const { r, g, b } = parseToRgb(color)
89 0 : gradient.addColorStop(0, `rgba(${r}, ${g}, ${b}, ${opacity})`)
90 0 : gradient.addColorStop(hardness, `rgba(${r}, ${g}, ${b}, ${opacity})`)
91 0 : gradient.addColorStop(1, `rgba(${r}, ${g}, ${b}, 0)`)
92 : }
93 :
94 0 : return gradient
95 : }
96 :
97 0 : function getCachedBrushTexture(
98 : radius: number,
99 : hardness: number,
100 : color: string,
101 : opacity: number
102 : ): HTMLCanvasElement {
103 0 : const cacheKey = `${radius}_${hardness}_${color}`
104 0 : const cached = brushTextureCache.get(cacheKey)
105 0 : if (cached) return cached
106 :
107 0 : const size = Math.max(1, Math.ceil(radius * 2))
108 0 : if (!Number.isFinite(size)) {
109 0 : throw new Error(`Invalid brush radius: ${radius}`)
110 : }
111 :
112 0 : const tempCanvas = document.createElement('canvas')
113 0 : tempCanvas.width = size
114 0 : tempCanvas.height = size
115 0 : const tempCtx = tempCanvas.getContext('2d')
116 0 : if (!tempCtx) {
117 0 : throw new Error('Unable to create 2D canvas context for brush texture')
118 : }
119 :
120 0 : const centerX = size / 2
121 0 : const centerY = size / 2
122 0 : const hardRadius = radius * hardness
123 0 : const imageData = tempCtx.createImageData(size, size)
124 0 : const data = imageData.data
125 0 : const { r, g, b } = parseToRgb(color)
126 0 : const fadeRange = radius - hardRadius
127 :
128 0 : for (let y = 0; y < size; y++) {
129 0 : const dy = y + 0.5 - centerY
130 0 : for (let x = 0; x < size; x++) {
131 0 : const dx = x + 0.5 - centerX
132 0 : const index = (y * size + x) * 4
133 0 : const distFromEdge = Math.max(Math.abs(dx), Math.abs(dy))
134 :
135 0 : let pixelOpacity = 0
136 0 : if (distFromEdge <= hardRadius) {
137 0 : pixelOpacity = opacity
138 0 : } else if (distFromEdge <= radius) {
139 0 : const fadeProgress = (distFromEdge - hardRadius) / fadeRange
140 0 : pixelOpacity = opacity * Math.pow(1 - fadeProgress, 2)
141 : }
142 :
143 0 : data[index] = r
144 0 : data[index + 1] = g
145 0 : data[index + 2] = b
146 0 : data[index + 3] = pixelOpacity * 255
147 : }
148 : }
149 :
150 0 : tempCtx.putImageData(imageData, 0, 0)
151 0 : brushTextureCache.set(cacheKey, tempCanvas)
152 0 : return tempCanvas
153 : }
154 :
155 38 : export function drawRgbShape(
156 : ctx: CanvasRenderingContext2D,
157 : point: Point,
158 : brushType: BrushShape,
159 : brushRadius: number,
160 : hardness: number,
161 : opacity: number,
162 : rgbColor: string
163 : ): void {
164 38 : const { x, y } = point
165 :
166 38 : if (brushType === BrushShape.Rect && hardness < 1) {
167 : const rgbaColor = formatRgba(rgbColor, opacity)
168 0 : const brushTexture = getCachedBrushTexture(
169 : brushRadius,
170 : hardness,
171 : rgbaColor,
172 : opacity
173 : )
174 0 : ctx.drawImage(brushTexture, x - brushRadius, y - brushRadius)
175 0 : return
176 : }
177 :
178 38 : if (hardness === 1) {
179 38 : ctx.fillStyle = formatRgba(rgbColor, opacity)
180 38 : drawShapeOnContext(ctx, brushType, x, y, brushRadius)
181 38 : return
182 : }
183 :
184 : const gradient = createBrushGradient(
185 : ctx,
186 : x,
187 : y,
188 : brushRadius,
189 : hardness,
190 : rgbColor,
191 : opacity,
192 : false
193 : )
194 0 : ctx.fillStyle = gradient
195 0 : drawShapeOnContext(ctx, brushType, x, y, brushRadius)
196 : }
197 :
198 658 : export function drawMaskShape(
199 : ctx: CanvasRenderingContext2D,
200 : point: Point,
201 : brushType: BrushShape,
202 : brushRadius: number,
203 : hardness: number,
204 : opacity: number,
205 : isErasing: boolean,
206 : maskColor: MaskColor
207 : ): void {
208 658 : const { x, y } = point
209 :
210 658 : if (brushType === BrushShape.Rect && hardness < 1) {
211 : const baseColor = isErasing
212 : ? `rgba(255, 255, 255, ${opacity})`
213 : : `rgba(${maskColor.r}, ${maskColor.g}, ${maskColor.b}, ${opacity})`
214 0 : const brushTexture = getCachedBrushTexture(
215 : brushRadius,
216 : hardness,
217 : baseColor,
218 : opacity
219 : )
220 0 : ctx.drawImage(brushTexture, x - brushRadius, y - brushRadius)
221 0 : return
222 : }
223 :
224 658 : if (hardness === 1) {
225 658 : ctx.fillStyle = isErasing
226 : ? `rgba(255, 255, 255, ${opacity})`
227 : : `rgba(${maskColor.r}, ${maskColor.g}, ${maskColor.b}, ${opacity})`
228 658 : drawShapeOnContext(ctx, brushType, x, y, brushRadius)
229 658 : return
230 : }
231 :
232 : const maskColorHex = `rgb(${maskColor.r}, ${maskColor.g}, ${maskColor.b})`
233 : const gradient = createBrushGradient(
234 : ctx,
235 : x,
236 : y,
237 : brushRadius,
238 : hardness,
239 : maskColorHex,
240 : opacity,
241 : isErasing
242 : )
243 0 : ctx.fillStyle = gradient
244 0 : drawShapeOnContext(ctx, brushType, x, y, brushRadius)
245 : }
|