Line data Source code
1 : import type {
2 : Point2D,
3 : Point2D as Point,
4 : Extents as DirtyRect
5 : } from '@comfyorg/shared-frontend-utils/geometry'
6 :
7 : /**
8 : * Computes the clamped dirty-rect coordinates for a putImageData call.
9 : *
10 : * Returns the full canvas dimensions when the dirty rect is uninitialised
11 : * (Infinity sentinels) or the resulting area has zero/negative size.
12 : */
13 0 : export function clampDirtyRect(
14 : rect: DirtyRect,
15 : canvasWidth: number,
16 : canvasHeight: number
17 : ): { dx: number; dy: number; dw: number; dh: number } {
18 0 : const full = { dx: 0, dy: 0, dw: canvasWidth, dh: canvasHeight }
19 0 : if (rect.minX === Infinity || rect.maxX === -Infinity) return full
20 :
21 0 : const dx = Math.floor(Math.max(0, rect.minX))
22 0 : const dy = Math.floor(Math.max(0, rect.minY))
23 0 : const dw = Math.ceil(Math.min(canvasWidth, rect.maxX)) - dx
24 0 : const dh = Math.ceil(Math.min(canvasHeight, rect.maxY)) - dy
25 :
26 0 : return dw > 0 && dh > 0 ? { dx, dy, dw, dh } : full
27 : }
28 :
29 0 : export function buildStrokePoints(
30 : points: Point[],
31 : skipResampling: boolean,
32 : stepSize: number
33 : ): (Point2D & { pressure: number })[] {
34 0 : if (skipResampling) {
35 0 : return points.map((p) => ({ x: p.x, y: p.y, pressure: 1.0 }))
36 : }
37 0 : const result: (Point2D & { pressure: number })[] = []
38 0 : for (let i = 0; i < points.length - 1; i++) {
39 0 : const p1 = points[i]
40 0 : const p2 = points[i + 1]
41 0 : const steps = Math.max(
42 : 1,
43 : Math.ceil(Math.hypot(p2.x - p1.x, p2.y - p1.y) / stepSize)
44 : )
45 0 : for (let s = 0; s <= steps; s++) {
46 0 : const t = s / steps
47 0 : result.push({
48 : x: p1.x + (p2.x - p1.x) * t,
49 : y: p1.y + (p2.y - p1.y) * t,
50 : pressure: 1.0
51 : })
52 : }
53 : }
54 0 : return result
55 : }
|