-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
695 lines (587 loc) · 26.7 KB
/
script.js
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
class PixelArt {
constructor() {
this.pixelSize = 20;
this.isDrawing = false;
this.currentColor = '#000000';
this.templates = {
heart: {
name: "Heart",
pixels: [
// Top curves
...[2,3,6,7].map(x => ({x, y: 0, color: '#FF1493'})),
...[1,2,3,4,5,6,7,8].map(x => ({x, y: 1, color: '#FF1493'})),
// Middle section
...[0,1,2,3,4,5,6,7,8,9].map(x => ({x, y: 2, color: '#FF1493'})),
...[0,1,2,3,4,5,6,7,8,9].map(x => ({x, y: 3, color: '#FF1493'})),
...[1,2,3,4,5,6,7,8].map(x => ({x, y: 4, color: '#FF1493'})),
// Bottom point
...[2,3,4,5,6,7].map(x => ({x, y: 5, color: '#FF1493'})),
...[3,4,5,6].map(x => ({x, y: 6, color: '#FF1493'})),
...[4,5].map(x => ({x, y: 7, color: '#FF1493'})),
// Highlights
...[2,3].map(x => ({x, y: 1, color: '#FFB6C1'})),
{x: 1, y: 2, color: '#FFB6C1'},
// Shadows
...[7,8].map(x => ({x, y: 3, color: '#DC143C'})),
...[6,7].map(x => ({x, y: 4, color: '#DC143C'})),
...[5,6].map(x => ({x, y: 5, color: '#DC143C'}))
]
},
ghost: {
name: "Blinky Ghost",
pixels: [
// Top curve
...[2,6].map(x => ({x, y: 0, color: '#00FF00'})),
...[1,2,3,4,5,6].map(x => ({x, y: 1, color: '#00FF00'})),
// Head
...[2,3,4,5,6].map(x => ({x, y: 2, color: '#00FF00'})),
// Middle section
...[1,2,3,4,5,6,7].map(x => ({x, y: 3, color: '#00FF00'})),
...[0,1,3,4,5,7,8].map(x => ({x, y: 4, color: '#00FF00'})),
...[0,1,2,3,4,5,6,7,8].map(x => ({x, y: 5, color: '#00FF00'})),
// Legs
...[1,2,6,7].map(x => ({x, y: 6, color: '#00FF00'})),
...[0,2,6,8].map(x => ({x, y: 7, color: '#00FF00'}))
]
},
mushroom: {
name: "Mushroom",
pixels: [
// Cap top
...[2,3,4,5].map(x => ({x, y: 0, color: '#FF0000'})),
...[1,2,3,4,5,6].map(x => ({x, y: 1, color: '#FF0000'})),
...[0,1,2,3,4,5,6,7].map(x => ({x, y: 2, color: '#FF0000'})),
...[0,1,2,3,4,5,6,7].map(x => ({x, y: 3, color: '#FF0000'})),
// Cap spots
{x: 2, y: 1, color: '#FFFFFF'},
{x: 5, y: 1, color: '#FFFFFF'},
{x: 1, y: 2, color: '#FFFFFF'},
{x: 4, y: 2, color: '#FFFFFF'},
{x: 6, y: 2, color: '#FFFFFF'},
// Stem
...[2,3,4,5].map(x => ({x, y: 4, color: '#FFE4B5'})),
...[2,3,4,5].map(x => ({x, y: 5, color: '#FFE4B5'})),
...[2,3,4,5].map(x => ({x, y: 6, color: '#FFE4B5'}))
]
},
star: {
name: "Star",
pixels: [
// Top point
{x: 7, y: 0, color: '#FFD700'},
...[6,7,8].map(x => ({x, y: 1, color: '#FFD700'})),
...[5,6,7,8,9].map(x => ({x, y: 2, color: '#FFD700'})),
// Upper middle section
...[3,4,5,6,7,8,9,10,11].map(x => ({x, y: 3, color: '#FFD700'})),
...[2,3,4,5,6,7,8,9,10,11,12].map(x => ({x, y: 4, color: '#FFD700'})),
// Middle section
...[1,2,3,4,5,6,7,8,9,10,11,12,13].map(x => ({x, y: 5, color: '#FFD700'})),
...[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14].map(x => ({x, y: 6, color: '#FFD700'})),
// Lower middle section
...[2,3,4,5,6,7,8,9,10,11,12].map(x => ({x, y: 7, color: '#FFD700'})),
...[3,4,5,6,7,8,9,10,11].map(x => ({x, y: 8, color: '#FFD700'})),
// Bottom points
...[4,5,6,8,9,10].map(x => ({x, y: 9, color: '#FFD700'})),
...[3,4,5,9,10,11].map(x => ({x, y: 10, color: '#FFD700'})),
...[2,3,4,10,11,12].map(x => ({x, y: 11, color: '#FFD700'})),
// Highlights
...[6,7,8].map(x => ({x, y: 2, color: '#FFED4A'})),
...[5,6,7].map(x => ({x, y: 3, color: '#FFED4A'})),
{x: 4, y: 4, color: '#FFED4A'},
// Shadows
...[11,12].map(x => ({x, y: 6, color: '#DAA520'})),
...[10,11].map(x => ({x, y: 7, color: '#DAA520'})),
...[9,10].map(x => ({x, y: 8, color: '#DAA520'}))
]
},
invader: {
name: "Space Invader",
pixels: [
// Antenna
...[2,6].map(x => ({x, y: 0, color: '#00FF00'})),
// Top row
...[3,5].map(x => ({x, y: 1, color: '#00FF00'})),
// Head
...[2,3,4,5,6].map(x => ({x, y: 2, color: '#00FF00'})),
// Middle section
...[1,2,3,4,5,6,7].map(x => ({x, y: 3, color: '#00FF00'})),
...[0,1,3,4,5,7,8].map(x => ({x, y: 4, color: '#00FF00'})),
...[0,1,2,3,4,5,6,7,8].map(x => ({x, y: 5, color: '#00FF00'})),
// Legs
...[1,2,6,7].map(x => ({x, y: 6, color: '#00FF00'})),
...[0,2,6,8].map(x => ({x, y: 7, color: '#00FF00'}))
]
},
sword: {
name: "Master Sword",
pixels: [
// Blade Tip
{x: 12, y: 0, color: '#C0C0C0'},
...[11,12,13].map(x => ({x, y: 1, color: '#C0C0C0'})),
...[10,11,12,13,14].map(x => ({x, y: 2, color: '#C0C0C0'})),
// Blade
...[11,12,13].map(x => ({x, y: 3, color: '#C0C0C0'})),
...[11,12,13].map(x => ({x, y: 4, color: '#C0C0C0'})),
...[11,12,13].map(x => ({x, y: 5, color: '#C0C0C0'})),
...[11,12,13].map(x => ({x, y: 6, color: '#C0C0C0'})),
...[11,12,13].map(x => ({x, y: 7, color: '#C0C0C0'})),
...[11,12,13].map(x => ({x, y: 8, color: '#C0C0C0'})),
...[11,12,13].map(x => ({x, y: 9, color: '#C0C0C0'})),
...[11,12,13].map(x => ({x, y: 10, color: '#C0C0C0'})),
// Blade Inner Detail
{x: 12, y: 3, color: '#E8E8E8'},
{x: 12, y: 4, color: '#E8E8E8'},
{x: 12, y: 5, color: '#E8E8E8'},
{x: 12, y: 6, color: '#E8E8E8'},
{x: 12, y: 7, color: '#E8E8E8'},
{x: 12, y: 8, color: '#E8E8E8'},
{x: 12, y: 9, color: '#E8E8E8'},
{x: 12, y: 10, color: '#E8E8E8'},
// Cross Guard
...[8,9,10,11,12,13,14,15,16].map(x => ({x, y: 11, color: '#FFD700'})),
...[9,10,11,12,13,14,15].map(x => ({x, y: 12, color: '#FFD700'})),
// Grip
...[11,12,13].map(x => ({x, y: 13, color: '#4169E1'})),
...[11,12,13].map(x => ({x, y: 14, color: '#4169E1'})),
...[11,12,13].map(x => ({x, y: 15, color: '#4169E1'})),
...[11,12,13].map(x => ({x, y: 16, color: '#4169E1'})),
// Pommel
...[10,11,12,13,14].map(x => ({x, y: 17, color: '#FFD700'})),
...[11,12,13].map(x => ({x, y: 18, color: '#FFD700'}))
]
}
};
this.currentTemplate = null;
this.width = 0;
this.height = 0;
this.drawingWidth = 0; // Width of the actual drawing area
this.drawingHeight = 0; // Height of the actual drawing area
this.initializeCanvases();
this.setupEventListeners();
this.createGrid();
this.setupTemplates();
this.createTemplatePalette();
}
initializeCanvases() {
this.pixelCanvas = document.getElementById('pixelCanvas');
this.templateCanvas = document.getElementById('templateCanvas');
this.pixelCtx = this.pixelCanvas.getContext('2d', { willReadFrequently: true });
this.templateCtx = this.templateCanvas.getContext('2d', { willReadFrequently: true });
this.resizeCanvases();
window.addEventListener('resize', () => this.resizeCanvases());
}
resizeCanvases() {
this.width = Math.floor(window.innerWidth / this.pixelSize) * this.pixelSize;
this.height = Math.floor(window.innerHeight / this.pixelSize) * this.pixelSize;
this.pixelCanvas.width = this.width;
this.pixelCanvas.height = this.height;
this.templateCanvas.width = this.width;
this.templateCanvas.height = this.height;
this.createGrid();
}
createGrid() {
// Fill with white background
this.pixelCtx.fillStyle = '#ffffff';
this.pixelCtx.fillRect(0, 0, this.width, this.height);
// Draw grid
this.pixelCtx.strokeStyle = '#e0e0e0';
this.pixelCtx.lineWidth = 0.5;
for (let x = 0; x <= this.width; x += this.pixelSize) {
this.pixelCtx.beginPath();
this.pixelCtx.moveTo(x, 0);
this.pixelCtx.lineTo(x, this.height);
this.pixelCtx.stroke();
}
for (let y = 0; y <= this.height; y += this.pixelSize) {
this.pixelCtx.beginPath();
this.pixelCtx.moveTo(0, y);
this.pixelCtx.lineTo(this.width, y);
this.pixelCtx.stroke();
}
}
setupEventListeners() {
this.pixelCanvas.addEventListener('mousedown', (e) => this.startDrawing(e));
this.pixelCanvas.addEventListener('mousemove', (e) => this.draw(e));
this.pixelCanvas.addEventListener('mouseup', () => this.stopDrawing());
this.pixelCanvas.addEventListener('mouseleave', () => this.stopDrawing());
document.getElementById('colorPicker').addEventListener('input', (e) => {
this.currentColor = e.target.value;
});
document.getElementById('templateBtn').addEventListener('click', () => {
const modal = document.getElementById('templateModal');
modal.style.display = 'block';
this.openTemplateModal();
});
document.getElementById('clearBtn').addEventListener('click', () => this.clearCanvas());
document.getElementById('exportBtn').addEventListener('click', () => {
document.getElementById('exportModal').style.display = 'block';
});
// Close buttons for modals
document.querySelectorAll('.modal .close').forEach(closeBtn => {
closeBtn.addEventListener('click', () => {
closeBtn.closest('.modal').style.display = 'none';
});
});
// Export buttons
document.querySelectorAll('.export-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
const format = e.target.dataset.format;
if (format === 'png') {
this.exportToPNG();
} else {
this.exportImage(format);
}
document.getElementById('exportModal').style.display = 'none';
});
});
// Keyboard shortcuts
document.addEventListener('keydown', (e) => {
if (e.key.toLowerCase() === 'r') {
this.clearCanvas();
}
});
}
getPixelCoords(e) {
const rect = this.pixelCanvas.getBoundingClientRect();
const x = Math.floor((e.clientX - rect.left) / this.pixelSize) * this.pixelSize;
const y = Math.floor((e.clientY - rect.top) / this.pixelSize) * this.pixelSize;
return { x, y };
}
startDrawing(e) {
this.isDrawing = true;
this.togglePixel(e);
}
draw(e) {
if (!this.isDrawing) return;
const coords = this.getPixelCoords(e);
// Draw the pixel
this.pixelCtx.fillStyle = this.currentColor;
this.pixelCtx.fillRect(coords.x, coords.y, this.pixelSize, this.pixelSize);
// Redraw grid lines for this pixel
this.pixelCtx.strokeStyle = '#e0e0e0';
this.pixelCtx.lineWidth = 0.5;
this.pixelCtx.strokeRect(coords.x, coords.y, this.pixelSize, this.pixelSize);
}
togglePixel(e) {
const coords = this.getPixelCoords(e);
const imageData = this.pixelCtx.getImageData(coords.x, coords.y, 1, 1).data;
const isWhite = imageData[0] === 255 && imageData[1] === 255 && imageData[2] === 255;
// Set color based on current state
this.pixelCtx.fillStyle = isWhite ? this.currentColor : '#ffffff';
this.pixelCtx.fillRect(coords.x, coords.y, this.pixelSize, this.pixelSize);
// Redraw grid lines for this pixel
this.pixelCtx.strokeStyle = '#e0e0e0';
this.pixelCtx.lineWidth = 0.5;
this.pixelCtx.strokeRect(coords.x, coords.y, this.pixelSize, this.pixelSize);
}
stopDrawing() {
this.isDrawing = false;
}
clearCanvas() {
this.pixelCtx.clearRect(0, 0, this.width, this.height);
this.templateCtx.clearRect(0, 0, this.width, this.height);
this.createGrid();
this.updateTemplatePalette(null);
}
setupTemplates() {
// No changes
}
createTemplatePalette() {
const palette = document.createElement('div');
palette.id = 'template-palette';
palette.style.cssText = `
position: absolute;
right: 20px;
top: 60px;
background: #2a2a2a;
border: 1px solid #444;
border-radius: 8px;
padding: 10px;
display: none;
box-shadow: 0 2px 10px rgba(0,0,0,0.3);
`;
const title = document.createElement('div');
title.textContent = 'Template Colors';
title.style.cssText = `
color: #fff;
font-size: 14px;
margin-bottom: 8px;
text-align: center;
`;
palette.appendChild(title);
const colorsContainer = document.createElement('div');
colorsContainer.id = 'template-colors';
colorsContainer.style.cssText = `
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 5px;
`;
palette.appendChild(colorsContainer);
document.body.appendChild(palette);
return palette;
}
updateTemplatePalette(template) {
const palette = document.getElementById('template-palette');
const colorsContainer = document.getElementById('template-colors');
colorsContainer.innerHTML = '';
if (!template) {
palette.style.display = 'none';
return;
}
// Extract unique colors from template
const colors = [...new Set(template.pixels.map(pixel => pixel.color))];
colors.forEach(color => {
const colorButton = document.createElement('div');
colorButton.style.cssText = `
width: 30px;
height: 30px;
background-color: ${color};
border-radius: 4px;
cursor: pointer;
border: 1px solid #444;
transition: transform 0.1s;
`;
colorButton.addEventListener('click', () => {
this.currentColor = color;
// Add visual feedback
colorButton.style.transform = 'scale(0.95)';
setTimeout(() => {
colorButton.style.transform = 'scale(1)';
}, 100);
});
colorButton.title = color;
colorsContainer.appendChild(colorButton);
});
palette.style.display = 'block';
}
openTemplateModal() {
const templateGrid = document.querySelector('.template-grid');
templateGrid.innerHTML = '';
Object.entries(this.templates).forEach(([key, template]) => {
const option = document.createElement('div');
option.className = 'template-option';
// Create preview container
const previewContainer = document.createElement('div');
previewContainer.className = 'template-preview';
// Create thumbnail canvas
const thumbnailCanvas = document.createElement('canvas');
thumbnailCanvas.width = 160;
thumbnailCanvas.height = 160;
const thumbCtx = thumbnailCanvas.getContext('2d');
// Clear canvas with white background
thumbCtx.fillStyle = '#ffffff';
thumbCtx.fillRect(0, 0, 160, 160);
// Find template dimensions
let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity;
template.pixels.forEach(pixel => {
minX = Math.min(minX, pixel.x);
maxX = Math.max(maxX, pixel.x);
minY = Math.min(minY, pixel.y);
maxY = Math.max(maxY, pixel.y);
});
const templateWidth = maxX - minX + 1;
const templateHeight = maxY - minY + 1;
const scale = Math.min(120 / templateWidth, 120 / templateHeight);
// Center the template in thumbnail
const offsetX = (160 - (templateWidth * scale)) / 2;
const offsetY = (160 - (templateHeight * scale)) / 2;
// Draw template pixels
template.pixels.forEach(pixel => {
thumbCtx.fillStyle = pixel.color;
thumbCtx.fillRect(
offsetX + ((pixel.x - minX) * scale),
offsetY + ((pixel.y - minY) * scale),
scale - 1,
scale - 1
);
});
// Add template name
const templateName = document.createElement('div');
templateName.className = 'template-name';
templateName.textContent = template.name;
// Assemble the elements
previewContainer.appendChild(thumbnailCanvas);
option.appendChild(previewContainer);
option.appendChild(templateName);
templateGrid.appendChild(option);
option.addEventListener('click', () => {
this.applyTemplate(key);
this.updateTemplatePalette(this.templates[key]);
document.getElementById('templateModal').style.display = 'none';
});
});
}
applyTemplate(templateName) {
const template = this.templates[templateName];
if (!template) return;
this.templateCtx.clearRect(0, 0, this.width, this.height);
// Find template dimensions
let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity;
template.pixels.forEach(pixel => {
minX = Math.min(minX, pixel.x);
maxX = Math.max(maxX, pixel.x);
minY = Math.min(minY, pixel.y);
maxY = Math.max(maxY, pixel.y);
});
const templateWidth = (maxX - minX + 1) * this.pixelSize;
const templateHeight = (maxY - minY + 1) * this.pixelSize;
// Calculate center position, ensuring alignment with pixel grid
const startX = Math.floor((this.width - templateWidth) / (2 * this.pixelSize)) * this.pixelSize;
const startY = Math.floor((this.height - templateHeight) / (2 * this.pixelSize)) * this.pixelSize;
// Draw template
this.templateCtx.globalAlpha = 0.5;
template.pixels.forEach(pixel => {
this.templateCtx.fillStyle = pixel.color;
this.templateCtx.fillRect(
startX + (pixel.x - minX) * this.pixelSize,
startY + (pixel.y - minY) * this.pixelSize,
this.pixelSize,
this.pixelSize
);
});
this.templateCtx.globalAlpha = 1;
}
exportToPNG() {
// Create a temporary canvas with the exact pixel dimensions
const tempCanvas = document.createElement('canvas');
tempCanvas.width = this.width;
tempCanvas.height = this.height;
const tempCtx = tempCanvas.getContext('2d', { willReadFrequently: true });
// Make sure we have a transparent background
tempCtx.clearRect(0, 0, this.width, this.height);
// Draw the pixel art
tempCtx.drawImage(this.pixelCanvas, 0, 0);
// Get the canvas data to find the bounds of drawn pixels
const imageData = tempCtx.getImageData(0, 0, this.width, this.height);
const data = imageData.data;
// Find the bounds of the colored pixels
let minX = this.width;
let minY = this.height;
let maxX = 0;
let maxY = 0;
let hasColoredPixels = false;
// Check each pixel in the canvas
for (let y = 0; y < this.height; y++) {
for (let x = 0; x < this.width; x++) {
const i = (y * this.width + x) * 4;
// Check if pixel is not white (RGB values not all 255)
if (data[i] !== 255 || data[i + 1] !== 255 || data[i + 2] !== 255) {
minX = Math.min(minX, x);
minY = Math.min(minY, y);
maxX = Math.max(maxX, x);
maxY = Math.max(maxY, y);
hasColoredPixels = true;
}
}
}
if (!hasColoredPixels) {
alert('No pixels to export! Draw something first.');
return;
}
// Create the final canvas with the cropped dimensions
const finalCanvas = document.createElement('canvas');
const width = maxX - minX + 1;
const height = maxY - minY + 1;
finalCanvas.width = width;
finalCanvas.height = height;
const finalCtx = finalCanvas.getContext('2d', { willReadFrequently: true });
// Draw the cropped region
finalCtx.drawImage(
tempCanvas,
minX, minY,
width, height,
0, 0,
width, height
);
// Create download link
const link = document.createElement('a');
link.download = 'pixel-art.png';
link.href = finalCanvas.toDataURL('image/png');
link.click();
}
exportImage(format) {
if (format === 'svg') {
this.exportToSVG();
return;
}
// For other formats (jpg)
const tempCanvas = document.createElement('canvas');
const tempCtx = tempCanvas.getContext('2d', { willReadFrequently: true });
tempCanvas.width = this.width;
tempCanvas.height = this.height;
// Draw white background for jpg
tempCtx.fillStyle = '#ffffff';
tempCtx.fillRect(0, 0, this.width, this.height);
// Draw the pixel art
tempCtx.drawImage(this.pixelCanvas, 0, 0);
// Create download link
const link = document.createElement('a');
link.download = `pixel-art.${format}`;
link.href = tempCanvas.toDataURL(`image/${format}`);
link.click();
}
exportToSVG() {
// Get the canvas data
const ctx = this.pixelCtx;
const imageData = ctx.getImageData(0, 0, this.width, this.height);
const data = imageData.data;
// Find the bounds of the non-white pixels
let minX = this.width;
let minY = this.height;
let maxX = 0;
let maxY = 0;
let hasColoredPixels = false;
// Check each pixel in the canvas
for (let y = 0; y < this.height; y++) {
for (let x = 0; x < this.width; x++) {
const i = (y * this.width + x) * 4;
// Check if pixel is not white (RGB values not all 255)
if (data[i] !== 255 || data[i + 1] !== 255 || data[i + 2] !== 255) {
minX = Math.min(minX, x);
minY = Math.min(minY, y);
maxX = Math.max(maxX, x);
maxY = Math.max(maxY, y);
hasColoredPixels = true;
}
}
}
if (!hasColoredPixels) {
alert('No pixels to export! Draw something first.');
return;
}
console.log('Bounds:', { minX, minY, maxX, maxY });
// Add a 1-pixel padding
minX = Math.max(0, minX - 1);
minY = Math.max(0, minY - 1);
maxX = Math.min(this.width - 1, maxX + 1);
maxY = Math.min(this.height - 1, maxY + 1);
// Calculate dimensions
const width = maxX - minX + 1;
const height = maxY - minY + 1;
// Create SVG content
let svgContent = `<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="${width}px" height="${height}px" viewBox="0 0 ${width} ${height}" version="1.1" xmlns="http://www.w3.org/2000/svg">`;
// Add colored pixels to SVG
for (let y = minY; y <= maxY; y++) {
for (let x = minX; x <= maxX; x++) {
const i = (y * this.width + x) * 4;
if (data[i] !== 255 || data[i + 1] !== 255 || data[i + 2] !== 255) {
const color = `rgb(${data[i]}, ${data[i + 1]}, ${data[i + 2]})`;
svgContent += `<rect x="${x - minX}" y="${y - minY}" width="1" height="1" fill="${color}" />`;
}
}
}
svgContent += '</svg>';
// Create download link with SVG content
const link = document.createElement('a');
link.download = 'pixel-art.svg';
const blob = new Blob([svgContent], { type: 'image/svg+xml' });
link.href = URL.createObjectURL(blob);
link.click();
URL.revokeObjectURL(link.href);
}
}
// Initialize when the page loads
window.addEventListener('load', () => {
new PixelArt();
});