-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathspline_mask_editor.js
More file actions
1269 lines (1181 loc) · 53.1 KB
/
Copy pathspline_mask_editor.js
File metadata and controls
1269 lines (1181 loc) · 53.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* SplineMaskEditorMEC – interactive spline drawing widget.
*
* Clean rewrite (May 2026):
* - HTML toolbar (no canvas-painted buttons).
* - Multiple paths supported (one active at a time).
* - spline_data JSON contract preserved:
* [ {points:[{x,y}], type, closed, handles?}, ... ]
*
* Interaction:
* Left click add control point to active path
* Drag a point move it
* Shift+click point delete it
* Right-click point delete it
* Alt / Middle drag pan
* Wheel zoom (cursor-anchored)
* N new path
* C toggle closed
* Delete clear active path
* Ctrl+Z / Ctrl+Y undo / redo
* F fit view
*/
import { app } from "../../scripts/app.js";
import { installModeGated } from "./_mode_gate.js";
import { C, bg3, border, peach } from "./_c2c_theme.js";
import { reportFailure as __c2cReport } from "./_c2c_report.js";
// Targets both the unified SplineMaskMEC (mode=edit) node and any
// legacy SplineMaskEditorMEC references that may still live on saved
// graphs. The unified node is gated on mode === "edit".
const NODE_NAMES = ["SplineMaskMEC", "SplineMaskEditorMEC"];
const NODE_NAME = "SplineMaskMEC";
const COLOR = {
bg: "var(--c2c-bg2)",
border: "var(--c2c-border)",
text: "var(--c2c-fg)",
sub: "var(--c2c-overlay1)",
paths: ["var(--c2c-green)", "var(--c2c-blue)", "var(--c2c-yellow)", "var(--c2c-peach)", "var(--c2c-pink)", "var(--c2c-teal)"],
};
const HISTORY_LIMIT = 80;
const POINT_HIT_PX = 8;
// Module-level decoded-image cache keyed by URL.
// Fixes "slow image load": when the user pans the graph or re-opens the
// node, we re-use the already-decoded HTMLImageElement instead of issuing
// a fresh fetch + decode every time the URL is re-set.
const IMG_CACHE = new Map(); // url -> {img, w, h}
const IMG_CACHE_MAX = 32;
function _cacheGet(url) { return IMG_CACHE.get(url) || null; }
function _cachePut(url, entry) {
IMG_CACHE.set(url, entry);
if (IMG_CACHE.size > IMG_CACHE_MAX) {
const firstKey = IMG_CACHE.keys().next().value;
if (firstKey !== undefined) IMG_CACHE.delete(firstKey);
}
}
class Editor {
constructor(node) {
this.node = node;
this.shapes = []; // {points:[{x,y}], type, closed, handles?}
this.active = -1;
this.canvasW = 512;
this.canvasH = 512;
this.zoom = 1;
this.panX = 0;
this.panY = 0;
this._fitted = false;
this.refImg = null;
this.refUrl = null;
this.hover = { shape: -1, point: -1 };
this.drag = null;
this.undoStack = [];
this.redoStack = [];
this.cursor = { x: 0, y: 0, visible: false };
this.splineType = "catmull_rom";
this.closedDefault = true;
this.samplesPerSegment = 20;
this.centripetalAlpha = 0.5;
}
snapshot() { return JSON.stringify({ s: this.shapes, a: this.active }); }
pushUndo() {
this.undoStack.push(this.snapshot());
if (this.undoStack.length > HISTORY_LIMIT) this.undoStack.shift();
this.redoStack.length = 0;
}
restore(s) {
// Defensive: corrupted snapshot would crash entire canvas.
let o;
try { o = JSON.parse(s); }
catch (e) { console.warn("[MEC.SplineMaskEditor] restore: malformed snapshot, ignored", e); return; }
if (!o) return;
this.shapes = Array.isArray(o.s) ? o.s : [];
this.active = Number.isFinite(o.a) ? o.a : -1;
}
undo() {
if (!this.undoStack.length) return false;
this.redoStack.push(this.snapshot());
this.restore(this.undoStack.pop()); return true;
}
redo() {
if (!this.redoStack.length) return false;
this.undoStack.push(this.snapshot());
this.restore(this.redoStack.pop()); return true;
}
viewToCanvas(vx, vy) {
return { x: (vx - this.panX) / this.zoom, y: (vy - this.panY) / this.zoom };
}
minZoom(vw, vh) {
if (vw <= 0 || vh <= 0) return 0.05;
return Math.min(vw / this.canvasW, vh / this.canvasH);
}
clampPan(vw, vh) {
const dW = this.canvasW * this.zoom;
const dH = this.canvasH * this.zoom;
if (dW <= vw) this.panX = (vw - dW) / 2;
else this.panX = Math.min(0, Math.max(vw - dW, this.panX));
if (dH <= vh) this.panY = (vh - dH) / 2;
else this.panY = Math.min(0, Math.max(vh - dH, this.panY));
}
setZoomAround(newZ, ax, ay, vw, vh) {
const minZ = this.minZoom(vw, vh);
newZ = Math.max(minZ, Math.min(8, newZ));
const k = newZ / this.zoom;
this.panX = ax - (ax - this.panX) * k;
this.panY = ay - (ay - this.panY) * k;
this.zoom = newZ;
this.clampPan(vw, vh);
}
fitView(vw, vh) {
if (vw <= 0 || vh <= 0) return;
const pad = 8;
const sx = (vw - pad * 2) / this.canvasW;
const sy = (vh - pad * 2) / this.canvasH;
this.zoom = Math.max(0.05, Math.min(sx, sy, 8));
this.panX = (vw - this.canvasW * this.zoom) / 2;
this.panY = (vh - this.canvasH * this.zoom) / 2;
this._fitted = true;
}
newPath() {
const closed = this.closedDefault;
this.shapes.push({ points: [], type: this.splineType, closed });
this.active = this.shapes.length - 1;
}
findPoint(cx, cy) {
const r = POINT_HIT_PX / this.zoom;
const r2 = r * r;
for (let s = this.shapes.length - 1; s >= 0; s--) {
const sh = this.shapes[s];
for (let i = sh.points.length - 1; i >= 0; i--) {
const p = sh.points[i];
const d = (p.x - cx) ** 2 + (p.y - cy) ** 2;
if (d < r2) return { shape: s, point: i };
}
}
return { shape: -1, point: -1 };
}
// Hit-test Bezier handle endpoints on the active shape only.
// Returns {shape, point, side:"in"|"out"} or null.
findHandle(cx, cy) {
if (this.active < 0) return null;
const sh = this.shapes[this.active];
if (!sh || sh.type !== "bezier" || !Array.isArray(sh.handles)) return null;
const r = POINT_HIT_PX / this.zoom;
const r2 = r * r;
for (let i = 0; i < sh.points.length; i++) {
const h = sh.handles[i];
if (!h) continue;
const p = sh.points[i];
for (const side of ["in", "out"]) {
const hv = h[side];
if (!hv) continue;
const hx = p.x + hv.x, hy = p.y + hv.y;
const d = (hx - cx) ** 2 + (hy - cy) ** 2;
if (d < r2) return { shape: this.active, point: i, side };
}
}
return null;
}
// Ensure shape.handles is sized to match shape.points; create symmetric
// tangents from neighbours when missing. Idempotent.
ensureHandles(sh) {
if (sh.type !== "bezier") return;
if (!Array.isArray(sh.handles)) sh.handles = [];
const n = sh.points.length;
for (let i = 0; i < n; i++) {
if (sh.handles[i] && sh.handles[i].in && sh.handles[i].out) continue;
const p = sh.points[i];
const prev = sh.points[i - 1] || (sh.closed ? sh.points[n - 1] : null);
const next = sh.points[i + 1] || (sh.closed ? sh.points[0] : null);
let tx = 0, ty = 0;
if (prev && next) { tx = (next.x - prev.x) * 0.25; ty = (next.y - prev.y) * 0.25; }
else if (prev) { tx = (p.x - prev.x) * 0.33; ty = (p.y - prev.y) * 0.33; }
else if (next) { tx = (next.x - p.x) * 0.33; ty = (next.y - p.y) * 0.33; }
else { tx = 40; ty = 0; }
sh.handles[i] = { in: { x: -tx, y: -ty }, out: { x: tx, y: ty } };
}
sh.handles.length = n;
}
save() {
const w = this.node.widgets?.find(w => w.name === "spline_data");
if (!w) return;
const data = this.shapes.map(sh => ({
points: sh.points.map(p => ({ x: +p.x.toFixed(2), y: +p.y.toFixed(2) })),
type: sh.type || this.splineType,
closed: !!sh.closed,
...(sh.handles ? { handles: sh.handles } : {}),
}));
w.value = JSON.stringify(data);
if (this.node.graph) this.node.graph.setDirtyCanvas(true, false);
}
load() {
const w = this.node.widgets?.find(w => w.name === "spline_data");
if (!w?.value) return;
try {
const o = JSON.parse(w.value);
if (Array.isArray(o)) {
this.shapes = o.map(sh => ({
points: (sh.points || []).map(p => ({ x: +p.x, y: +p.y })),
type: sh.type || this.splineType,
closed: !!sh.closed,
...(sh.handles ? { handles: sh.handles } : {}),
}));
this.active = this.shapes.length ? 0 : -1;
for (const sh of this.shapes) if (sh.type === "bezier") this.ensureHandles(sh);
}
} catch (__c2cErr) { __c2cReport("spline_mask_editor", __c2cErr); }
}
setRefImage(url, ow, oh) {
if (url === this.refUrl && this.refImg && this.refImg.complete) return;
const sameUrl = (url === this.refUrl);
this.refUrl = url;
// Cache hit: zero-cost re-use; do NOT reset zoom/pan when the URL
// didn't actually change (was the source of the bounce bug — every
// re-render set refImg again, clearing _fitted, snapping the view).
const cached = _cacheGet(url);
if (cached?.img?.complete) {
this.refImg = cached.img;
const w = ow || cached.w;
const h = oh || cached.h;
if (w > 0 && h > 0 && (this.canvasW !== w || this.canvasH !== h)) {
this.canvasW = w; this.canvasH = h;
const wW = this.node.widgets?.find(x => x.name === "width");
const hW = this.node.widgets?.find(x => x.name === "height");
if (wW && +wW.value !== w) wW.value = w;
if (hW && +hW.value !== h) hW.value = h;
this._fitted = false; // dims changed — a fresh fit is correct
}
if (!sameUrl) this._fitted = false;
this.onLoaded?.();
return;
}
const img = new Image();
img.crossOrigin = "anonymous";
img.onload = () => {
this.refImg = img;
const w = ow || img.naturalWidth;
const h = oh || img.naturalHeight;
if (w > 0 && h > 0) {
this.canvasW = w; this.canvasH = h;
const wW = this.node.widgets?.find(x => x.name === "width");
const hW = this.node.widgets?.find(x => x.name === "height");
if (wW && +wW.value !== w) wW.value = w;
if (hW && +hW.value !== h) hW.value = h;
}
_cachePut(url, { img, w, h });
this._fitted = false;
this.onLoaded?.();
};
img.src = url;
}
}
function catmullRom(points, samplesPerSeg, closed, alpha) {
const n = points.length;
if (n < 2) return points.slice();
if (n === 2) {
const [a, b] = points;
const out = [];
for (let i = 0; i <= samplesPerSeg; i++) {
const t = i / samplesPerSeg;
out.push({ x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t });
}
return out;
}
const ext = closed
? [points[n - 1], ...points, points[0], points[1]]
: [points[0], ...points, points[n - 1]];
const out = [];
const segs = closed ? n : n - 1;
// Centripetal Catmull-Rom — alpha is now configurable (0=uniform,
// 0.5=centripetal, 1=chordal). 0.5 is the default and avoids cusps.
const a = (typeof alpha === "number") ? Math.max(0, Math.min(1, alpha)) : 0.5;
for (let i = 0; i < segs; i++) {
const p0 = ext[i], p1 = ext[i + 1], p2 = ext[i + 2], p3 = ext[i + 3];
const t01 = Math.pow(Math.hypot(p1.x - p0.x, p1.y - p0.y), a) || 1e-6;
const t12 = Math.pow(Math.hypot(p2.x - p1.x, p2.y - p1.y), a) || 1e-6;
const t23 = Math.pow(Math.hypot(p3.x - p2.x, p3.y - p2.y), a) || 1e-6;
const m1x = (p2.x - p0.x) - (p1.x - p0.x) * t12 / (t01 + t12) + (p2.x - p1.x) * t01 / (t01 + t12);
const m1y = (p2.y - p0.y) - (p1.y - p0.y) * t12 / (t01 + t12) + (p2.y - p1.y) * t01 / (t01 + t12);
const m2x = (p3.x - p1.x) - (p2.x - p1.x) * t23 / (t12 + t23) + (p3.x - p2.x) * t12 / (t12 + t23);
const m2y = (p3.y - p1.y) - (p2.y - p1.y) * t23 / (t12 + t23) + (p3.y - p2.y) * t12 / (t12 + t23);
const last = i === segs - 1;
const steps = samplesPerSeg + (last ? 1 : 0);
for (let s = 0; s < steps; s++) {
const t = s / samplesPerSeg;
const t2 = t * t, t3 = t2 * t;
const h00 = 2 * t3 - 3 * t2 + 1;
const h10 = t3 - 2 * t2 + t;
const h01 = -2 * t3 + 3 * t2;
const h11 = t3 - t2;
out.push({
x: h00 * p1.x + h10 * m1x * 0.5 + h01 * p2.x + h11 * m2x * 0.5,
y: h00 * p1.y + h10 * m1y * 0.5 + h01 * p2.y + h11 * m2y * 0.5,
});
}
}
return out;
}
function bezierCubic(points, samplesPerSeg, closed, handles) {
const n = points.length;
if (n < 2 || !Array.isArray(handles) || handles.length < n) return points.slice();
const out = [];
const segs = closed ? n : n - 1;
for (let i = 0; i < segs; i++) {
const p1 = points[i];
const p2 = points[(i + 1) % n];
const h1 = handles[i];
const h2 = handles[(i + 1) % n];
if (!h1 || !h2) continue;
const c1x = p1.x + (h1.out?.x ?? 0), c1y = p1.y + (h1.out?.y ?? 0);
const c2x = p2.x + (h2.in?.x ?? 0), c2y = p2.y + (h2.in?.y ?? 0);
const last = i === segs - 1;
const steps = samplesPerSeg + (last ? 1 : 0);
for (let s = 0; s < steps; s++) {
const t = s / samplesPerSeg;
const u = 1 - t;
const b0 = u * u * u, b1 = 3 * u * u * t, b2 = 3 * u * t * t, b3 = t * t * t;
out.push({
x: b0 * p1.x + b1 * c1x + b2 * c2x + b3 * p2.x,
y: b0 * p1.y + b1 * c1y + b2 * c2y + b3 * p2.y,
});
}
}
return out;
}
// ── new accurate curve families (mirror nodes/_spline_curves.py exactly so
// the on-canvas preview matches the rasterized mask) ──────────────────
const BSPLINE_M = [[-1, 3, -3, 1], [3, -6, 3, 0], [-3, 0, 3, 0], [1, 4, 1, 0]]
.map((r) => r.map((v) => v / 6));
function bsplineLike(points, spb, closed, weights) {
const n = points.length;
if (n < 3) return points.slice();
const W = (Array.isArray(weights) && weights.length === n)
? weights.map((w) => Math.max(1e-6, +w || 1)) : points.map(() => 1);
let idx, segs;
if (closed) { idx = [n - 1]; for (let i = 0; i < n; i++) idx.push(i); idx.push(0, 1); segs = n; }
else { idx = [0, 0]; for (let i = 0; i < n; i++) idx.push(i); idx.push(n - 1, n - 1); segs = idx.length - 3; }
const out = [], lastSeg = segs - 1;
for (let s = 0; s < segs; s++) {
const c = [idx[s], idx[s + 1], idx[s + 2], idx[s + 3]];
const steps = spb + ((!closed && s === lastSeg) ? 1 : 0);
for (let k = 0; k < steps; k++) {
const t = k / spb, tv = [t * t * t, t * t, t, 1];
const basis = [0, 0, 0, 0];
for (let a = 0; a < 4; a++) { let su = 0; for (let b = 0; b < 4; b++) su += tv[b] * BSPLINE_M[b][a]; basis[a] = su; }
let nx = 0, ny = 0, den = 0;
for (let a = 0; a < 4; a++) { const P = points[c[a]], wb = basis[a] * W[c[a]]; nx += wb * P.x; ny += wb * P.y; den += wb; }
out.push({ x: nx / den, y: ny / den });
}
}
return out;
}
function solveLinear(A, b) { // small dense Gaussian elimination (n<=~64)
const n = b.length, M = A.map((r, i) => r.concat(b[i]));
for (let col = 0; col < n; col++) {
let piv = col; for (let r = col + 1; r < n; r++) if (Math.abs(M[r][col]) > Math.abs(M[piv][col])) piv = r;
[M[col], M[piv]] = [M[piv], M[col]];
const d = M[col][col] || 1e-12;
for (let r = 0; r < n; r++) { if (r === col) continue; const f = M[r][col] / d; for (let cc = col; cc <= n; cc++) M[r][cc] -= f * M[col][cc]; }
}
return M.map((r, i) => r[n] / (r[i] || 1e-12));
}
function naturalCubic1d(y, closed) {
const n = y.length, k = new Array(n).fill(0);
if (closed) {
const A = Array.from({ length: n }, () => new Array(n).fill(0)), rhs = new Array(n).fill(0);
for (let i = 0; i < n; i++) {
A[i][(i - 1 + n) % n] = 1; A[i][i] = 4; A[i][(i + 1) % n] = 1;
rhs[i] = 6 * (y[(i + 1) % n] - 2 * y[i] + y[(i - 1 + n) % n]);
}
return solveLinear(A, rhs);
}
if (n >= 3) {
const m = n - 2, A = Array.from({ length: m }, () => new Array(m).fill(0)), rhs = new Array(m).fill(0);
for (let i = 0; i < m; i++) { A[i][i] = 4; if (i > 0) A[i][i - 1] = 1; if (i < m - 1) A[i][i + 1] = 1; rhs[i] = 6 * (y[i + 2] - 2 * y[i + 1] + y[i]); }
const sol = solveLinear(A, rhs); for (let i = 0; i < m; i++) k[i + 1] = sol[i];
}
return k;
}
function naturalCubic(points, spb, closed) {
const n = points.length;
if (n < 3) return points.slice();
const kx = naturalCubic1d(points.map((p) => p.x), closed);
const ky = naturalCubic1d(points.map((p) => p.y), closed);
const segs = closed ? n : n - 1, out = [], lastSeg = segs - 1;
for (let i = 0; i < segs; i++) {
const j = (i + 1) % n, steps = spb + ((!closed && i === lastSeg) ? 1 : 0);
for (let s = 0; s < steps; s++) {
const t = s / spb, u = 1 - t;
const cx = u * points[i].x + t * points[j].x + ((u ** 3 - u) * kx[i] + (t ** 3 - t) * kx[j]) / 6;
const cy = u * points[i].y + t * points[j].y + ((u ** 3 - u) * ky[i] + (t ** 3 - t) * ky[j]) / 6;
out.push({ x: cx, y: cy });
}
}
return out;
}
function cardinalSpline(points, spb, closed, tension) {
const n = points.length;
if (n < 3) return points.slice();
const c = 1 - Math.max(0, Math.min(1, +tension || 0));
let ext;
if (closed) ext = [points[n - 1], ...points, points[0], points[1]];
else ext = [{ x: 2 * points[0].x - points[1].x, y: 2 * points[0].y - points[1].y }, ...points,
{ x: 2 * points[n - 1].x - points[n - 2].x, y: 2 * points[n - 1].y - points[n - 2].y }];
const segs = closed ? n : n - 1, out = [], lastSeg = segs - 1;
for (let i = 0; i < segs; i++) {
const p0 = ext[i], p1 = ext[i + 1], p2 = ext[i + 2], p3 = ext[i + 3];
const m1x = c * (p2.x - p0.x) * 0.5, m1y = c * (p2.y - p0.y) * 0.5;
const m2x = c * (p3.x - p1.x) * 0.5, m2y = c * (p3.y - p1.y) * 0.5;
const steps = spb + ((!closed && i === lastSeg) ? 1 : 0);
for (let s = 0; s < steps; s++) {
const t = s / spb, t2 = t * t, t3 = t2 * t;
const h00 = 2 * t3 - 3 * t2 + 1, h10 = t3 - 2 * t2 + t, h01 = -2 * t3 + 3 * t2, h11 = t3 - t2;
out.push({ x: h00 * p1.x + h10 * m1x + h01 * p2.x + h11 * m2x,
y: h00 * p1.y + h10 * m1y + h01 * p2.y + h11 * m2y });
}
}
return out;
}
function sampleFamily(pts, type, spb, closed, weights, tension) {
if (type === "b_spline") return bsplineLike(pts, spb, closed);
if (type === "nurbs") return bsplineLike(pts, spb, closed, weights);
if (type === "natural_cubic") return naturalCubic(pts, spb, closed);
if (type === "cardinal") return cardinalSpline(pts, spb, closed, tension);
return catmullRom(pts, spb, closed, 0.5);
}
function sampleWithCusps(sh, type, spb) {
const pts = sh.points, n = pts.length;
if (n < 3) return pts.slice();
const cusps = sh.cusps;
if (!Array.isArray(cusps) || !cusps.some(Boolean))
return sampleFamily(pts, type, spb, sh.closed, sh.weights, sh.tension);
const corner = pts.map((_, i) => !!cusps[i]);
const order = pts.map((_, i) => i).concat(sh.closed ? [0] : []);
const runs = []; let cur = [];
for (const i of order) { cur.push(i); if (corner[i] && cur.length > 1) { runs.push(cur); cur = [i]; } }
if (cur.length > 1) runs.push(cur);
const out = [];
for (const run of runs) {
const rp = run.map((i) => pts[i]);
const rw = sh.weights ? run.map((i) => sh.weights[i] ?? 1) : null;
let seg = sampleFamily(rp, type, spb, false, rw, sh.tension);
if (out.length && seg.length && Math.abs(out[out.length - 1].x - seg[0].x) < 1e-6
&& Math.abs(out[out.length - 1].y - seg[0].y) < 1e-6) seg = seg.slice(1);
out.push(...seg);
}
return out;
}
// Parametric primitives (mirror _spline_curves.make_primitive) for preview.
const PRIMITIVE_TYPES = ["circle", "ellipse", "rectangle", "rounded_rect", "polygon", "star", "arc"];
function makePrimitive(type, pts, samples, params) {
params = params || {};
if (pts.length < 2) return pts.slice();
const xs = pts.map((p) => p.x), ys = pts.map((p) => p.y);
const x0 = Math.min(...xs), y0 = Math.min(...ys), x1 = Math.max(...xs), y1 = Math.max(...ys);
const cx = (x0 + x1) / 2, cy = (y0 + y1) / 2;
const rx = Math.max(1e-6, (x1 - x0) / 2), ry = Math.max(1e-6, (y1 - y0) / 2);
const rot = +params.rotation || 0, cr = Math.cos(rot), sr = Math.sin(rot);
const place = (dx, dy) => ({ x: cx + dx * cr - dy * sr, y: cy + dx * sr + dy * cr });
const n = Math.max(3, samples | 0), TAU = Math.PI * 2;
if (type === "circle" || type === "ellipse") {
const r = type === "circle" ? Math.min(rx, ry) : null;
return Array.from({ length: n }, (_, i) => place((r || rx) * Math.cos(TAU * i / n), (r || ry) * Math.sin(TAU * i / n)));
}
if (type === "rectangle") return [place(-rx, -ry), place(rx, -ry), place(rx, ry), place(-rx, ry)];
if (type === "rounded_rect") {
let rad = params.corner_radius != null ? +params.corner_radius : Math.min(rx, ry) * 0.25;
rad = Math.max(0, Math.min(rad, Math.min(rx, ry)));
const per = Math.max(2, n >> 2), out = [];
[[rx - rad, ry - rad, 0], [-rx + rad, ry - rad, Math.PI / 2], [-rx + rad, -ry + rad, Math.PI], [rx - rad, -ry + rad, 1.5 * Math.PI]]
.forEach(([ccx, ccy, a0]) => { for (let k = 0; k <= per; k++) { const a = a0 + (Math.PI / 2) * (k / per); out.push(place(ccx + rad * Math.cos(a), ccy + rad * Math.sin(a))); } });
return out;
}
if (type === "polygon") {
const sides = Math.max(3, params.sides | 0 || 6), a0 = params.start_angle != null ? +params.start_angle : -Math.PI / 2;
return Array.from({ length: sides }, (_, i) => place(rx * Math.cos(a0 + TAU * i / sides), ry * Math.sin(a0 + TAU * i / sides)));
}
if (type === "star") {
const sides = Math.max(2, params.sides | 0 || 5), inner = Math.max(0.05, Math.min(1, params.inner_ratio != null ? +params.inner_ratio : 0.5));
const a0 = params.start_angle != null ? +params.start_angle : -Math.PI / 2, out = [];
for (let i = 0; i < sides * 2; i++) { const rr = i % 2 === 0 ? 1 : inner, a = a0 + Math.PI * i / sides; out.push(place(rx * rr * Math.cos(a), ry * rr * Math.sin(a))); }
return out;
}
if (type === "arc") {
const a0 = +params.start_angle || 0, a1 = params.end_angle != null ? +params.end_angle : Math.PI, r = Math.min(rx, ry);
return Array.from({ length: n }, (_, i) => place(r * Math.cos(a0 + (a1 - a0) * i / (n - 1)), r * Math.sin(a0 + (a1 - a0) * i / (n - 1))));
}
return pts.slice();
}
function sampleShape(sh, samplesPerSeg, alpha) {
const pts = sh.points;
if (pts.length < 2) return pts.slice();
if (PRIMITIVE_TYPES.includes(sh.type))
return makePrimitive(sh.type, pts, Math.max(3, samplesPerSeg * 3), sh.params);
if (sh.type === "polyline") return pts.slice();
if (sh.type === "bezier") return bezierCubic(pts, samplesPerSeg, sh.closed, sh.handles);
if (sh.type === "b_spline" || sh.type === "nurbs" || sh.type === "natural_cubic" || sh.type === "cardinal")
return sampleWithCusps(sh, sh.type, samplesPerSeg);
return catmullRom(pts, samplesPerSeg, sh.closed, alpha);
}
function draw(ed, ctx, vw, vh) {
ctx.save();
ctx.fillStyle = COLOR.bg;
ctx.fillRect(0, 0, vw, vh);
const z = ed.zoom;
ctx.translate(ed.panX, ed.panY);
ctx.scale(z, z);
if (ed.refImg && ed.refImg.complete) {
ctx.imageSmoothingEnabled = true;
ctx.imageSmoothingQuality = "high";
try { ctx.drawImage(ed.refImg, 0, 0, ed.canvasW, ed.canvasH); } catch (__c2cErr) { __c2cReport("spline_mask_editor", __c2cErr); }
} else {
ctx.fillStyle = C.bg3;
ctx.fillRect(0, 0, ed.canvasW, ed.canvasH);
}
ctx.strokeStyle = C.surface1;
ctx.lineWidth = 1 / z;
ctx.strokeRect(0, 0, ed.canvasW, ed.canvasH);
for (let s = 0; s < ed.shapes.length; s++) {
const sh = ed.shapes[s];
const c = COLOR.paths[s % COLOR.paths.length];
const isActive = s === ed.active;
if (sh.points.length >= 2) {
const sampled = sampleShape(sh, ed.samplesPerSegment, ed.centripetalAlpha);
ctx.strokeStyle = c;
ctx.lineWidth = (isActive ? 2.0 : 1.5) / z;
ctx.fillStyle = c + "22";
ctx.beginPath();
ctx.moveTo(sampled[0].x, sampled[0].y);
for (let i = 1; i < sampled.length; i++) ctx.lineTo(sampled[i].x, sampled[i].y);
if (sh.closed) {
ctx.closePath();
ctx.fill();
}
ctx.stroke();
}
for (let i = 0; i < sh.points.length; i++) {
const p = sh.points[i];
const isHov = ed.hover.shape === s && ed.hover.point === i;
const r = (isHov ? 6 : 4) / z;
ctx.fillStyle = c;
ctx.beginPath(); ctx.arc(p.x, p.y, r, 0, Math.PI * 2); ctx.fill();
ctx.strokeStyle = C.black;
ctx.lineWidth = 1.5 / z;
ctx.stroke();
if (isActive && i === 0) {
ctx.strokeStyle = C.white;
ctx.lineWidth = 1 / z;
ctx.beginPath(); ctx.arc(p.x, p.y, r + 2 / z, 0, Math.PI * 2); ctx.stroke();
}
}
// Bezier handle endpoints + connector lines (active shape only).
if (isActive && sh.type === "bezier" && Array.isArray(sh.handles)) {
for (let i = 0; i < sh.points.length; i++) {
const p = sh.points[i];
const h = sh.handles[i];
if (!h) continue;
for (const side of ["in", "out"]) {
const hv = h[side];
if (!hv) continue;
const hx = p.x + hv.x, hy = p.y + hv.y;
// Canvas can't parse var(); use the theme-resolved color.
// (was "var(--c2c-peach)aa" → silently rendered as default/black.)
ctx.save();
ctx.globalAlpha = 0.67; // the intended ~"aa" alpha
ctx.strokeStyle = C.peach;
ctx.lineWidth = 1 / z;
ctx.beginPath();
ctx.moveTo(p.x, p.y);
ctx.lineTo(hx, hy);
ctx.stroke();
ctx.restore();
ctx.fillStyle = C.peach;
ctx.beginPath();
ctx.arc(hx, hy, 3.5 / z, 0, Math.PI * 2);
ctx.fill();
ctx.strokeStyle = C.black;
ctx.lineWidth = 1 / z;
ctx.stroke();
}
}
}
}
ctx.restore();
}
function findRefImage(node) {
if (!node.inputs) return null;
const linkedInput = node.inputs.find(i => i.name === "image" && i.link != null);
if (!linkedInput) return null;
// Resolve links within the node's owning graph (subgraph-safe).
const ownerGraph = node.graph || app.graph;
const link = ownerGraph?.links?.[linkedInput.link];
if (!link) return null;
const visited = new Set();
const queue = [{ id: link.origin_id, depth: 0 }];
while (queue.length) {
const { id, depth } = queue.shift();
if (visited.has(id)) continue;
visited.add(id);
const n = ownerGraph.getNodeById?.(id);
if (!n) continue;
if (n.imgs?.length) return { url: n.imgs[0].src };
const w = n.widgets?.find(w => w.name === "image");
if (w?.value && typeof w.value === "string") {
const parts = w.value.split("/");
const sub = parts.length > 1 ? parts.slice(0, -1).join("/") : "";
const fn = parts[parts.length - 1];
return { url: `/view?filename=${encodeURIComponent(fn)}&subfolder=${encodeURIComponent(sub)}&type=input` };
}
if (depth < 3 && n.inputs) {
for (const inp of n.inputs) {
if (inp.link == null) continue;
const li = ownerGraph.links?.[inp.link];
if (li) queue.push({ id: li.origin_id, depth: depth + 1 });
}
}
}
return null;
}
function installEditor(node) {
if (node._mecSplineEditHost) return;
const ed = new Editor(node);
const hideWidget = (w) => {
if (!w) return;
w.type = "hidden";
w.computeSize = () => [0, -4];
w.draw = () => {};
const el = w.element;
if (el) {
el.hidden = true;
el.style.display = "none";
const wrap = el.parentElement;
if (wrap?.classList?.contains("dom-widget")) wrap.style.display = "none";
}
};
hideWidget(node.widgets?.find(w => w.name === "spline_data"));
const syncFromWidgets = () => {
const t = node.widgets?.find(x => x.name === "spline_type");
const c = node.widgets?.find(x => x.name === "closed");
const sps = node.widgets?.find(x => x.name === "samples_per_segment");
const w = node.widgets?.find(x => x.name === "width");
const h = node.widgets?.find(x => x.name === "height");
const ca = node.widgets?.find(x => x.name === "centripetal_alpha");
if (t) ed.splineType = t.value;
if (c) ed.closedDefault = !!c.value;
if (sps) ed.samplesPerSegment = +sps.value || 20;
if (w && +w.value > 0) ed.canvasW = +w.value;
if (h && +h.value > 0) ed.canvasH = +h.value;
if (ca && typeof ca.value === "number") ed.centripetalAlpha = ca.value;
};
syncFromWidgets();
ed.load();
const root = document.createElement("div");
// Inset from node body — leaves a hit area for LiteGraph border / resize
// corner so the user can grab edges and the bottom-right grip.
root.style.cssText = `
display:flex;flex-direction:column;
width:calc(100% - 12px);height:calc(100% - 18px);
margin:2px 6px 16px 6px;
background:${COLOR.bg};border:1px solid ${COLOR.border};border-radius:6px;
overflow:hidden;font-family:Inter,system-ui,sans-serif;color:${COLOR.text};
box-sizing:border-box;user-select:none;
pointer-events:none;
`;
const tb = document.createElement("div");
tb.style.cssText = `
display:flex;align-items:center;gap:4px;padding:4px 6px;
background:linear-gradient(var(--c2c-panelTint),var(--c2c-panelHi));
border-bottom:1px solid ${COLOR.border};
flex:0 0 auto;font-size:11px;line-height:1;
pointer-events:auto;
`;
root.appendChild(tb);
const canvasWrap = document.createElement("div");
canvasWrap.style.cssText = "position:relative;flex:1 1 auto;min-height:0;overflow:hidden;cursor:crosshair;background:var(--c2c-bg3);pointer-events:auto;";
root.appendChild(canvasWrap);
const canvas = document.createElement("canvas");
canvas.style.cssText = "display:block;width:100%;height:100%;outline:none;";
canvas.tabIndex = 0;
canvasWrap.appendChild(canvas);
const status = document.createElement("div");
status.style.cssText = `
position:absolute;left:6px;bottom:6px;padding:3px 7px;
background:var(--c2c-bg)d8;border:1px solid ${COLOR.border};border-radius:4px;
font-size:10px;color:${COLOR.sub};pointer-events:none;
font-family:ui-monospace,Menlo,monospace;letter-spacing:.2px;
`;
canvasWrap.appendChild(status);
const ctx = canvas.getContext("2d");
const mkIcon = (label, title, onClick) => {
const b = document.createElement("button");
b.textContent = label; b.title = title;
b.style.cssText = `
min-width:26px;height:24px;padding:0 7px;
border:1px solid ${COLOR.border};border-radius:4px;
background:var(--c2c-border);color:${COLOR.text};
font-size:11px;font-weight:500;cursor:pointer;
display:inline-flex;align-items:center;justify-content:center;
white-space:nowrap;line-height:1;flex:0 0 auto;
transition:background .12s,border-color .12s;
`;
b.onmouseenter = () => { b.style.background = "var(--c2c-surface1)"; b.style.borderColor = "var(--c2c-surface2)"; };
b.onmouseleave = () => { b.style.background = C.border; b.style.borderColor = COLOR.border; };
b.onmousedown = (e) => e.stopPropagation();
b.onclick = (e) => { e.preventDefault(); e.stopPropagation(); onClick(b); render(); };
return b;
};
const counter = document.createElement("span");
counter.style.cssText = `
color:${COLOR.text};font-size:10px;flex:1 1 auto;
font-family:ui-monospace,Menlo,monospace;
padding:3px 8px;background:var(--c2c-bg3);border:1px solid ${COLOR.border};
border-radius:4px;letter-spacing:.3px;
overflow:hidden;text-overflow:ellipsis;white-space:nowrap;
`;
tb.appendChild(counter);
tb.appendChild(mkIcon("+ Path", "Add new path (N)", () => {
ed.pushUndo(); ed.newPath(); ed.save();
}));
const closedBtn = mkIcon("◯", "Toggle closed (C)", () => {
if (ed.active < 0) return;
ed.pushUndo();
ed.shapes[ed.active].closed = !ed.shapes[ed.active].closed;
ed.save();
});
tb.appendChild(closedBtn);
tb.appendChild(mkIcon("↶", "Undo (Ctrl+Z)", () => { if (ed.undo()) ed.save(); }));
tb.appendChild(mkIcon("↷", "Redo (Ctrl+Y)", () => { if (ed.redo()) ed.save(); }));
tb.appendChild(mkIcon("\u2796", "Zoom out (-)", () => {
const r = canvasWrap.getBoundingClientRect();
ed.setZoomAround(ed.zoom * 0.85, r.width / 2, r.height / 2, r.width, r.height);
}));
tb.appendChild(mkIcon("\u2795", "Zoom in (+)", () => {
const r = canvasWrap.getBoundingClientRect();
ed.setZoomAround(ed.zoom * 1.15, r.width / 2, r.height / 2, r.width, r.height);
}));
tb.appendChild(mkIcon("\u2B1B", "Fit image (0 / F)", () => {
const r = canvasWrap.getBoundingClientRect(); ed.fitView(r.width, r.height);
}));
const menuBtn = mkIcon("≡", "More actions", () => toggleMenu());
tb.appendChild(menuBtn);
const menu = document.createElement("div");
menu.style.cssText = `
position:absolute;top:34px;right:6px;z-index:var(--c2c-z-hud);
background:var(--c2c-bg);border:1px solid ${COLOR.border};border-radius:6px;
box-shadow:0 6px 20px var(--c2c-black);padding:4px;display:none;
min-width:200px;font-size:11px;
`;
root.style.position = "relative";
root.appendChild(menu);
const mkMenuItem = (label, onClick, danger) => {
const it = document.createElement("div");
it.textContent = label;
it.style.cssText = `
padding:7px 12px;border-radius:4px;cursor:pointer;
color:${danger ? "var(--c2c-red)" : COLOR.text};white-space:nowrap;
`;
it.onmouseenter = () => it.style.background = C.border;
it.onmouseleave = () => it.style.background = "";
it.onclick = (e) => { e.stopPropagation(); menu.style.display = "none"; onClick(); render(); };
return it;
};
const mkMenuSep = () => {
const s = document.createElement("div");
s.style.cssText = `height:1px;background:${COLOR.border};margin:4px 2px;`;
return s;
};
menu.appendChild(mkMenuItem("Reset zoom (100%)", () => { ed.zoom = 1; ed.panX = 0; ed.panY = 0; }));
menu.appendChild(mkMenuItem("Fit to view", () => {
const r = canvasWrap.getBoundingClientRect(); ed.fitView(r.width, r.height);
}));
menu.appendChild(mkMenuSep());
menu.appendChild(mkMenuItem("Clear active path", () => {
if (ed.active < 0) return;
ed.pushUndo(); ed.shapes[ed.active].points = []; ed.save();
}, true));
menu.appendChild(mkMenuItem("Delete active path", () => {
if (ed.active < 0) return;
ed.pushUndo();
ed.shapes.splice(ed.active, 1);
ed.active = ed.shapes.length ? Math.min(ed.active, ed.shapes.length - 1) : -1;
ed.save();
}, true));
menu.appendChild(mkMenuItem("Clear all paths", () => {
if (!ed.shapes.length) return;
ed.pushUndo(); ed.shapes = []; ed.active = -1; ed.save();
}, true));
function toggleMenu() {
const open = menu.style.display === "none" || !menu.style.display;
menu.style.display = open ? "block" : "none";
if (open) {
const off = (e) => {
if (!menu.contains(e.target) && e.target !== menuBtn) {
menu.style.display = "none";
document.removeEventListener("mousedown", off, true);
}
};
setTimeout(() => document.addEventListener("mousedown", off, true), 0);
}
}
let widgetH = 220;
const canvasWidget = node.addDOMWidget("spline_editor", "canvas", root, {
serialize: false,
hideOnZoom: false,
getMinHeight: () => widgetH,
getHeight: () => widgetH,
});
canvasWidget.computeSize = function (width) {
const base = node.computeSize?.(width);
const chrome = Array.isArray(base) ? (base[1] || 0) : 0;
const extra = Math.max(0, (node.size?.[1] || 0) - chrome);
const h = extra > 120 ? Math.min(360, extra - 32) : widgetH;
widgetH = h;
return [width, Math.max(160, h)];
};
node._mecSplineEditHost = root;
node._mecSplineEditWidget = canvasWidget;
node._mecSplineEditWidgetH = () => widgetH;
node.setSize?.([Math.max(node.size?.[0] || 0, 600), Math.max(node.size?.[1] || 0, 380)]);
// When a backdrop image is loaded, grow the canvas widget so the
// image's aspect fits without huge empty bars. Mirrors points editor.
function resizeForImage() {
if (!ed.canvasW || !ed.canvasH) return;
const baseW = (node.size?.[0] || 600) - 24;
if (baseW <= 0) return;
const aspect = ed.canvasH / ed.canvasW;
const targetH = Math.max(220, Math.min(520, Math.round(baseW * aspect) + 48));
if (Math.abs(targetH - widgetH) >= 10) {
widgetH = targetH;
const curH = node.size?.[1] || widgetH;
const minH = (node.computeSize?.()?.[1]) || widgetH;
if (curH < minH) node.setSize?.([node.size[0], minH]);
}
}
let lastW = 0, lastH = 0;
function syncCanvasPx() {
const r = canvasWrap.getBoundingClientRect();
// Parent ComfyUI canvas applies CSS transform: scale(zoom) to DOM
// widgets. Use offsetWidth/Height (unscaled layout pixels) to keep
// the canvas resolution stable when the user zooms the graph.
const cssW = canvasWrap.offsetWidth || r.width;
const cssH = canvasWrap.offsetHeight || r.height;
const w = Math.max(1, Math.round(cssW));
const h = Math.max(1, Math.round(cssH));
const dpr = window.devicePixelRatio || 1;
const needPx = (canvas.width !== w * dpr) || (canvas.height !== h * dpr);
const sizeChanged = (w !== lastW) || (h !== lastH);
if (!sizeChanged && !needPx) return false;
lastW = w; lastH = h;
canvas.width = w * dpr;
canvas.height = h * dpr;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
// Auto-fit ONLY on first sizing or when a new image set _fitted=false.
// Previously we re-fit on every resize → any container reflow snapped
// the user's zoom/pan back to "fit", which is the "bouncy image"
// complaint. Now we just clamp pan into the new viewport instead.
if (!ed._fitted) {
ed.fitView(w, h);
} else {
ed.clampPan(w, h);
}
return true;
}
// Reflect "closed" state on the icon button
function updateClosedBtn() {
const sh = ed.active >= 0 ? ed.shapes[ed.active] : null;
const isClosed = sh?.closed;
closedBtn.textContent = isClosed ? "◉" : "◯";
closedBtn.title = isClosed ? "Active path is CLOSED — click to open (C)"
: "Active path is OPEN — click to close (C)";
}
let raf = 0;
function render() {
if (raf) return;
raf = requestAnimationFrame(() => {
raf = 0;
syncCanvasPx();
draw(ed, ctx, lastW, lastH);
const totalPts = ed.shapes.reduce((a, s) => a + s.points.length, 0);
const activePts = ed.active >= 0 ? ed.shapes[ed.active].points.length : 0;
const closed = ed.active >= 0 ? (ed.shapes[ed.active].closed ? "closed" : "open") : "—";
counter.textContent = `paths ${ed.shapes.length} · active ${ed.active >= 0 ? ed.active + 1 : "—"}/${ed.shapes.length} · pts ${activePts}/${totalPts} · ${closed}`;
status.textContent = `${ed.canvasW}×${ed.canvasH} · zoom ${(ed.zoom * 100).toFixed(0)}% · ${ed.splineType}`;
updateClosedBtn();
});
}
ed.onLoaded = () => { ed._fitted = false; resizeForImage(); render(); };
const ro = new ResizeObserver(() => render());
ro.observe(canvasWrap);
// ComfyUI's outer canvas applies a CSS scale transform when the user
// zooms the graph. getBoundingClientRect() returns the *visual*
// (post-transform) box; the canvas's logical pixels are unscaled.
// Multiply mouse delta by (offsetWidth / rect.width) to undo the
// parent scale so clicks land exactly under the cursor at any zoom.
function _scale(r, el) {
return {
sx: (el.offsetWidth || r.width) / Math.max(1, r.width),
sy: (el.offsetHeight || r.height) / Math.max(1, r.height),
};
}
function eventCanvas(e) {
const r = canvas.getBoundingClientRect();
const { sx, sy } = _scale(r, canvas);
return ed.viewToCanvas((e.clientX - r.left) * sx, (e.clientY - r.top) * sy);
}
function eventClient(e) {
const r = canvas.getBoundingClientRect();
const { sx, sy } = _scale(r, canvas);
return { x: (e.clientX - r.left) * sx, y: (e.clientY - r.top) * sy };
}
canvas.addEventListener("pointerdown", (e) => {
e.preventDefault(); canvas.focus();
canvas.setPointerCapture(e.pointerId);