-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1472 lines (1366 loc) · 64.9 KB
/
script.js
File metadata and controls
1472 lines (1366 loc) · 64.9 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
// Entry point: initialize the app once the DOM is fully loaded
import { hexToRgba, hexToHsv, hsvToHex, hexToHsl, hslToHex, rotateHue } from './color-utils.js';
window.addEventListener('load', () => {
// ===== Setup & DOM refs ==================================================
// --- DOM references ---
const canvas = document.getElementById('drawing-canvas');
// Visible viewport canvas/context (only used for rendering the document)
const ctx = canvas.getContext('2d');
const toolbar = document.getElementById('toolbar');
const colorPicker = document.getElementById('color-picker');
const customSwatch = document.getElementById('custom-color-swatch');
const customPicker = document.getElementById('custom-color-picker');
const cpSV = document.getElementById('cp-sv');
const cpH = document.getElementById('cp-h');
const cpHex = document.getElementById('cp-hex');
const cpClose = document.getElementById('cp-close');
const harmonySelector = document.getElementById('harmony-selector');
const paletteContainer = document.getElementById('palette-container');
const brushSizeSlider = document.getElementById('brush-size');
const primarySlider = document.getElementById('primary-slider');
const primarySliderLabel = document.getElementById('primary-slider-label');
const sprayOpacityContainer = document.getElementById('spray-opacity-container');
const sprayOpacitySlider = document.getElementById('spray-opacity-slider');
const brushPreview = document.getElementById('brush-preview');
const brushSizePreview = document.getElementById('brush-size-preview');
const symmetryToggle = document.getElementById('symmetry-toggle');
const roundedToggle = document.getElementById('rounded-corners-toggle');
const fillShapeToggle = document.getElementById('fill-shape-toggle');
const undoBtn = document.getElementById('undo-btn');
const redoBtn = document.getElementById('redo-btn');
const saveBtn = document.getElementById('save-image');
const resetViewBtn = document.getElementById('reset-view');
const clearButton = document.getElementById('clear-canvas');
const downloadLink = document.getElementById('download-link');
const saveModal = document.getElementById('save-modal');
const saveModalClose = document.getElementById('save-modal-close');
const saveFilename = document.getElementById('save-filename');
const saveFormat = document.getElementById('save-format');
const saveQualityRow = document.getElementById('save-quality-row');
const saveQuality = document.getElementById('save-quality');
const saveQualityVal = document.getElementById('save-quality-val');
const saveScale = document.getElementById('save-scale');
const saveBg = document.getElementById('save-bg');
const saveCancel = document.getElementById('save-cancel');
const saveConfirm = document.getElementById('save-confirm');
const helpBtn = document.getElementById('help-shortcuts');
const helpModal = document.getElementById('help-modal');
const helpModalClose = document.getElementById('help-modal-close');
const helpOk = document.getElementById('help-ok');
// Confirm clear modal refs
const confirmModal = document.getElementById('confirm-clear-modal');
const confirmClose = document.getElementById('confirm-clear-close');
const confirmCancel = document.getElementById('confirm-clear-cancel');
const confirmConfirm = document.getElementById('confirm-clear-confirm');
// --- Drawing state ---
let isDrawing = false; // true while mouse is held down on canvas
let activeTool = 'brush-tool'; // current tool id (matches button id)
let lastX = 0, lastY = 0; // last mouse position for freehand strokes
// --- Brush settings ---
let currentColor = '#ff9500'; // active color (hex)
let currentBrushSize = 10; // stroke width / nib size
let currentOpacity = 1; // alpha for most tools
let sprayStrength = 20; // particles per tick for spray tool
let sprayOpacity = 1; // alpha used for spray particles
let isSymmetryMode = false; // mirror painting horizontally
let isShapeFilled = false; // fill vs stroke for shape tools
let isRoundedRect = true; // rectangle tool corner style
// --- Calligraphy brush parameters ---
let nibAngleDeg = -35; // visual rotation for nib preview
let nibAngle = (nibAngleDeg * Math.PI) / 180; // precomputed radians
let nibAspect = 0.35; // ellipse aspect ratio
let calligraphyPoints = []; // sampled points along the stroke
let calligraphyOffscreenCanvas = null; // composited in-memory canvas for live stroke
let calligraphyOffCtx = null; // context for the offscreen canvas
let strokeOpacity = 1; // preserves opacity during commit
// --- Shape drawing + preview paths ---
let shapeStartX = 0, shapeStartY = 0; // drag start for shapes
let savedCanvasState; // snapshot for live preview
let savedStateDX = 0, savedStateDY = 0; // putImageData offsets after expansion
let currentPath; // freehand path being drawn
let pathPoints = []; // stored points for reconstructing strokes after canvas expansion
let symmetryPath; // mirrored path when symmetry enabled
// --- Undo/redo history ---
let history = []; // array of data URLs
let historyStep = -1; // index of current state
// --- UI interactions / color picker state ---
let isDraggingToolbar = false;
let dragOffsetX = 0, dragOffsetY = 0;
let isMouseOverToolbar = false; // suppress canvas cursor preview when over toolbar
let sliderTimeout; // timeout for brush size overlay
let hue = 30/360; // HSV hue [0..1]
let sat = 1; // HSV saturation [0..1]
let val = 1; // HSV value [0..1]
let isDraggingSV = false; // dragging state for SV canvas
let isDraggingH = false; // dragging state for Hue canvas
// Offscreen document canvas/context (actual drawing happens here)
let docCanvas = document.createElement('canvas');
let docCtx = docCanvas.getContext('2d');
// Viewport sizing (visible canvas), separate from document size
let cssWidth = 0, cssHeight = 0, dpr = 1; // viewport size and pixel ratio
let docCssW = 0, docCssH = 0, docDpr = 1; // document logical size (CSS px) and pixel ratio
// Pan/zoom state (screen = world * scale + offset)
let viewScale = 1;
let viewOffsetX = 0;
let viewOffsetY = 0;
function resizeViewport() {
cssWidth = window.innerWidth;
cssHeight = window.innerHeight;
dpr = Math.max(1, Math.min(3, window.devicePixelRatio || 1));
canvas.style.width = cssWidth + 'px';
canvas.style.height = cssHeight + 'px';
canvas.width = Math.floor(cssWidth * dpr);
canvas.height = Math.floor(cssHeight * dpr);
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
render();
}
function initDocumentCanvas(initW, initH) {
docCssW = Math.max(1, Math.floor(initW));
docCssH = Math.max(1, Math.floor(initH));
docDpr = dpr; // tie document resolution to current device ratio
docCanvas.width = Math.floor(docCssW * docDpr);
docCanvas.height = Math.floor(docCssH * docDpr);
docCtx.setTransform(docDpr, 0, 0, docDpr, 0, 0); // draw in CSS px on doc
docCtx.imageSmoothingEnabled = true;
// start transparent
docCtx.clearRect(0, 0, docCssW, docCssH);
}
function render() {
// draw the document canvas into the visible canvas with pan+zoom
if (!ctx || !docCanvas) return;
ctx.save();
// baseline to CSS pixels on visible canvas
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
// clear (fill with page bg color by clearing and letting CSS show through)
ctx.clearRect(0, 0, cssWidth, cssHeight);
// apply view transform and draw the doc in logical CSS px
ctx.translate(viewOffsetX, viewOffsetY);
ctx.scale(viewScale, viewScale);
ctx.drawImage(
docCanvas,
0, 0, docCanvas.width, docCanvas.height,
0, 0, docCssW, docCssH
);
ctx.restore();
}
// Initialize viewport and document sizes
resizeViewport();
initDocumentCanvas(window.innerWidth, window.innerHeight);
window.addEventListener('resize', () => { resizeViewport(); ensureToolbarInViewport(); });
// --- Infinite canvas expansion helpers --------------------------------
function expandDocument(leftPad, topPad, rightPad, bottomPad) {
const addL = Math.max(0, Math.floor(leftPad));
const addT = Math.max(0, Math.floor(topPad));
const addR = Math.max(0, Math.floor(rightPad));
const addB = Math.max(0, Math.floor(bottomPad));
if (!addL && !addT && !addR && !addB) return { expanded: false, addL: 0, addT: 0 };
const oldCssW = docCssW;
const oldCssH = docCssH;
const newCssW = oldCssW + addL + addR;
const newCssH = oldCssH + addT + addB;
const leftPx = addL * docDpr;
const topPx = addT * docDpr;
const newDoc = document.createElement('canvas');
newDoc.width = Math.floor(newCssW * docDpr);
newDoc.height = Math.floor(newCssH * docDpr);
const nd = newDoc.getContext('2d');
// Copy old pixels into new at offset
nd.setTransform(1,0,0,1,0,0);
nd.clearRect(0,0,newDoc.width,newDoc.height);
// When expanding during an active stroke, we need to copy the original document
// without any in-progress stroke that might be visible on it
if (savedCanvasState && isDrawing) {
nd.putImageData(savedCanvasState, leftPx, topPx);
} else {
nd.drawImage(docCanvas, leftPx, topPx);
}
// Set transform for CSS px drawing going forward
nd.setTransform(docDpr,0,0,docDpr,0,0);
nd.imageSmoothingEnabled = true;
// Replace document
docCanvas = newDoc;
docCtx = nd;
docCssW = newCssW;
docCssH = newCssH;
// Do not modify savedStateDX/DY here; we refresh snapshot after expansion
// Expand calligraphy offscreen canvas if active
if (calligraphyOffscreenCanvas && calligraphyOffCtx) {
const newOff = document.createElement('canvas');
newOff.width = newDoc.width;
newOff.height = newDoc.height;
const noctx = newOff.getContext('2d');
noctx.setTransform(1,0,0,1,0,0);
noctx.clearRect(0,0,newOff.width,newOff.height);
noctx.drawImage(calligraphyOffscreenCanvas, leftPx, topPx);
noctx.setTransform(docDpr,0,0,docDpr,0,0);
noctx.globalCompositeOperation = 'source-over';
noctx.globalAlpha = 1.0;
noctx.fillStyle = currentColor;
calligraphyOffscreenCanvas = newOff;
calligraphyOffCtx = noctx;
}
// Keep view anchored when adding space to left/top
if (addL) viewOffsetX -= addL * viewScale;
if (addT) viewOffsetY -= addT * viewScale;
render();
return { expanded: true, addL, addT };
}
function ensureDocBounds(x, y, radius = 0) {
const pad = 64; // extra breathing room in CSS px
const minX = x - radius - pad;
const minY = y - radius - pad;
const maxX = x + radius + pad;
const maxY = y + radius + pad;
let addL = 0, addT = 0, addR = 0, addB = 0;
if (minX < 0) addL = Math.ceil(-minX);
if (minY < 0) addT = Math.ceil(-minY);
if (maxX > docCssW) addR = Math.ceil(maxX - docCssW);
if (maxY > docCssH) addB = Math.ceil(maxY - docCssH);
if (addL || addT || addR || addB) return expandDocument(addL, addT, addR, addB);
return { expanded: false, addL: 0, addT: 0 };
}
// Detach color picker from toolbar so it overlays UI and doesn't affect toolbar size
if (customPicker && customPicker.parentElement !== document.body) {
document.body.appendChild(customPicker);
}
/** Push current canvas state into undo history. Truncates future states after undo. */
function saveHistory() {
if (historyStep < history.length - 1) {
history = history.slice(0, historyStep + 1);
}
history.push(docCanvas.toDataURL());
historyStep++;
updateUndoRedoButtons();
}
/** Restore a canvas state from a data URL. */
function loadState(stateData) {
const img = new Image();
img.onload = () => {
// draw the state into the document canvas (scale to document size)
docCtx.save();
docCtx.setTransform(docDpr, 0, 0, docDpr, 0, 0);
docCtx.clearRect(0, 0, docCssW, docCssH);
docCtx.drawImage(img, 0, 0, img.width, img.height, 0, 0, docCssW, docCssH);
docCtx.restore();
render();
};
img.src = stateData;
}
/** Step one state back if available. */
function undo() {
if (historyStep > 0) {
historyStep--;
loadState(history[historyStep]);
updateUndoRedoButtons();
}
}
/** Step one state forward if available. */
function redo() {
if (historyStep < history.length - 1) {
historyStep++;
loadState(history[historyStep]);
updateUndoRedoButtons();
}
}
/** Enable/disable undo/redo buttons based on history position. */
function updateUndoRedoButtons() {
const canUndo = historyStep > 0;
const canRedo = historyStep < history.length - 1;
undoBtn.disabled = !canUndo;
redoBtn.disabled = !canRedo;
}
/** Main drawing dispatcher for mousemove while drawing. */
function draw(e) {
if (!isDrawing) return;
const x = (e.offsetX - viewOffsetX) / viewScale;
const y = (e.offsetY - viewOffsetY) / viewScale;
// Expand document if near/over edge
const exp = ensureDocBounds(x, y, currentBrushSize / 2);
if (exp?.expanded) {
// For brush/eraser, we need to adjust all stored path points
if (activeTool.includes('brush') || activeTool.includes('eraser')) {
// Shift all stored points by the expansion amount
if (exp.addL || exp.addT) {
for (let i = 0; i < pathPoints.length; i++) {
pathPoints[i].x += exp.addL;
pathPoints[i].y += exp.addT;
}
}
}
// Shape start points need to be shifted for ongoing shape preview
if (activeTool.includes('rect') || activeTool.includes('circle')) {
shapeStartX += exp.addL;
shapeStartY += exp.addT;
}
// Also shift last position so next segment connects correctly
lastX += exp.addL;
lastY += exp.addT;
// Reapply drawing styles to the new context
docCtx.globalCompositeOperation = activeTool === 'eraser-tool' ? 'destination-out' : 'source-over';
docCtx.globalAlpha = (activeTool === 'spray-brush-tool') ? 1.0 : currentOpacity;
docCtx.strokeStyle = currentColor;
docCtx.fillStyle = currentColor;
docCtx.lineWidth = currentBrushSize;
docCtx.lineCap = 'round';
docCtx.lineJoin = 'round';
// Take a fresh snapshot of the expanded (but empty) document
if (activeTool !== 'spray-brush-tool') {
savedCanvasState = docCtx.getImageData(0, 0, docCanvas.width, docCanvas.height);
savedStateDX = 0; savedStateDY = 0;
}
// For calligraphy, we need to adjust the points
if (activeTool === 'calligraphy-brush-tool' && (exp.addL || exp.addT)) {
for (let i = 0; i < calligraphyPoints.length; i++) {
calligraphyPoints[i].x += exp.addL;
calligraphyPoints[i].y += exp.addT;
}
}
}
if (activeTool === 'spray-brush-tool') {
sprayBrush(x, y);
} else if (activeTool === 'calligraphy-brush-tool') {
if (savedCanvasState) docCtx.putImageData(savedCanvasState, savedStateDX, savedStateDY);
if (calligraphyPoints.length === 0) calligraphyPoints.push({ x: lastX, y: lastY });
const p0 = calligraphyPoints[calligraphyPoints.length - 1];
drawCalligraphySegment(calligraphyOffCtx, p0.x, p0.y, x, y);
calligraphyPoints.push({ x, y });
docCtx.save();
const prevAlpha = docCtx.globalAlpha;
const prevComp = docCtx.globalCompositeOperation;
docCtx.globalCompositeOperation = 'source-over';
docCtx.globalAlpha = strokeOpacity;
docCtx.drawImage(calligraphyOffscreenCanvas, 0, 0);
docCtx.globalAlpha = prevAlpha;
docCtx.globalCompositeOperation = prevComp;
[lastX, lastY] = [x, y];
} else {
if (savedCanvasState) docCtx.putImageData(savedCanvasState, savedStateDX, savedStateDY);
if (activeTool.includes('brush') || activeTool.includes('eraser')) {
renderStandardStroke(x, y);
} else if (activeTool.includes('rect') || activeTool.includes('circle')) {
drawShape(x, y);
}
}
render();
}
/** Render a typical freehand stroke (brush/eraser), including symmetry if enabled. */
function renderStandardStroke(x, y) {
// Add the new point to our points array
pathPoints.push({x, y});
// Restore original canvas state
if (savedCanvasState) {
docCtx.putImageData(savedCanvasState, savedStateDX, savedStateDY);
}
// Build and stroke the entire path
if (pathPoints.length >= 2) {
const p = new Path2D();
p.moveTo(pathPoints[0].x, pathPoints[0].y);
for (let i = 1; i < pathPoints.length; i++) {
p.lineTo(pathPoints[i].x, pathPoints[i].y);
}
docCtx.stroke(p);
// Draw mirrored path if symmetry is on
if (isSymmetryMode) {
docCtx.save();
docCtx.translate(docCssW, 0);
docCtx.scale(-1, 1);
docCtx.stroke(p);
docCtx.restore();
}
}
lastX = x; lastY = y;
}
/**
* Stamp an elliptical nib for the calligraphy brush at the given point.
* This builds up a stroke by repeated stamps along the path.
*/
function stampCalligraphyNib(targetCtx, x, y, angleRad) {
targetCtx.save();
targetCtx.translate(x, y);
targetCtx.rotate(angleRad);
targetCtx.beginPath();
targetCtx.ellipse(0, 0, currentBrushSize / 2, (currentBrushSize * nibAspect) / 2, 0, 0, Math.PI * 2);
targetCtx.fill();
targetCtx.restore();
}
/**
* Interpolate between two points and stamp nibs along the way to create
* a smooth calligraphy stroke. Mirrors if symmetry is enabled.
*/
function drawCalligraphySegment(targetCtx, x1, y1, x2, y2) {
const dx = x2 - x1;
const dy = y2 - y1;
const dist = Math.hypot(dx, dy);
if (dist === 0) {
stampCalligraphyNib(targetCtx, x2, y2, nibAngle);
if (isSymmetryMode) stampCalligraphyNib(targetCtx, docCssW - x2, y2, -nibAngle);
return;
}
const spacing = Math.max(1, currentBrushSize * 0.3);
const steps = Math.ceil(dist / spacing);
for (let i = 1; i <= steps; i++) {
const t = i / steps;
const px = x1 + dx * t;
const py = y1 + dy * t;
stampCalligraphyNib(targetCtx, px, py, nibAngle);
if (isSymmetryMode) {
stampCalligraphyNib(targetCtx, docCssW - px, py, -nibAngle);
}
}
}
// --- Batched spray brush -------------------------------------------------
const sprayQueue = [];
let sprayRaf = null;
function flushSpray() {
sprayRaf = null;
if (!sprayQueue.length) return;
docCtx.save();
docCtx.globalCompositeOperation = activeTool === 'eraser-tool' ? 'destination-out' : 'source-over';
docCtx.globalAlpha = 1;
docCtx.fillStyle = hexToRgba(currentColor, sprayOpacity);
const p = new Path2D();
for (let i = 0; i < sprayQueue.length; i++) {
const s = sprayQueue[i];
p.rect(s.x, s.y, 1, 1);
if (s.mx !== undefined) p.rect(s.mx, s.y, 1, 1);
}
docCtx.fill(p);
docCtx.restore();
sprayQueue.length = 0;
render();
}
/** Spray paint tool: enqueue tiny squares; flushed in one fill per frame. */
function sprayBrush(x, y) {
for (let i = 0; i < sprayStrength; i++) {
const offsetX = (Math.random() - 0.5) * currentBrushSize * 2;
const offsetY = (Math.random() - 0.5) * currentBrushSize * 2;
if (Math.hypot(offsetX, offsetY) < currentBrushSize) {
const sx = x + offsetX;
const sy = y + offsetY;
const sample = { x: sx, y: sy };
if (isSymmetryMode) sample.mx = docCssW - x - offsetX;
sprayQueue.push(sample);
}
}
if (!sprayRaf) sprayRaf = requestAnimationFrame(flushSpray);
}
/** Draw a rectangle or circle based on drag start and current cursor position. */
function drawShape(endX, endY) {
const startX = Math.min(shapeStartX, endX);
const startY = Math.min(shapeStartY, endY);
const width = Math.abs(endX - shapeStartX);
const height = Math.abs(endY - shapeStartY);
// Ensure canvas fits the shape area
const radius = activeTool === 'circle-tool' ? Math.hypot(width, height) / 2 : 0;
const pad = Math.max(currentBrushSize / 2, 1);
const minX = activeTool === 'circle-tool' ? (startX + width/2) - radius - pad : startX - pad;
const minY = activeTool === 'circle-tool' ? (startY + height/2) - radius - pad : startY - pad;
const maxX = activeTool === 'circle-tool' ? (startX + width/2) + radius + pad : startX + width + pad;
const maxY = activeTool === 'circle-tool' ? (startY + height/2) + radius + pad : startY + height + pad;
ensureDocBounds(minX, minY, 0); // handle left/top if needed
ensureDocBounds(maxX, maxY, 0); // handle right/bottom if needed
const needsSharpStrokeCorners = (activeTool === 'rect-tool' && !isRoundedRect && !isShapeFilled);
if (needsSharpStrokeCorners) {
docCtx.save();
docCtx.lineJoin = 'miter';
docCtx.miterLimit = 10;
docCtx.lineCap = 'butt';
}
docCtx.beginPath();
if (activeTool === 'rect-tool') {
if (isRoundedRect) {
const path = roundedRectPath(startX, startY, width, height, Math.min(20, Math.min(width, height) * 0.2));
isShapeFilled ? docCtx.fill(path) : docCtx.stroke(path);
} else {
docCtx.rect(startX, startY, width, height);
isShapeFilled ? docCtx.fill() : docCtx.stroke();
}
} else if (activeTool === 'circle-tool') {
const radius = Math.hypot(width, height) / 2;
docCtx.arc(startX + width/2, startY + height/2, radius, 0, Math.PI * 2);
isShapeFilled ? docCtx.fill() : docCtx.stroke();
}
if (isSymmetryMode) {
docCtx.beginPath();
if (activeTool === 'rect-tool') {
if (isRoundedRect) {
const path = roundedRectPath(docCssW - startX - width, startY, width, height, Math.min(20, Math.min(width, height) * 0.2));
isShapeFilled ? docCtx.fill(path) : docCtx.stroke(path);
} else {
docCtx.rect(docCssW - startX - width, startY, width, height);
isShapeFilled ? docCtx.fill() : docCtx.stroke();
}
} else if (activeTool === 'circle-tool') {
const radius = Math.hypot(width, height) / 2;
docCtx.arc(docCssW - (startX + width/2), startY + height/2, radius, 0, Math.PI * 2);
isShapeFilled ? docCtx.fill() : docCtx.stroke();
}
}
if (needsSharpStrokeCorners) {
docCtx.restore();
}
}
/** Build a rounded-rectangle Path2D with clamped radius. */
function roundedRectPath(x, y, w, h, r) {
const path = new Path2D();
const rr = Math.max(0, Math.min(r, Math.min(w, h) / 2));
path.moveTo(x + rr, y);
path.lineTo(x + w - rr, y);
path.quadraticCurveTo(x + w, y, x + w, y + rr);
path.lineTo(x + w, y + h - rr);
path.quadraticCurveTo(x + w, y + h, x + w - rr, y + h);
path.lineTo(x + rr, y + h);
path.quadraticCurveTo(x, y + h, x, y + h - rr);
path.lineTo(x, y + rr);
path.quadraticCurveTo(x, y, x + rr, y);
path.closePath();
return path;
}
/**
* Pointer down: initialize tool-specific state and capture a canvas snapshot
* for live preview (except for spray, which draws directly).
*/
function startDrawing(e) {
isDrawing = true;
const wx = (e.offsetX - viewOffsetX) / viewScale;
const wy = (e.offsetY - viewOffsetY) / viewScale;
[lastX, lastY] = [wx, wy];
// Ensure we have room when starting the stroke
ensureDocBounds(wx, wy, currentBrushSize / 2);
docCtx.globalCompositeOperation = activeTool === 'eraser-tool' ? 'destination-out' : 'source-over';
docCtx.globalAlpha = (activeTool === 'spray-brush-tool') ? 1.0 : currentOpacity;
docCtx.strokeStyle = currentColor;
docCtx.fillStyle = currentColor;
docCtx.lineWidth = currentBrushSize;
docCtx.lineCap = 'round';
docCtx.lineJoin = 'round';
if (activeTool === 'calligraphy-brush-tool' || activeTool.includes('rect') || activeTool.includes('circle') || activeTool.includes('brush') || activeTool.includes('eraser')) {
savedCanvasState = docCtx.getImageData(0, 0, docCanvas.width, docCanvas.height);
savedStateDX = 0; savedStateDY = 0;
} else {
savedCanvasState = null; savedStateDX = 0; savedStateDY = 0;
}
if (activeTool.includes('rect') || activeTool.includes('circle')) {
[shapeStartX, shapeStartY] = [wx, wy];
} else if (activeTool === 'calligraphy-brush-tool') {
calligraphyOffscreenCanvas = document.createElement('canvas');
calligraphyOffscreenCanvas.width = docCanvas.width;
calligraphyOffscreenCanvas.height = docCanvas.height;
calligraphyOffCtx = calligraphyOffscreenCanvas.getContext('2d');
calligraphyOffCtx.setTransform(docDpr, 0, 0, docDpr, 0, 0);
calligraphyOffCtx.clearRect(0, 0, docCssW, docCssH);
calligraphyOffCtx.globalCompositeOperation = 'source-over';
calligraphyOffCtx.globalAlpha = 1.0;
calligraphyOffCtx.fillStyle = currentColor;
calligraphyPoints = [{ x: lastX, y: lastY }];
strokeOpacity = currentOpacity;
} else if (activeTool.includes('brush') || activeTool.includes('eraser')) {
// Initialize empty path points array
pathPoints = [{x: lastX, y: lastY}];
}
render();
}
/** Pointer up/leave: finalize rendering and push to history. */
function stopDrawing(e) {
if (!isDrawing) return;
isDrawing = false;
if (activeTool !== 'spray-brush-tool' && savedCanvasState) {
docCtx.putImageData(savedCanvasState, savedStateDX, savedStateDY);
if (activeTool === 'calligraphy-brush-tool') {
docCtx.save();
const prevAlpha = docCtx.globalAlpha;
const prevComp = docCtx.globalCompositeOperation;
docCtx.globalCompositeOperation = 'source-over';
docCtx.globalAlpha = strokeOpacity;
docCtx.drawImage(calligraphyOffscreenCanvas, 0, 0);
docCtx.globalAlpha = prevAlpha;
docCtx.globalCompositeOperation = prevComp;
} else if (activeTool.includes('rect') || activeTool.includes('circle')) {
const wx = (e.offsetX - viewOffsetX) / viewScale;
const wy = (e.offsetY - viewOffsetY) / viewScale;
drawShape(wx, wy);
} else if (activeTool.includes('brush') || activeTool.includes('eraser')) {
// Commit the path from points array
if (pathPoints.length >= 2) {
const p = new Path2D();
p.moveTo(pathPoints[0].x, pathPoints[0].y);
for (let i = 1; i < pathPoints.length; i++) {
p.lineTo(pathPoints[i].x, pathPoints[i].y);
}
docCtx.stroke(p);
// Draw mirrored path if symmetry is on
if (isSymmetryMode) {
docCtx.save();
docCtx.translate(docCssW, 0);
docCtx.scale(-1, 1);
docCtx.stroke(p);
docCtx.restore();
}
}
}
}
saveHistory();
render();
savedCanvasState = null;
currentPath = null;
symmetryPath = null;
pathPoints = [];
calligraphyPoints = [];
calligraphyOffCtx = null;
calligraphyOffscreenCanvas = null;
}
/** Activate a tool by id and adjust UI + cursor + primary slider semantics. */
function setActiveTool(toolId) {
activeTool = toolId;
document.querySelectorAll('.tool-btn[id$="-tool"]').forEach(btn => {
btn.classList.toggle('active', btn.id === toolId);
btn.setAttribute('aria-pressed', String(btn.id === toolId));
});
canvas.className = `cursor-${toolId}`;
if (toolId === 'spray-brush-tool') {
primarySliderLabel.textContent = "Strength:";
primarySlider.min = 5;
primarySlider.max = 50;
primarySlider.step = 1;
primarySlider.value = sprayStrength;
sprayOpacityContainer.style.display = 'flex';
} else {
primarySliderLabel.textContent = "Opacity:";
primarySlider.min = 0.1;
primarySlider.max = 1;
primarySlider.step = 0.1;
primarySlider.value = currentOpacity;
sprayOpacityContainer.style.display = 'none';
}
if (roundedToggle) {
roundedToggle.style.display = (toolId === 'rect-tool') ? 'grid' : 'none';
roundedToggle.classList.toggle('active', isRoundedRect);
roundedToggle.setAttribute('aria-pressed', String(isRoundedRect));
}
}
/** Show a live brush-sized overlay following the cursor (for brush/eraser/calligraphy). */
function updateBrushCursorPreview(e) {
if (isMouseOverToolbar || isDrawing) {
brushPreview.style.display = 'none';
return;
}
const showPreview = activeTool.includes('brush') || activeTool === 'eraser-tool';
brushPreview.style.display = showPreview ? 'block' : 'none';
canvas.classList.toggle('hide-cursor', showPreview);
if (showPreview && e) {
const isCalligraphy = activeTool === 'calligraphy-brush-tool';
const w = currentBrushSize * viewScale;
const h = (isCalligraphy ? currentBrushSize * nibAspect : currentBrushSize) * viewScale;
brushPreview.style.width = `${w}px`;
brushPreview.style.height = `${h}px`;
brushPreview.style.left = `${e.clientX}px`;
brushPreview.style.top = `${e.clientY}px`;
brushPreview.style.borderRadius = '50%';
const rot = isCalligraphy ? ` rotate(${nibAngleDeg}deg)` : '';
brushPreview.style.transform = `translate(-50%, -50%)${rot}`;
try { brushPreview.style.backgroundColor = hexToRgba(currentColor, 0.2); } catch (_) {}
}
}
// --- Random shape helpers (used by potential future tools/features) ---
function generateRandomShapePoints(size) {
const half = size / 2;
const angleOffset = Math.random() * Math.PI * 2;
const points = [];
const shapeType = pickOne(['triangle','polygon','star','blob']);
switch (shapeType) {
case 'triangle': {
for (let i = 0; i < 3; i++) {
const ang = angleOffset + i * (2 * Math.PI / 3);
const r = half * (0.8 + Math.random() * 0.4);
points.push({ x: Math.cos(ang) * r, y: Math.sin(ang) * r });
}
break;
}
case 'polygon': {
const sides = 4 + Math.floor(Math.random() * 4);
for (let i = 0; i < sides; i++) {
const ang = angleOffset + i * (2 * Math.PI / sides);
const r = half * (0.7 + Math.random() * 0.6);
points.push({ x: Math.cos(ang) * r, y: Math.sin(ang) * r });
}
break;
}
case 'star': {
const spikes = 5 + Math.floor(Math.random() * 3);
const inner = half * 0.4;
const outer = half;
for (let i = 0; i < spikes * 2; i++) {
const r = i % 2 === 0 ? outer : inner;
const ang = angleOffset + i * (Math.PI / spikes);
points.push({ x: Math.cos(ang) * r, y: Math.sin(ang) * r });
}
break;
}
case 'blob':
default: {
const count = 8 + Math.floor(Math.random() * 6);
for (let i = 0; i < count; i++) {
const ang = angleOffset + i * (2 * Math.PI / count);
const r = half * (0.6 + Math.random() * 0.6);
const jx = (Math.random() * 2 - 1) * size * 0.05;
const jy = (Math.random() * 2 - 1) * size * 0.05;
points.push({ x: Math.cos(ang) * r + jx, y: Math.sin(ang) * r + jy });
}
break;
}
}
return points;
}
function paintShape(ctx2, cx, cy, points) {
if (!points || points.length === 0) return;
const path = new Path2D();
path.moveTo(cx + points[0].x, cy + points[0].y);
for (let i = 1; i < points.length; i++) {
path.lineTo(cx + points[i].x, cy + points[i].y);
}
path.closePath();
if (isShapeFilled) ctx2.fill(path); else ctx2.stroke(path);
}
function drawRandomShape(targetCtx, x, y, size, color, mirror) {
const points = generateRandomShapePoints(size);
targetCtx.save();
targetCtx.fillStyle = color;
targetCtx.strokeStyle = color;
targetCtx.lineWidth = Math.max(1, size * 0.1);
paintShape(targetCtx, x, y, points);
if (mirror) {
const mx = docCssW - x;
paintShape(targetCtx, mx, y, points);
}
targetCtx.restore();
}
function pickOne(arr) { return arr[Math.floor(Math.random() * arr.length)]; }
/** Temporarily show a centered overlay matching the current brush size. */
function showBrushSizePreview() {
brushPreview.style.display = 'none';
const isCalligraphy = activeTool === 'calligraphy-brush-tool';
const w = currentBrushSize * viewScale;
const h = (isCalligraphy ? currentBrushSize * nibAspect : currentBrushSize) * viewScale;
brushSizePreview.style.width = `${w}px`;
brushSizePreview.style.height = `${h}px`;
const baseTranslate = 'translate(-50%, -50%)';
brushSizePreview.style.transform = isCalligraphy
? `${baseTranslate} rotate(${nibAngleDeg}deg)`
: baseTranslate;
try {
brushSizePreview.style.backgroundColor = hexToRgba(currentColor, 0.3);
} catch (_) {
}
brushSizePreview.classList.remove('hidden');
clearTimeout(sliderTimeout);
sliderTimeout = setTimeout(() => brushSizePreview.classList.add('hidden'), 1000);
}
/** Keep toolbar within viewport bounds */
function ensureToolbarInViewport() {
const margin = 8;
const w = toolbar.offsetWidth;
const h = toolbar.offsetHeight;
let left = parseFloat(toolbar.style.left || '0');
let top = parseFloat(toolbar.style.top || '0');
const maxLeft = Math.max(margin, window.innerWidth - w - margin);
const maxTop = Math.max(margin, window.innerHeight - h - margin);
left = Math.min(Math.max(left, margin), maxLeft);
top = Math.min(Math.max(top, margin), maxTop);
toolbar.style.left = `${left}px`;
toolbar.style.top = `${top}px`;
}
/** Drag handler to move the floating toolbar around the screen. */
function dragToolbar(e) {
if (!isDraggingToolbar) return;
toolbar.style.transform = 'translateX(0)';
toolbar.style.left = `${e.clientX - dragOffsetX}px`;
toolbar.style.top = `${e.clientY - dragOffsetY}px`;
ensureToolbarInViewport();
}
/** Stop dragging the toolbar and remove listeners. */
function stopDragToolbar() {
isDraggingToolbar = false;
window.removeEventListener('mousemove', dragToolbar);
window.removeEventListener('mouseup', stopDragToolbar);
}
// --- Pan/zoom interactions ---
let isPanning = false;
let panStartX = 0, panStartY = 0;
let spaceDown = false;
function startPan(e) {
isPanning = true;
panStartX = e.clientX;
panStartY = e.clientY;
canvas.classList.add('panning');
}
function doPan(e) {
if (!isPanning) return;
const dx = e.clientX - panStartX;
const dy = e.clientY - panStartY;
panStartX = e.clientX;
panStartY = e.clientY;
viewOffsetX += dx;
viewOffsetY += dy;
render();
}
function endPan() { isPanning = false; canvas.classList.remove('panning'); }
function zoomAt(screenX, screenY, factor) {
const wx = (screenX - viewOffsetX) / viewScale;
const wy = (screenY - viewOffsetY) / viewScale;
viewScale = Math.max(0.1, Math.min(8, viewScale * factor));
// keep point (wx, wy) stationary in screen space
viewOffsetX = screenX - wx * viewScale;
viewOffsetY = screenY - wy * viewScale;
render();
}
canvas.addEventListener('mousedown', (e) => {
if (isMouseOverToolbar) return;
if (spaceDown || e.button === 1) { startPan(e); return; }
startDrawing(e);
});
canvas.addEventListener('mousemove', draw);
window.addEventListener('mousemove', (e) => { if (isPanning) doPan(e); });
window.addEventListener('mouseup', (e) => {
if (isPanning) { endPan(); return; }
// Ensure offsets exist even if mouseup happens outside canvas
if (typeof e.offsetX !== 'number' || typeof e.offsetY !== 'number') {
const rect = canvas.getBoundingClientRect();
const ox = e.clientX - rect.left;
const oy = e.clientY - rect.top;
stopDrawing({ offsetX: ox, offsetY: oy });
} else {
stopDrawing(e);
}
});
canvas.addEventListener('mouseout', () => { if (isDrawing) stopDrawing({offsetX: lastX * viewScale + viewOffsetX, offsetY: lastY * viewScale + viewOffsetY}); });
canvas.addEventListener('wheel', (e) => {
if (e.ctrlKey) {
e.preventDefault();
const factor = Math.pow(1.1, -e.deltaY / 100);
zoomAt(e.offsetX, e.offsetY, factor);
} else {
// pan with wheel
viewOffsetX -= e.deltaX;
viewOffsetY -= e.deltaY;
render();
}
}, { passive: false });
window.addEventListener('keydown', (e) => { if (e.code === 'Space') { spaceDown = true; } });
window.addEventListener('keyup', (e) => { if (e.code === 'Space') { spaceDown = false; } });
// Quick reset: double-click to reset view to 1x at origin
canvas.addEventListener('dblclick', () => {
viewScale = 1; viewOffsetX = 0; viewOffsetY = 0; render();
});
window.addEventListener('mousemove', updateBrushCursorPreview);
toolbar.addEventListener('mouseenter', () => { isMouseOverToolbar = true; updateBrushCursorPreview(); });
toolbar.addEventListener('mouseleave', () => { isMouseOverToolbar = false; updateBrushCursorPreview(); });
toolbar.addEventListener('mousedown', (e) => {
if (e.target.closest('button, input, select, .palette-swatch, .color-picker-popup, .color-swatch')) return;
isDraggingToolbar = true;
const rect = toolbar.getBoundingClientRect();
dragOffsetX = e.clientX - rect.left;
dragOffsetY = e.clientY - rect.top;
window.addEventListener('mousemove', dragToolbar);
window.addEventListener('mouseup', stopDragToolbar);
});
document.querySelectorAll('.tool-btn[id$="-tool"]').forEach(btn => btn.addEventListener('click', () => setActiveTool(btn.id)));
colorPicker.addEventListener('input', (e) => {
currentColor = e.target.value;
if (customSwatch) customSwatch.style.background = currentColor;
if (cpHex && cpHex !== document.activeElement) cpHex.value = currentColor;
try {
const hsv = hexToHsv(currentColor);
hue = hsv.h; sat = hsv.s; val = hsv.v;
drawHue();
drawSV();
} catch(_) {}
updateHarmonyPalette();
});
harmonySelector.addEventListener('change', updateHarmonyPalette);
paletteContainer.addEventListener('click', (e) => {
if (e.target.classList.contains('palette-swatch')) {
colorPicker.value = e.target.dataset.hex;
colorPicker.dispatchEvent(new Event('input'));
}
});
brushSizeSlider.addEventListener('input', (e) => { currentBrushSize = Number(e.target.value); showBrushSizePreview(); });
primarySlider.addEventListener('input', (e) => {
if (activeTool === 'spray-brush-tool') {
sprayStrength = Number(e.target.value);
} else {
currentOpacity = Number(e.target.value);
}
});
sprayOpacitySlider.addEventListener('input', (e) => { sprayOpacity = Number(e.target.value); });
symmetryToggle.addEventListener('click', () => {
isSymmetryMode = !isSymmetryMode;
symmetryToggle.classList.toggle('active', isSymmetryMode);
symmetryToggle.setAttribute('aria-pressed', String(isSymmetryMode));
});
if (roundedToggle) {
roundedToggle.addEventListener('click', () => {
isRoundedRect = !isRoundedRect;
roundedToggle.classList.toggle('active', isRoundedRect);
roundedToggle.setAttribute('aria-pressed', String(isRoundedRect));
});
}
fillShapeToggle.addEventListener('click', () => {
isShapeFilled = !isShapeFilled;
fillShapeToggle.classList.toggle('active', isShapeFilled);
fillShapeToggle.setAttribute('aria-pressed', String(isShapeFilled));