Repository navigation
Expand file tree
/
Copy patheditor.js
More file actions
1577 lines (1445 loc) · 59.5 KB
/
Copy patheditor.js
File metadata and controls
1577 lines (1445 loc) · 59.5 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
(function () {
// Layer editor for the keychain customizer: image / text / shape layers on a 2D artboard,
// with move / resize / stretch / rotate handles, undo history, background removal and
// project (de)serialization. It knows nothing about 3D — app.js asks it for a flattened
// composite (renderComposite) and feeds that to the die-cut sticker pipeline.
const { showInfo } = window.KCModal;
const $ = id => document.getElementById(id);
const BASE_FONT_SIZE = 80;
const LINE_HEIGHT = 1.2;
const MIN_SCALE_FACTOR = 0.03;
const HANDLE_HIT_PX = 11;
const ROTATE_HANDLE_OFFSET_PX = 32;
// Built-in fonts come from Google Fonts (see <link> in keychain.html) so they render the
// same everywhere, unlike named OS fonts which silently fall back when not installed.
const BUILTIN_FONTS = [
{ value: `"Roboto", Arial, sans-serif`, label: 'Roboto' },
{ value: `"Montserrat", Arial, sans-serif`, label: 'Montserrat' },
{ value: `"Oswald", Impact, sans-serif`, label: 'Oswald (Condensed)' },
{ value: `"Bebas Neue", Impact, sans-serif`, label: 'Bebas Neue (Bold Display)' },
{ value: `"Anton", Impact, sans-serif`, label: 'Anton (Heavy Display)' },
{ value: `"Playfair Display", Georgia, serif`, label: 'Playfair Display' },
{ value: `"Merriweather", Georgia, serif`, label: 'Merriweather' },
{ value: `"Cinzel", Georgia, serif`, label: 'Cinzel (Engraved)' },
{ value: `"Roboto Mono", "Courier New", monospace`, label: 'Roboto Mono' },
{ value: `"Dancing Script", cursive`, label: 'Dancing Script (Script)' },
{ value: `"Pacifico", cursive`, label: 'Pacifico (Casual Script)' },
];
// default local (unscaled) bounding box for a freshly-added shape of each kind
const SHAPE_DEFAULTS = {
star: { w: 200, h: 200 },
rectangle: { w: 240, h: 160 },
square: { w: 180, h: 180 },
ellipse: { w: 240, h: 160 },
circle: { w: 180, h: 180 },
line: { w: 240, h: 24 },
};
const IMAGE_FIT_SIZE = 500; // longest edge (world units) of a newly added image
const TEXT_FIT = 2.5;
const SHAPE_FIT = 1.5;
const DEFAULT_HISTORY_LIMIT = 50;
const HISTORY_LIMIT_KEY = 'gripCustomizerHistoryLimit';
// --- color helpers ---------------------------------------------------------
function cmykToHex(c, m, y, k) {
c /= 100; m /= 100; y /= 100; k /= 100;
const ch = v => Math.max(0, Math.min(255, Math.round(255 * v * (1 - k)))).toString(16).padStart(2, '0');
return '#' + ch(1 - c) + ch(1 - m) + ch(1 - y);
}
function hexToCmyk(hex) {
const n = parseInt(hex.slice(1), 16);
const r = ((n >> 16) & 255) / 255, g = ((n >> 8) & 255) / 255, b = (n & 255) / 255;
const k = 1 - Math.max(r, g, b);
if (k >= 1) return { c: 0, m: 0, y: 0, k: 100 };
const pct = v => Math.round(v * 100);
return { c: pct((1 - r - k) / (1 - k)), m: pct((1 - g - k) / (1 - k)), y: pct((1 - b - k) / (1 - k)), k: pct(k) };
}
const layerHex = l => cmykToHex(l.color.c, l.color.m, l.color.y, l.color.k);
const isColorable = l => !!l && (l.type === 'text' || l.type === 'shape');
const cloneLayer = l => ({ ...l, color: l.color ? { ...l.color } : undefined });
// --- state ------------------------------------------------------------------
let layers = [];
let selectedId = null;
let idCounter = 0;
let assetCounter = 0;
let customFontCounter = 0;
let showGuide = true;
let onChange = () => {};
let onImagesDropped = () => {};
const assets = new Map(); // key -> { key, dataURL, img, name }
const customFonts = []; // { family, value, label, dataURL, fileName }
const pendingFonts = new Set();
const view = { zoom: 1, tx: 0, ty: 0 };
const MIN_VIEW_ZOOM = 0.05, MAX_VIEW_ZOOM = 12;
// --- DOM ---------------------------------------------------------------------
const stageContainer = $('stageContainer');
const canvas = $('stage');
const ctx = canvas.getContext('2d', { willReadFrequently: false });
const layerListEl = $('layerList');
const removeLayerBtn = $('removeLayerBtn');
const imageFieldsGroup = $('imageFieldsGroup');
const removeBgBtn = $('removeBgBtn');
const restoreOriginalBtn = $('restoreOriginalBtn');
const removeBgStatus = $('removeBgStatus');
const zoomRange = $('zoomRange'), zoomLabel = $('zoomLabel');
const rotRange = $('rotRange'), rotLabel = $('rotLabel');
const opacityRange = $('opacityRange'), opacityLabel = $('opacityLabel');
const shapeFieldsGroup = $('shapeFieldsGroup');
const shapeKindSelect = $('shapeKindSelect');
const shapeFilledRow = $('shapeFilledRow');
const shapeFilledCheckbox = $('shapeFilledCheckbox');
const shapeStrokeWidthField = $('shapeStrokeWidthField');
const shapeStrokeWidthRange = $('shapeStrokeWidthRange');
const shapeStrokeWidthLabel = $('shapeStrokeWidthLabel');
const colorFieldsGroup = $('colorFieldsGroup');
const colorPicker = $('colorPicker');
const colorSwatch = $('colorSwatch');
const cmykReadout = $('cmykReadout');
const cmykInputs = { c: $('cmykCInput'), m: $('cmykMInput'), y: $('cmykYInput'), k: $('cmykKInput') };
const colorDropperBtn = $('colorDropperBtn');
const textFieldsGroup = $('textFieldsGroup');
const textContentInput = $('textContentInput');
const fontSelect = $('fontSelect');
const builtinFontGroup = $('builtinFontGroup');
const customFontGroup = $('customFontGroup');
const fontFileInput = $('fontFileInput');
const historyListEl = $('historyList');
const historyLimitInput = $('historyLimitInput');
const getSelected = () => layers.find(l => l.id === selectedId) || null;
const layerLabel = l => {
if (!l) return 'layer';
if (l.type === 'text') return `"${(l.text || 'Text').split('\n')[0].slice(0, 20)}"`;
if (l.type === 'shape') return l.shapeKind;
return l.name || 'image';
};
// --- assets (image data kept out of layer objects so history snapshots stay tiny) ------
function loadImageElement(dataURL) {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => resolve(img);
img.onerror = () => reject(new Error('Could not decode that image.'));
img.src = dataURL;
});
}
async function registerAsset(dataURL, name) {
const img = await loadImageElement(dataURL);
const key = `a${++assetCounter}`;
const asset = { key, dataURL, img, name };
assets.set(key, asset);
return asset;
}
function readFileAsDataURL(file) {
return new Promise((resolve, reject) => {
const r = new FileReader();
r.onload = () => resolve(r.result);
r.onerror = () => reject(new Error(`Couldn't read ${file.name || 'that file'}.`));
r.readAsDataURL(file);
});
}
// --- fonts -------------------------------------------------------------------
function ensureFont(family) {
const p = document.fonts.load(`${BASE_FONT_SIZE}px ${family}`).then(() => {
let changed = false;
for (const l of layers) {
if (l.type === 'text' && l.fontFamily === family) { measureText(l); changed = true; }
}
if (changed) { requestDraw(); onChange(); }
}).catch(() => {}).finally(() => pendingFonts.delete(p));
pendingFonts.add(p);
return p;
}
const scratch = document.createElement('canvas').getContext('2d');
function textLines(l) { return (l.text || '').split('\n'); }
function measureText(l) {
scratch.font = `${BASE_FONT_SIZE}px ${l.fontFamily}`;
let w = 0;
const lines = textLines(l);
for (const line of lines) w = Math.max(w, scratch.measureText(line).width);
l.nw = Math.max(8, w);
l.nh = Math.max(BASE_FONT_SIZE * LINE_HEIGHT, lines.length * BASE_FONT_SIZE * LINE_HEIGHT);
}
function addFontOption(family, label) {
const opt = document.createElement('option');
opt.value = family;
opt.textContent = label;
customFontGroup.appendChild(opt);
}
async function registerCustomFont(fileName, dataURL, familyOverride, labelOverride) {
const baseName = (fileName || 'CustomFont').replace(/\.[^.]+$/, '');
const family = familyOverride || `UserFont${++customFontCounter}-${baseName.replace(/[^a-z0-9]+/gi, '').slice(0, 24)}`;
const face = new FontFace(family, `url(${dataURL})`);
await face.load();
document.fonts.add(face);
const value = `"${family}", sans-serif`;
const label = labelOverride || `${baseName} (custom)`;
customFonts.push({ family, value, label, dataURL, fileName });
addFontOption(value, label);
return value;
}
// --- layer factories -----------------------------------------------------------------
function newBase(type, fit) {
return {
id: ++idCounter, type, visible: true, opacity: 100,
cx: 0, cy: 0, sx: fit, sy: fit, rot: 0, flipX: false, flipY: false, fit,
};
}
function addLayer(layer, desc) {
layers.push(layer);
selectedId = layer.id;
refreshAll();
record(desc || `Added layer ${layerLabel(layer)}`);
}
async function addImageFiles(fileList) {
const files = Array.from(fileList || []).filter(f => f.type && f.type.startsWith('image/'));
for (const file of files) {
try {
const dataURL = await readFileAsDataURL(file);
const asset = await registerAsset(dataURL, file.name);
const nw = asset.img.naturalWidth, nh = asset.img.naturalHeight;
if (!nw || !nh) throw new Error('That image has no dimensions.');
const fit = IMAGE_FIT_SIZE / Math.max(nw, nh);
const layer = { ...newBase('image', fit), name: file.name, assetKey: asset.key, nw, nh };
addLayer(layer);
if (layers.length === 1) fitView();
} catch (err) {
showInfo(err.message || 'Could not add that image.');
}
}
}
function addTextLayer() {
const layer = {
...newBase('text', TEXT_FIT),
text: 'Your Text', fontFamily: BUILTIN_FONTS[0].value, color: { c: 0, m: 0, y: 0, k: 100 },
};
measureText(layer);
ensureFont(layer.fontFamily);
addLayer(layer);
if (layers.length === 1) fitView();
textContentInput.focus();
textContentInput.select();
}
function addShapeLayer(kind) {
const dims = SHAPE_DEFAULTS[kind] || SHAPE_DEFAULTS.rectangle;
const layer = {
...newBase('shape', SHAPE_FIT),
shapeKind: kind, filled: true, strokeWidth: 6, color: { c: 0, m: 0, y: 0, k: 100 },
nw: dims.w, nh: kind === 'line' ? 6 : dims.h,
};
addLayer(layer);
if (layers.length === 1) fitView();
}
function removeLayer(id) {
const idx = layers.findIndex(l => l.id === id);
if (idx === -1) return;
const removed = layers[idx];
layers.splice(idx, 1);
if (selectedId === id) selectedId = layers.length ? layers[Math.min(idx, layers.length - 1)].id : null;
refreshAll();
record(`Removed layer ${layerLabel(removed)}`);
}
// --- geometry ------------------------------------------------------------------------
function layerCorners(l) {
const hw = l.nw * l.sx / 2, hh = l.nh * l.sy / 2;
const r = l.rot * Math.PI / 180, cos = Math.cos(r), sin = Math.sin(r);
return [[-hw, -hh], [hw, -hh], [hw, hh], [-hw, hh]].map(([x, y]) => ({
x: l.cx + x * cos - y * sin, y: l.cy + x * sin + y * cos,
}));
}
function isDrawable(l) {
if (l.visible === false) return false;
if (l.type === 'image') { const a = assets.get(l.assetKey); return !!(a && a.img); }
if (l.type === 'text') return (l.text || '').trim().length > 0;
return true;
}
function layersBounds() {
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
for (const l of layers) {
if (!isDrawable(l)) continue;
for (const p of layerCorners(l)) {
if (p.x < minX) minX = p.x;
if (p.x > maxX) maxX = p.x;
if (p.y < minY) minY = p.y;
if (p.y > maxY) maxY = p.y;
}
}
return minX === Infinity ? null : { minX, minY, maxX, maxY };
}
function hitTest(wx, wy, pad) {
for (let i = layers.length - 1; i >= 0; i--) {
const l = layers[i];
if (!isDrawable(l)) continue;
const r = l.rot * Math.PI / 180, cos = Math.cos(r), sin = Math.sin(r);
const dx = wx - l.cx, dy = wy - l.cy;
const lx = dx * cos + dy * sin, ly = -dx * sin + dy * cos;
if (Math.abs(lx) <= l.nw * l.sx / 2 + pad && Math.abs(ly) <= l.nh * l.sy / 2 + pad) return l;
}
return null;
}
// --- drawing ---------------------------------------------------------------------------
function starPoints(hw, hh) {
const pts = [];
for (let i = 0; i < 10; i++) {
const a = -Math.PI / 2 + i * Math.PI / 5;
const rad = i % 2 === 0 ? 1 : 0.4;
pts.push([Math.cos(a) * rad, Math.sin(a) * rad]);
}
let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity;
for (const [x, y] of pts) {
if (x < minX) minX = x; if (x > maxX) maxX = x;
if (y < minY) minY = y; if (y > maxY) maxY = y;
}
return pts.map(([x, y]) => [
((x - minX) / (maxX - minX) - 0.5) * 2 * hw,
((y - minY) / (maxY - minY) - 0.5) * 2 * hh,
]);
}
function drawLayer(c, l) {
if (!isDrawable(l)) return;
c.save();
c.globalAlpha = Math.max(0, Math.min(1, l.opacity / 100));
c.translate(l.cx, l.cy);
c.rotate(l.rot * Math.PI / 180);
c.scale(l.sx * (l.flipX ? -1 : 1), l.sy * (l.flipY ? -1 : 1));
const hw = l.nw / 2, hh = l.nh / 2;
if (l.type === 'image') {
c.drawImage(assets.get(l.assetKey).img, -hw, -hh, l.nw, l.nh);
} else if (l.type === 'text') {
c.font = `${BASE_FONT_SIZE}px ${l.fontFamily}`;
c.fillStyle = layerHex(l);
c.textAlign = 'center';
c.textBaseline = 'middle';
const lh = BASE_FONT_SIZE * LINE_HEIGHT;
textLines(l).forEach((line, i) => c.fillText(line, 0, -hh + (i + 0.5) * lh));
} else if (l.type === 'shape') {
const color = layerHex(l);
const kind = l.shapeKind;
const filled = kind === 'line' || l.filled;
const sw = l.strokeWidth;
c.fillStyle = color;
c.strokeStyle = color;
c.lineWidth = sw;
c.lineJoin = 'round';
const inset = filled ? 0 : sw / 2;
if (kind === 'line') {
c.fillRect(-hw, -hh, l.nw, l.nh);
} else if (kind === 'ellipse' || kind === 'circle') {
c.beginPath();
c.ellipse(0, 0, Math.max(0.1, hw - inset), Math.max(0.1, hh - inset), 0, 0, Math.PI * 2);
filled ? c.fill() : c.stroke();
} else if (kind === 'star') {
const pts = starPoints(Math.max(0.1, hw - inset), Math.max(0.1, hh - inset));
c.beginPath();
pts.forEach(([x, y], i) => (i ? c.lineTo(x, y) : c.moveTo(x, y)));
c.closePath();
filled ? c.fill() : c.stroke();
} else {
const w = Math.max(0.1, l.nw - inset * 2), h = Math.max(0.1, l.nh - inset * 2);
filled ? c.fillRect(-hw, -hh, l.nw, l.nh) : c.strokeRect(-w / 2, -h / 2, w, h);
}
}
c.restore();
}
let drawQueued = false;
function requestDraw() {
if (drawQueued) return;
drawQueued = true;
requestAnimationFrame(() => { drawQueued = false; draw(); });
}
const toScreen = (x, y) => ({ x: x * view.zoom + view.tx, y: y * view.zoom + view.ty });
const toWorld = (sx, sy) => ({ x: (sx - view.tx) / view.zoom, y: (sy - view.ty) / view.zoom });
let dpr = 1;
function draw() {
const w = canvas.width / dpr, h = canvas.height / dpr;
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.setTransform(dpr * view.zoom, 0, 0, dpr * view.zoom, dpr * view.tx, dpr * view.ty);
for (const l of layers) drawLayer(ctx, l);
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
const b = layersBounds();
if (showGuide && b) {
const a = toScreen(b.minX, b.minY), z = toScreen(b.maxX, b.maxY);
ctx.save();
ctx.setLineDash([6, 5]);
ctx.lineWidth = 1;
ctx.strokeStyle = getComputedStyle(document.documentElement).getPropertyValue('--muted').trim() || '#888';
ctx.strokeRect(a.x, a.y, z.x - a.x, z.y - a.y);
ctx.restore();
}
if (!layers.length) {
ctx.fillStyle = getComputedStyle(document.documentElement).getPropertyValue('--muted').trim() || '#888';
ctx.font = '14px -apple-system, "Segoe UI", sans-serif';
ctx.textAlign = 'center';
ctx.fillText('Drop an image here or use "+ Add image", "+ Add text", or a shape.', w / 2, h / 2);
}
drawHandles();
}
// handles: [{ id, hx, hy }] where hx/hy in {-1,0,1} pick a corner/edge of the selection box
const HANDLE_DEFS = [
{ id: 'tl', hx: -1, hy: -1 }, { id: 'tr', hx: 1, hy: -1 }, { id: 'br', hx: 1, hy: 1 }, { id: 'bl', hx: -1, hy: 1 },
{ id: 'top', hx: 0, hy: -1 }, { id: 'bottom', hx: 0, hy: 1 }, { id: 'left', hx: -1, hy: 0 }, { id: 'right', hx: 1, hy: 0 },
];
function handlePositions(l) {
const hw = l.nw * l.sx / 2, hh = l.nh * l.sy / 2;
const r = l.rot * Math.PI / 180, cos = Math.cos(r), sin = Math.sin(r);
const screenPt = (lx, ly) => toScreen(l.cx + lx * cos - ly * sin, l.cy + lx * sin + ly * cos);
const out = HANDLE_DEFS.map(d => ({ ...d, s: screenPt(d.hx * hw, d.hy * hh) }));
// rotate handle sits a fixed screen distance beyond the top edge, along the layer's up axis
const tc = screenPt(0, -hh);
out.push({ id: 'rotate', s: { x: tc.x + Math.sin(r) * ROTATE_HANDLE_OFFSET_PX, y: tc.y - Math.cos(r) * ROTATE_HANDLE_OFFSET_PX }, tc });
return out;
}
function drawHandles() {
const l = getSelected();
if (!l || !isDrawable(l) || samplingColor) return;
const hs = handlePositions(l);
const accent = getComputedStyle(document.documentElement).getPropertyValue('--accent').trim() || '#3b82f6';
const corners = layerCorners(l).map(p => toScreen(p.x, p.y));
ctx.save();
ctx.strokeStyle = accent;
ctx.lineWidth = 1.5;
ctx.beginPath();
corners.forEach((p, i) => (i ? ctx.lineTo(p.x, p.y) : ctx.moveTo(p.x, p.y)));
ctx.closePath();
ctx.stroke();
const rot = hs.find(h => h.id === 'rotate');
ctx.beginPath();
ctx.moveTo(rot.tc.x, rot.tc.y);
ctx.lineTo(rot.s.x, rot.s.y);
ctx.stroke();
for (const h of hs) {
ctx.beginPath();
const corner = h.id.length === 2;
ctx.arc(h.s.x, h.s.y, h.id === 'rotate' ? 8 : 7, 0, Math.PI * 2);
ctx.fillStyle = corner ? '#fff' : accent;
ctx.strokeStyle = corner ? accent : '#fff';
ctx.lineWidth = 2;
ctx.fill();
ctx.stroke();
}
ctx.restore();
}
function hitHandle(sx, sy) {
const l = getSelected();
if (!l || !isDrawable(l)) return null;
const hs = handlePositions(l);
// rotate handle first so it wins when it overlaps a small layer's edge handles
hs.sort((a, b) => (a.id === 'rotate' ? -1 : b.id === 'rotate' ? 1 : 0));
for (const h of hs) {
if (Math.hypot(sx - h.s.x, sy - h.s.y) <= HANDLE_HIT_PX) return h;
}
return null;
}
const CURSORS = {
tl: 'nwse-resize', br: 'nwse-resize', tr: 'nesw-resize', bl: 'nesw-resize',
top: 'ns-resize', bottom: 'ns-resize', left: 'ew-resize', right: 'ew-resize', rotate: 'grab',
};
// --- pointer interaction ---------------------------------------------------------------
let drag = null;
let samplingColor = false;
function canvasPoint(e) {
const r = canvas.getBoundingClientRect();
return { x: e.clientX - r.left, y: e.clientY - r.top };
}
function resizeDrag(l, start, p, hx, hy) {
const rad = start.rot * Math.PI / 180, cos = Math.cos(rad), sin = Math.sin(rad);
const dx = p.x - start.cx, dy = p.y - start.cy;
const lx = dx * cos + dy * sin, ly = -dx * sin + dy * cos;
const hw = start.nw * start.sx / 2, hh = start.nh * start.sy / 2;
const ax = -hx * hw, ay = -hy * hh; // the opposite corner/edge stays pinned
let kx = 1, ky = 1;
if (hx !== 0 && hy !== 0) {
const vx = hx * hw - ax, vy = hy * hh - ay;
const den = vx * vx + vy * vy;
if (den < 1e-9) return;
kx = ky = ((lx - ax) * vx + (ly - ay) * vy) / den;
} else if (hx !== 0) {
const den = hx * hw - ax;
if (Math.abs(den) < 1e-9) return;
kx = (lx - ax) / den;
} else {
const den = hy * hh - ay;
if (Math.abs(den) < 1e-9) return;
ky = (ly - ay) / den;
}
kx = Math.max(MIN_SCALE_FACTOR, kx);
ky = Math.max(MIN_SCALE_FACTOR, ky);
const ox = ax * (1 - kx), oy = ay * (1 - ky);
l.sx = start.sx * kx;
l.sy = start.sy * ky;
l.cx = start.cx + ox * cos - oy * sin;
l.cy = start.cy + ox * sin + oy * cos;
}
canvas.addEventListener('pointerdown', e => {
const sp = canvasPoint(e);
if (samplingColor) {
if (e.button === 0) { e.preventDefault(); commitSample(sp); }
return;
}
canvas.setPointerCapture(e.pointerId);
if (e.button === 1 || e.button === 2) {
e.preventDefault();
drag = { kind: 'pan', lastX: e.clientX, lastY: e.clientY, pointerId: e.pointerId };
canvas.style.cursor = 'grabbing';
return;
}
if (e.button !== 0) return;
const handle = hitHandle(sp.x, sp.y);
const l = getSelected();
if (handle && l) {
drag = {
kind: handle.id === 'rotate' ? 'rotate' : 'resize', handle, pointerId: e.pointerId, moved: false,
start: { cx: l.cx, cy: l.cy, sx: l.sx, sy: l.sy, rot: l.rot, nw: l.nw, nh: l.nh },
desc: handle.id === 'rotate' ? 'Rotated' : 'Resized',
};
return;
}
const wp = toWorld(sp.x, sp.y);
const hit = hitTest(wp.x, wp.y, 4 / view.zoom);
if (hit) {
if (hit.id !== selectedId) { selectedId = hit.id; syncAll(); }
drag = { kind: 'move', pointerId: e.pointerId, wp, start: { cx: hit.cx, cy: hit.cy }, moved: false };
} else {
// empty space: deselect, and let a drag pan the workspace
if (selectedId !== null) { selectedId = null; syncAll(); }
drag = { kind: 'pan', lastX: e.clientX, lastY: e.clientY, pointerId: e.pointerId };
canvas.style.cursor = 'grabbing';
}
});
canvas.addEventListener('pointermove', e => {
const sp = canvasPoint(e);
if (samplingColor) { previewSample(sp); return; }
if (!drag) {
const h = hitHandle(sp.x, sp.y);
if (h) canvas.style.cursor = CURSORS[h.id];
else {
const wp = toWorld(sp.x, sp.y);
canvas.style.cursor = hitTest(wp.x, wp.y, 4 / view.zoom) ? 'move' : 'default';
}
return;
}
if (e.pointerId !== drag.pointerId) return;
if (drag.kind === 'pan') {
view.tx += e.clientX - drag.lastX;
view.ty += e.clientY - drag.lastY;
drag.lastX = e.clientX; drag.lastY = e.clientY;
requestDraw();
return;
}
const l = getSelected();
if (!l) return;
const wp = toWorld(sp.x, sp.y);
drag.moved = true;
if (drag.kind === 'move') {
l.cx = drag.start.cx + (wp.x - drag.wp.x);
l.cy = drag.start.cy + (wp.y - drag.wp.y);
drag.desc = 'Moved';
} else if (drag.kind === 'rotate') {
let deg = Math.atan2(wp.y - l.cy, wp.x - l.cx) * 180 / Math.PI + 90;
if (e.shiftKey) deg = Math.round(deg / 15) * 15;
l.rot = normalizeDeg(deg);
} else if (drag.kind === 'resize') {
resizeDrag(l, drag.start, wp, drag.handle.hx, drag.handle.hy);
}
syncTransformControls();
requestDraw();
onChange(true);
});
function endDrag(e) {
if (!drag || e.pointerId !== drag.pointerId) return;
const finished = drag;
drag = null;
canvas.style.cursor = 'default';
if (finished.kind !== 'pan' && finished.moved) {
const l = getSelected();
record(`${finished.desc || 'Moved'} layer ${layerLabel(l)}`);
renderLayerList();
}
}
canvas.addEventListener('pointerup', endDrag);
canvas.addEventListener('pointercancel', endDrag);
canvas.addEventListener('contextmenu', e => e.preventDefault());
canvas.addEventListener('mousedown', e => { if (e.button === 1) e.preventDefault(); });
canvas.addEventListener('wheel', e => {
e.preventDefault();
const l = getSelected();
if (e.shiftKey && l) {
l.rot = normalizeDeg(l.rot + (e.deltaY > 0 ? 5 : -5));
syncTransformControls();
requestDraw();
onChange(true);
debouncedRecord(`Rotated layer ${layerLabel(l)}`);
return;
}
const sp = canvasPoint(e);
zoomViewAt(sp.x, sp.y, e.deltaY < 0 ? 1.1 : 1 / 1.1);
}, { passive: false });
function zoomViewAt(px, py, factor) {
const next = Math.max(MIN_VIEW_ZOOM, Math.min(MAX_VIEW_ZOOM, view.zoom * factor));
const f = next / view.zoom;
view.tx = px - (px - view.tx) * f;
view.ty = py - (py - view.ty) * f;
view.zoom = next;
requestDraw();
}
function normalizeDeg(d) {
d = ((d + 180) % 360 + 360) % 360 - 180;
return d === -180 ? 180 : d;
}
let recordTimer = null;
function debouncedRecord(desc) {
clearTimeout(recordTimer);
recordTimer = setTimeout(() => record(desc), 400);
}
// drop images anywhere over the center area (either tab). Handles real files, and images
// dragged out of a web page (which arrive as a URL / <img> markup rather than a file).
const dropZone = document.querySelector('.stage-wrap');
let dragDepth = 0;
const hasDroppable = dt => !!dt && Array.from(dt.types || []).some(t => ['Files', 'text/uri-list', 'text/html'].includes(t));
const setDropHighlight = on => dropZone.querySelectorAll('.stage-container').forEach(c => c.classList.toggle('dragover', on));
dropZone.addEventListener('dragenter', e => {
if (!hasDroppable(e.dataTransfer)) return;
e.preventDefault();
dragDepth++;
setDropHighlight(true);
});
dropZone.addEventListener('dragover', e => {
if (!hasDroppable(e.dataTransfer)) return;
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
});
dropZone.addEventListener('dragleave', () => {
dragDepth = Math.max(0, dragDepth - 1);
if (!dragDepth) setDropHighlight(false);
});
dropZone.addEventListener('drop', async e => {
if (!hasDroppable(e.dataTransfer)) return;
e.preventDefault();
e.stopPropagation();
dragDepth = 0;
setDropHighlight(false);
onImagesDropped();
const dt = e.dataTransfer;
const files = Array.from(dt.files || []).filter(f => (f.type && f.type.startsWith('image/')) || /\.(png|jpe?g|webp|gif|bmp|svg)$/i.test(f.name));
if (files.length) { addImageFiles(files); return; }
// dragged from another page: pull an image URL out of the drag data
let url = (dt.getData('text/uri-list') || '').split(/\r?\n/).find(l => l && !l.startsWith('#'));
if (!url) {
const m = /<img[^>]+src=["']([^"']+)["']/i.exec(dt.getData('text/html') || '');
if (m) url = m[1];
}
if (!url) { showInfo("That drop didn't contain an image file."); return; }
try {
const blob = await (await fetch(url)).blob();
if (!blob.type.startsWith('image/')) throw new Error('not an image');
addImageFiles([new File([blob], decodeURIComponent(url.split('/').pop().split('?')[0]) || 'image', { type: blob.type })]);
} catch (err) {
showInfo("Couldn't read that image from the other page (the site may block it). Save the image to your computer and drop the file instead.");
}
});
// --- view fitting / canvas sizing ------------------------------------------------------------
function resizeCanvas() {
dpr = Math.min(window.devicePixelRatio || 1, 2);
const w = stageContainer.clientWidth, h = stageContainer.clientHeight;
if (!w || !h) return;
canvas.width = Math.round(w * dpr);
canvas.height = Math.round(h * dpr);
requestDraw();
}
new ResizeObserver(resizeCanvas).observe(stageContainer);
function fitView() {
const w = stageContainer.clientWidth || 800, h = stageContainer.clientHeight || 600;
const b = layersBounds() || { minX: -300, maxX: 300, minY: -300, maxY: 300 };
const bw = Math.max(1, b.maxX - b.minX), bh = Math.max(1, b.maxY - b.minY);
const pad = 70;
view.zoom = Math.max(MIN_VIEW_ZOOM, Math.min(MAX_VIEW_ZOOM, Math.min((w - pad * 2) / bw, (h - pad * 2) / bh)));
view.tx = w / 2 - ((b.minX + b.maxX) / 2) * view.zoom;
view.ty = h / 2 - ((b.minY + b.maxY) / 2) * view.zoom;
requestDraw();
}
// --- color dropper -------------------------------------------------------------------------------
const sampleCtx = (() => {
const c = document.createElement('canvas');
c.width = c.height = 1;
return c.getContext('2d', { willReadFrequently: true });
})();
function sampleWorldPoint(wx, wy) {
sampleCtx.setTransform(1, 0, 0, 1, 0, 0);
sampleCtx.clearRect(0, 0, 1, 1);
sampleCtx.setTransform(1, 0, 0, 1, -wx, -wy);
for (const l of layers) drawLayer(sampleCtx, l);
const d = sampleCtx.getImageData(0, 0, 1, 1).data;
return d[3] < 8 ? null : '#' + [d[0], d[1], d[2]].map(v => v.toString(16).padStart(2, '0')).join('');
}
let samplingBackup = null;
function startSampling() {
const l = getSelected();
if (!isColorable(l)) return;
samplingColor = true;
samplingBackup = { ...l.color };
colorDropperBtn.classList.add('active');
canvas.style.cursor = 'crosshair';
requestDraw();
}
function exitSampling(restore) {
samplingColor = false;
colorDropperBtn.classList.remove('active');
canvas.style.cursor = 'default';
const l = getSelected();
if (restore && l && samplingBackup) l.color = samplingBackup;
samplingBackup = null;
syncColorControls();
requestDraw();
}
function previewSample(sp) {
const l = getSelected();
const wp = toWorld(sp.x, sp.y);
const hex = sampleWorldPoint(wp.x, wp.y);
if (hex && l) {
colorSwatch.style.background = hex;
}
}
function commitSample(sp) {
const l = getSelected();
const wp = toWorld(sp.x, sp.y);
const hex = sampleWorldPoint(wp.x, wp.y);
if (hex && l) {
l.color = hexToCmyk(hex);
samplingBackup = null;
exitSampling(false);
requestDraw();
onChange();
record(`Changed color of layer ${layerLabel(l)}`);
} else {
exitSampling(true);
}
}
colorDropperBtn.addEventListener('click', () => (samplingColor ? exitSampling(true) : startSampling()));
// --- history --------------------------------------------------------------------------------------
let history = [];
let historyIndex = -1;
let historyLimit = DEFAULT_HISTORY_LIMIT;
try {
const saved = parseInt(localStorage.getItem(HISTORY_LIMIT_KEY), 10);
if (Number.isFinite(saved) && saved >= -1) historyLimit = saved;
} catch (e) { /* ignore */ }
historyLimitInput.value = historyLimit;
const snapshot = () => ({ layers: layers.map(cloneLayer), selectedId });
const stateKey = s => JSON.stringify(s.layers);
function record(desc) {
const s = snapshot();
const key = stateKey(s);
if (historyIndex >= 0 && history[historyIndex].key === key) return;
history = history.slice(0, historyIndex + 1);
history.push({ desc, state: s, key });
historyIndex = history.length - 1;
applyHistoryLimit();
renderHistory();
onChange();
}
function applyHistoryLimit() {
if (historyLimit < 0) return;
const maxEntries = historyLimit + 1; // N undo steps = N+1 states
if (history.length > maxEntries) {
const drop = history.length - maxEntries;
history = history.slice(drop);
historyIndex = Math.max(0, historyIndex - drop);
}
}
function restoreHistory(i) {
if (i < 0 || i >= history.length) return;
historyIndex = i;
layers = history[i].state.layers.map(cloneLayer);
selectedId = layers.some(l => l.id === history[i].state.selectedId) ? history[i].state.selectedId : null;
for (const l of layers) if (l.type === 'text') ensureFont(l.fontFamily);
refreshAll();
renderHistory();
onChange();
}
function undo() {
if (historyIndex > 0) restoreHistory(historyIndex - 1);
}
function resetHistory(desc) {
history = [];
historyIndex = -1;
record(desc);
}
function renderHistory() {
historyListEl.innerHTML = '';
history.forEach((entry, i) => {
const li = document.createElement('li');
li.className = 'history-item' + (i === historyIndex ? ' current' : i > historyIndex ? ' future' : '');
li.textContent = entry.desc;
li.title = entry.desc;
li.addEventListener('click', () => restoreHistory(i));
historyListEl.appendChild(li);
});
// scroll only the list itself (scrollIntoView would also scroll the whole side panel)
const current = historyListEl.querySelector('.current');
if (current) {
const top = current.offsetTop, bottom = top + current.offsetHeight;
if (top < historyListEl.scrollTop) historyListEl.scrollTop = top;
else if (bottom > historyListEl.scrollTop + historyListEl.clientHeight) historyListEl.scrollTop = bottom - historyListEl.clientHeight;
}
}
historyLimitInput.addEventListener('change', () => {
let v = parseInt(historyLimitInput.value, 10);
if (!Number.isFinite(v) || v < -1) v = DEFAULT_HISTORY_LIMIT;
historyLimit = v;
historyLimitInput.value = v;
try { localStorage.setItem(HISTORY_LIMIT_KEY, String(v)); } catch (e) { /* ignore */ }
applyHistoryLimit();
renderHistory();
});
// --- layer list ---------------------------------------------------------------------------------------
let dragSrcLi = null;
function renderLayerList() {
layerListEl.innerHTML = '';
if (!layers.length) {
const empty = document.createElement('div');
empty.className = 'layer-empty';
empty.textContent = 'No layers yet — add an image, text or shape.';
layerListEl.appendChild(empty);
return;
}
layers.slice().reverse().forEach(layer => {
const li = document.createElement('li');
li.className = 'layer-item' + (layer.id === selectedId ? ' active' : '') + (layer.visible === false ? ' hidden-layer' : '');
li.draggable = true;
li.dataset.id = String(layer.id);
const grip = document.createElement('span');
grip.className = 'layer-drag-handle';
grip.textContent = '⠿';
li.appendChild(grip);
const name = document.createElement('span');
name.className = 'layer-name';
const icon = layer.type === 'text' ? '🔤 ' : layer.type === 'shape' ? '🔷 ' : '';
name.textContent = icon + (layer.type === 'shape' ? layer.shapeKind.charAt(0).toUpperCase() + layer.shapeKind.slice(1) : layerLabel(layer).replace(/^"|"$/g, ''));
li.appendChild(name);
const eye = document.createElement('button');
eye.type = 'button';
eye.className = 'layer-icon-btn';
eye.title = layer.visible === false ? 'Show layer' : 'Hide layer';
eye.textContent = layer.visible === false ? '🚫' : '👁';
eye.addEventListener('click', e => {
e.stopPropagation();
layer.visible = layer.visible === false;
renderLayerList();
requestDraw();
record(`${layer.visible ? 'Showed' : 'Hid'} layer ${layerLabel(layer)}`);
});
li.appendChild(eye);
const rm = document.createElement('button');
rm.type = 'button';
rm.className = 'layer-icon-btn remove';
rm.title = 'Remove layer';
rm.textContent = '×';
rm.addEventListener('click', e => { e.stopPropagation(); removeLayer(layer.id); });
li.appendChild(rm);
li.addEventListener('click', () => selectLayer(layer.id));
li.addEventListener('dragstart', e => {
dragSrcLi = li;
li.classList.add('dragging');
e.dataTransfer.effectAllowed = 'move';
try { e.dataTransfer.setData('text/plain', li.dataset.id); } catch (err) { /* ignore */ }
});
li.addEventListener('dragover', e => {
e.preventDefault();
if (!dragSrcLi || dragSrcLi === li) return;
const rect = li.getBoundingClientRect();
li.parentNode.insertBefore(dragSrcLi, (e.clientY - rect.top) > rect.height / 2 ? li.nextSibling : li);
});
li.addEventListener('drop', e => e.preventDefault());
li.addEventListener('dragend', () => {
li.classList.remove('dragging');
dragSrcLi = null;
const topToBottom = Array.from(layerListEl.children).filter(el => el.dataset && el.dataset.id).map(el => el.dataset.id);
layers = topToBottom.slice().reverse().map(id => layers.find(l => String(l.id) === id)).filter(Boolean);
requestDraw();
record('Reordered layers');
});
layerListEl.appendChild(li);
});
}
function selectLayer(id) {
if (samplingColor) exitSampling(true);
selectedId = id;
syncAll();
}
// --- control syncing ---------------------------------------------------------------------------------------
function syncAll() {
renderLayerList();
syncTransformControls();
syncColorControls();
syncTypeControls();
requestDraw();
}
function refreshAll() {
syncAll();
}
function syncTransformControls() {
const l = getSelected();
const on = !!l;
removeLayerBtn.disabled = !on;
zoomRange.disabled = rotRange.disabled = opacityRange.disabled = !on;
$('rotLeftBtn').disabled = $('rotRightBtn').disabled = !on;
if (!l) return;
const z = Math.round(100 * l.sx / l.fit);
zoomRange.value = Math.max(+zoomRange.min, Math.min(+zoomRange.max, z));
zoomLabel.textContent = `${z}%`;
rotRange.value = Math.round(l.rot);
rotLabel.textContent = `${Math.round(l.rot)}°`;
opacityRange.value = l.opacity;
opacityLabel.textContent = `${Math.round(l.opacity)}%`;
}
function syncColorControls() {
const l = getSelected();
if (!isColorable(l)) return;
const hex = layerHex(l);
colorPicker.value = hex;
colorSwatch.style.background = hex;
cmykReadout.textContent = `C${Math.round(l.color.c)} M${Math.round(l.color.m)} Y${Math.round(l.color.y)} K${Math.round(l.color.k)}`;
for (const k of ['c', 'm', 'y', 'k']) {
if (document.activeElement !== cmykInputs[k]) cmykInputs[k].value = Math.round(l.color[k]);
}
}
function syncTypeControls() {
const l = getSelected();
imageFieldsGroup.style.display = l && l.type === 'image' ? '' : 'none';
shapeFieldsGroup.style.display = l && l.type === 'shape' ? '' : 'none';
colorFieldsGroup.style.display = isColorable(l) ? '' : 'none';
textFieldsGroup.style.display = l && l.type === 'text' ? '' : 'none';
if (!l) return;
if (l.type === 'image') {
const hasOriginal = !!l.origAssetKey;