-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
790 lines (644 loc) · 22.4 KB
/
Copy pathscript.js
File metadata and controls
790 lines (644 loc) · 22.4 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
// ========================================
// CANVAS OCH KONTEXT - LAGER-SYSTEM
// ========================================
// Hämta alla canvas-lager
const layer0 = document.getElementById('layer0');
const layer1 = document.getElementById('layer1');
const tempCanvas = document.getElementById('tempCanvas');
// Hämta contexts för varje lager
const ctx0 = layer0.getContext('2d');
const ctx1 = layer1.getContext('2d');
const tempCtx = tempCanvas.getContext('2d');
// Array med alla lager för enkel hantering
const layers = [layer0, layer1];
const contexts = [ctx0, ctx1];
// ========================================
// TILLSTÅNDSVARIABLER
// ========================================
// Aktuellt aktivt lager (0 eller 1)
let activeLayerIndex = 0;
// Nuvarande aktivt verktyg
let currentTool = 'brush'; // 'brush', 'eraser', 'rectangle', 'circle', 'triangle', 'fill'
// Spårar om användaren ritar
let isDrawing = false;
// Position för ritning
let lastX = 0;
let lastY = 0;
// Startposition för former (när användaren börjar dra)
let startX = 0;
let startY = 0;
// Aktuell färg och penselstorlek
let currentColor = '#000000';
let currentBrushSize = 5;
// Processing flag för flood fill
let isProcessing = false;
// ========================================
// VALIDERING AV ANVÄNDARINPUT
// ========================================
/**
* Validerar och säkrar färgvärde
*/
function sanitizeColor(color) {
// Kontrollera att det är en giltig hex-färg
const hexPattern = /^#[0-9A-Fa-f]{6}$/;
if (typeof color === 'string' && hexPattern.test(color)) {
return color;
}
console.warn('Ogiltig färg detekterad, använder standardfärg #000000');
return '#000000';
}
/**
* Validerar och säkrar penselstorlek
*/
function sanitizeBrushSize(size) {
const numSize = Number(size);
// Säkerställ att värdet är ett positivt tal mellan 1 och 50
if (isNaN(numSize) || numSize < 1 || numSize > 50) {
console.warn('Ogiltig penselstorlek detekterad, använder standardstorlek 5');
return 5;
}
return Math.floor(numSize);
}
/**
* Validerar koordinater
*/
function sanitizeCoordinate(coord, max) {
const numCoord = Number(coord);
if (isNaN(numCoord)) return 0;
return Math.max(0, Math.min(max, Math.floor(numCoord)));
}
/**
* Säker getter för aktuell färg
*/
function getCurrentColor() {
return sanitizeColor(currentColor);
}
/**
* Säker getter för aktuell penselstorlek
*/
function getCurrentBrushSize() {
return sanitizeBrushSize(currentBrushSize);
}
// ========================================
// UNDO/REDO SYSTEM - UPPDATERAT FÖR LAGER
// ========================================
// Historik per lager
let history = [[], []]; // En array för varje lager
let historyStep = [-1, -1]; // En pekare per lager
const MAX_HISTORY = 20;
/**
* Sparar tillstånd för aktivt lager
*/
function saveState() {
const layerIndex = activeLayerIndex;
historyStep[layerIndex]++;
// Ta bort framtida historik om vi ritar efter undo
if (historyStep[layerIndex] < history[layerIndex].length) {
history[layerIndex].length = historyStep[layerIndex];
}
// Spara aktuellt lagers tillstånd
const ctx = contexts[layerIndex];
const canvas = layers[layerIndex];
history[layerIndex].push(ctx.getImageData(0, 0, canvas.width, canvas.height));
// Begränsa historik
if (history[layerIndex].length > MAX_HISTORY) {
history[layerIndex].shift();
historyStep[layerIndex]--;
}
updateButtons();
}
/**
* Ångra senaste ändring på aktivt lager
*/
function undo() {
const layerIndex = activeLayerIndex;
if (historyStep[layerIndex] > 0) {
historyStep[layerIndex]--;
const ctx = contexts[layerIndex];
ctx.putImageData(history[layerIndex][historyStep[layerIndex]], 0, 0);
updateButtons();
}
}
/**
* Gör om ångrad ändring på aktivt lager
*/
function redo() {
const layerIndex = activeLayerIndex;
if (historyStep[layerIndex] < history[layerIndex].length - 1) {
historyStep[layerIndex]++;
const ctx = contexts[layerIndex];
ctx.putImageData(history[layerIndex][historyStep[layerIndex]], 0, 0);
updateButtons();
}
}
/**
* Uppdatera knappstatus baserat på aktivt lagers historik
*/
function updateButtons() {
const layerIndex = activeLayerIndex;
const undoBtn = document.getElementById('undoBtn');
const redoBtn = document.getElementById('redoBtn');
undoBtn.disabled = historyStep[layerIndex] <= 0;
redoBtn.disabled = historyStep[layerIndex] >= history[layerIndex].length - 1;
}
// ========================================
// DEBOUNCING OCH LADDNINGSSTATUS
// ========================================
/**
* Visar laddningsindikator
*/
function showLoadingIndicator() {
// Ändra muspekaren
document.body.style.cursor = 'wait';
layers.forEach(layer => layer.style.cursor = 'wait');
// Disable alla knappar
document.querySelectorAll('button').forEach(btn => {
btn.disabled = true;
btn.classList.add('opacity-50');
});
}
/**
* Döljer laddningsindikator
*/
function hideLoadingIndicator() {
document.body.style.cursor = 'default';
layers.forEach(layer => layer.style.cursor = 'crosshair');
// Enable alla knappar utom undo/redo (de har egen logik)
document.querySelectorAll('button').forEach(btn => {
btn.disabled = false;
btn.classList.remove('opacity-50');
});
updateButtons(); // Återställ undo/redo status
}
// ========================================
// INITIERING
// ========================================
/**
* Initierar alla lager och verktyg
*/
function initCanvas() {
contexts.forEach((ctx, index) => {
const canvas = layers[index];
// Grundinställningar
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
ctx.strokeStyle = getCurrentColor();
ctx.lineWidth = getCurrentBrushSize();
});
// Endast lager 0 (bakgrund) får vit bakgrund
ctx0.fillStyle = '#ffffff';
ctx0.fillRect(0, 0, layer0.width, layer0.height);
// Spara initialt tillstånd för varje lager
contexts.forEach((ctx, index) => {
activeLayerIndex = index;
saveState();
});
// Återställ till lager 0
activeLayerIndex = 0;
updateButtons();
}
// ========================================
// VERKTYGSHANTERING
// ========================================
/**
* Hämtar aktuell context (beroende på aktivt lager)
*/
function getActiveContext() {
return contexts[activeLayerIndex];
}
/**
* Byter aktivt verktyg med förbättrad visuell feedback
*/
function setTool(tool) {
currentTool = tool;
// Återställ alla knappar
document.querySelectorAll('.tool-btn').forEach(btn => {
btn.classList.remove('bg-blue-600', 'text-white', 'ring-4', 'ring-blue-300', 'shadow-lg', 'scale-105');
btn.classList.add('bg-gray-200', 'text-gray-700', 'shadow-sm');
});
const toolButtons = {
'brush': 'brushTool',
'eraser': 'eraserTool',
'rectangle': 'rectangleTool',
'circle': 'circleTool',
'triangle': 'triangleTool',
'fill': 'fillTool'
};
const activeBtn = document.getElementById(toolButtons[tool]);
if (activeBtn) {
activeBtn.classList.remove('bg-gray-200', 'text-gray-700', 'shadow-sm');
activeBtn.classList.add('bg-blue-600', 'text-white', 'ring-4', 'ring-blue-300', 'shadow-lg', 'scale-105', 'transform');
}
}
/**
* Byter aktivt lager med förbättrad visuell feedback
*/
function setActiveLayer(index) {
activeLayerIndex = index;
// Återställ alla lagerknappar
document.querySelectorAll('.layer-btn').forEach(btn => {
btn.classList.remove('bg-blue-600', 'text-white', 'ring-4', 'ring-blue-300', 'font-bold');
btn.classList.add('bg-gray-200', 'text-gray-700');
});
const layerBtn = document.getElementById(`layer${index}Btn`);
if (layerBtn) {
layerBtn.classList.remove('bg-gray-200', 'text-gray-700');
layerBtn.classList.add('bg-blue-600', 'text-white', 'ring-4', 'ring-blue-300', 'font-bold');
}
updateButtons();
}
// ========================================
// TOUCH-STÖD FÖR SURFPLATTOR
// ========================================
/**
* Normaliserar event-koordinater från både mus och touch
* Konverterar CSS-koordinater till canvas interna koordinater
*/
function getEventCoordinates(e, canvas) {
const rect = canvas.getBoundingClientRect();
let clientX, clientY;
if (e.type.startsWith('touch')) {
// Touch event
const touch = e.touches[0] || e.changedTouches[0];
clientX = touch.clientX;
clientY = touch.clientY;
} else {
// Mouse event
clientX = e.clientX;
clientY = e.clientY;
}
// Beräkna position relativt canvas
const cssX = clientX - rect.left;
const cssY = clientY - rect.top;
// Skala från CSS-koordinater till canvas interna koordinater
const scaleX = canvas.width / rect.width;
const scaleY = canvas.height / rect.height;
let x = cssX * scaleX;
let y = cssY * scaleY;
// Validera koordinater
x = sanitizeCoordinate(x, canvas.width);
y = sanitizeCoordinate(y, canvas.height);
return { x, y };
}
/**
* Unified start-funktion för både mus och touch
*/
function handleStart(e) {
// Förhindra default touch-beteende (scrollning)
if (e.type.startsWith('touch')) {
e.preventDefault();
}
const canvas = layers[activeLayerIndex];
const coords = getEventCoordinates(e, canvas);
const ctx = getActiveContext();
isDrawing = true;
startX = coords.x;
startY = coords.y;
lastX = coords.x;
lastY = coords.y;
if (currentTool === 'brush' || currentTool === 'eraser') {
if (currentTool === 'eraser') {
ctx.globalCompositeOperation = 'destination-out';
} else {
ctx.globalCompositeOperation = 'source-over';
}
ctx.beginPath();
ctx.arc(coords.x, coords.y, getCurrentBrushSize() / 2, 0, Math.PI * 2);
ctx.fillStyle = getCurrentColor();
ctx.fill();
} else if (currentTool === 'fill') {
// Kör flood fill asynkront
floodFill(coords.x, coords.y).then(() => {
saveState();
});
isDrawing = false;
}
}
/**
* Unified move-funktion för både mus och touch
*/
function handleMove(e) {
if (!isDrawing) return;
if (e.type.startsWith('touch')) {
e.preventDefault();
}
const canvas = layers[activeLayerIndex];
const coords = getEventCoordinates(e, canvas);
const ctx = getActiveContext();
if (currentTool === 'brush' || currentTool === 'eraser') {
ctx.beginPath();
ctx.moveTo(lastX, lastY);
ctx.lineTo(coords.x, coords.y);
ctx.stroke();
lastX = coords.x;
lastY = coords.y;
} else if (['rectangle', 'circle', 'triangle'].includes(currentTool)) {
drawShapePreview(coords.x, coords.y);
}
}
/**
* Unified stop-funktion för både mus och touch
*/
function handleStop(e) {
if (!isDrawing) return;
if (e.type.startsWith('touch')) {
e.preventDefault();
}
const canvas = layers[activeLayerIndex];
const coords = getEventCoordinates(e, canvas);
const ctx = getActiveContext();
if (currentTool === 'brush' || currentTool === 'eraser') {
ctx.globalCompositeOperation = 'source-over';
saveState();
} else if (['rectangle', 'circle', 'triangle'].includes(currentTool)) {
drawFinalShape(coords.x, coords.y);
tempCtx.clearRect(0, 0, tempCanvas.width, tempCanvas.height);
saveState();
}
isDrawing = false;
}
// ========================================
// FORMRITNING
// ========================================
/**
* Ritar förhandsvisning av form på temp canvas
*/
function drawShapePreview(currentX, currentY) {
// Rensa temp canvas
tempCtx.clearRect(0, 0, tempCanvas.width, tempCanvas.height);
// Kopiera stil från aktivt lager
tempCtx.strokeStyle = getCurrentColor();
tempCtx.lineWidth = getCurrentBrushSize();
tempCtx.lineCap = 'round';
tempCtx.lineJoin = 'round';
drawShape(tempCtx, startX, startY, currentX, currentY);
}
/**
* Ritar slutgiltig form på aktivt lager
*/
function drawFinalShape(currentX, currentY) {
const ctx = getActiveContext();
drawShape(ctx, startX, startY, currentX, currentY);
}
/**
* Ritar vald form på given context
*/
function drawShape(ctx, x1, y1, x2, y2) {
ctx.beginPath();
const width = x2 - x1;
const height = y2 - y1;
if (currentTool === 'rectangle') {
ctx.rect(x1, y1, width, height);
} else if (currentTool === 'circle') {
// Beräkna radie från diagonalen
const radius = Math.sqrt(width * width + height * height) / 2;
const centerX = x1 + width / 2;
const centerY = y1 + height / 2;
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2);
} else if (currentTool === 'triangle') {
// Triangel: topp i mitten, bas längst ner
const topX = x1 + width / 2;
const topY = y1;
const leftX = x1;
const leftY = y2;
const rightX = x2;
const rightY = y2;
ctx.moveTo(topX, topY);
ctx.lineTo(leftX, leftY);
ctx.lineTo(rightX, rightY);
ctx.closePath();
}
ctx.stroke();
}
// ========================================
// FLOOD FILL (HINK-VERKTYG)
// ========================================
/**
* Hjälpfunktion: Konvertera hex-färg till RGB
*/
function hexToRgb(hex) {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : { r: 0, g: 0, b: 0 };
}
/**
* Optimerad flood fill-algoritm med async processing
* Använder requestAnimationFrame för att inte blockera UI
*/
async function floodFill(startX, startY) {
// Förhindra flera samtidiga fills
if (isProcessing) {
console.log('Fyllning pågår redan, vänligen vänta...');
return;
}
isProcessing = true;
showLoadingIndicator();
const ctx = getActiveContext();
const canvas = layers[activeLayerIndex];
// Hämta bilddata
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const data = imageData.data;
startX = Math.floor(startX);
startY = Math.floor(startY);
const startIndex = (startY * canvas.width + startX) * 4;
const startR = data[startIndex];
const startG = data[startIndex + 1];
const startB = data[startIndex + 2];
const startA = data[startIndex + 3];
const fillColor = hexToRgb(getCurrentColor());
// Om färgen redan är samma, gör inget
if (startR === fillColor.r && startG === fillColor.g && startB === fillColor.b) {
isProcessing = false;
hideLoadingIndicator();
return;
}
const stack = [[startX, startY]];
const visited = new Set();
// Process i batches för att inte frysa UI
const BATCH_SIZE = 1000;
let processedCount = 0;
while (stack.length > 0) {
// Processa en batch
for (let i = 0; i < BATCH_SIZE && stack.length > 0; i++) {
const [x, y] = stack.pop();
if (x < 0 || x >= canvas.width || y < 0 || y >= canvas.height) continue;
const key = `${x},${y}`;
if (visited.has(key)) continue;
visited.add(key);
const index = (y * canvas.width + x) * 4;
const r = data[index];
const g = data[index + 1];
const b = data[index + 2];
const a = data[index + 3];
if (r !== startR || g !== startG || b !== startB || a !== startA) {
continue;
}
data[index] = fillColor.r;
data[index + 1] = fillColor.g;
data[index + 2] = fillColor.b;
data[index + 3] = 255;
stack.push([x + 1, y]);
stack.push([x - 1, y]);
stack.push([x, y + 1]);
stack.push([x, y - 1]);
processedCount++;
}
// Ge webbläsaren chans att uppdatera UI
if (stack.length > 0) {
await new Promise(resolve => requestAnimationFrame(resolve));
}
}
// Skriv tillbaka modifierad bilddata
ctx.putImageData(imageData, 0, 0);
isProcessing = false;
hideLoadingIndicator();
}
// ========================================
// RENSA CANVAS
// ========================================
/**
* Rensar aktivt lager
* Lager 0 (bakgrund) fylls med vitt
* Övriga lager rensas till transparent
*/
function clearCanvas() {
const ctx = getActiveContext();
const canvas = layers[activeLayerIndex];
if (activeLayerIndex === 0) {
// Bakgrundslager: fyll med vitt
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
} else {
// Övriga lager: rensa till transparent
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
saveState();
}
// ========================================
// BEKRÄFTELSEDIALOG FÖR RENSNING
// ========================================
/**
* Visar bekräftelsedialog innan rensning
*/
function showClearConfirmation() {
const modal = document.getElementById('confirmModal');
modal.classList.remove('hidden');
// Fokusera på "Nej"-knappen som standard (säkrare)
document.getElementById('confirmNo').focus();
}
/**
* Döljer bekräftelsedialogren
*/
function hideClearConfirmation() {
const modal = document.getElementById('confirmModal');
modal.classList.add('hidden');
}
/**
* Hanterar bekräftad rensning
*/
function handleConfirmedClear() {
clearCanvas();
hideClearConfirmation();
}
// ========================================
// SPARA MÅLNING
// ========================================
/**
* Sparar målning som PNG
* Kombinerar alla lager till en bild
*/
function savePainting() {
// Skapa temporärt canvas för att kombinera lager
const mergedCanvas = document.createElement('canvas');
mergedCanvas.width = layers[0].width;
mergedCanvas.height = layers[0].height;
const mergedCtx = mergedCanvas.getContext('2d');
// Rita alla lager på merged canvas
layers.forEach(layer => {
mergedCtx.drawImage(layer, 0, 0);
});
// Konvertera till data URL
const dataURL = mergedCanvas.toDataURL('image/png');
// Skapa nedladdningslänk
const link = document.createElement('a');
link.download = `målning_${Date.now()}.png`;
link.href = dataURL;
// Trigga nedladdning
link.click();
}
// ========================================
// EVENT LISTENERS - CANVAS (MUS + TOUCH)
// ========================================
layers.forEach(layer => {
// Mus-events
layer.addEventListener('mousedown', handleStart);
layer.addEventListener('mousemove', handleMove);
layer.addEventListener('mouseup', handleStop);
layer.addEventListener('mouseleave', handleStop);
// Touch-events
layer.addEventListener('touchstart', handleStart, { passive: false });
layer.addEventListener('touchmove', handleMove, { passive: false });
layer.addEventListener('touchend', handleStop, { passive: false });
layer.addEventListener('touchcancel', handleStop, { passive: false });
});
// ========================================
// EVENT LISTENERS - UI-KONTROLLER
// ========================================
// Färgväljare
document.getElementById('colorPicker').addEventListener('change', (e) => {
const sanitized = sanitizeColor(e.target.value);
currentColor = sanitized;
e.target.value = sanitized; // Uppdatera UI om värdet korrigerades
contexts.forEach(ctx => ctx.strokeStyle = sanitized);
});
// Penselstorlek
document.getElementById('brushSize').addEventListener('input', (e) => {
const sanitized = sanitizeBrushSize(e.target.value);
currentBrushSize = sanitized;
e.target.value = sanitized;
contexts.forEach(ctx => ctx.lineWidth = sanitized);
document.getElementById('brushSizeValue').textContent = sanitized + 'px';
});
// Verktygsknappar
document.getElementById('brushTool').addEventListener('click', () => setTool('brush'));
document.getElementById('eraserTool').addEventListener('click', () => setTool('eraser'));
document.getElementById('rectangleTool').addEventListener('click', () => setTool('rectangle'));
document.getElementById('circleTool').addEventListener('click', () => setTool('circle'));
document.getElementById('triangleTool').addEventListener('click', () => setTool('triangle'));
document.getElementById('fillTool').addEventListener('click', () => setTool('fill'));
// Lagerknappar
document.getElementById('layer0Btn').addEventListener('click', () => setActiveLayer(0));
document.getElementById('layer1Btn').addEventListener('click', () => setActiveLayer(1));
// Åtgärdsknappar
document.getElementById('clearBtn').addEventListener('click', showClearConfirmation);
document.getElementById('undoBtn').addEventListener('click', undo);
document.getElementById('redoBtn').addEventListener('click', redo);
document.getElementById('saveBtn').addEventListener('click', savePainting);
// Bekräftelsedialog-knappar
document.getElementById('confirmYes').addEventListener('click', handleConfirmedClear);
document.getElementById('confirmNo').addEventListener('click', hideClearConfirmation);
// Stäng modal vid klick utanför
document.getElementById('confirmModal').addEventListener('click', (e) => {
if (e.target.id === 'confirmModal') {
hideClearConfirmation();
}
});
// Responsiv toolbar-toggle
document.getElementById('toolbarToggle').addEventListener('click', () => {
const content = document.getElementById('toolbarContent');
const toggleText = document.getElementById('toolbarToggleText');
if (content.classList.contains('hidden')) {
content.classList.remove('hidden');
toggleText.textContent = 'Dölj verktyg ▲';
} else {
content.classList.add('hidden');
toggleText.textContent = 'Visa verktyg ▼';
}
});
// ========================================
// STARTA APPLIKATIONEN
// ========================================
initCanvas();