-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1168 lines (989 loc) · 41.5 KB
/
Copy pathindex.html
File metadata and controls
1168 lines (989 loc) · 41.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pixel Art Generator</title>
<meta name="description" content="Create stunning pixel art online with our free Pixel Art Generator. Customize grid size, colors, and download your artwork instantly. No signup required!">
<meta name="keywords" content="pixel art generator, online pixel art maker, create pixel art, pixel art tool, pixel drawing, pixel grid editor, pixel art creator, pixel editor">
<meta name="robots" content="index, follow">
<meta property="og:title" content="Pixel Art Generator">
<meta property="og:description" content="Easily create and customize pixel art with our intuitive online Pixel Art Generator. Download your creations in one click.">
<meta property="og:type" content="website">
<meta property="og:url" content="https://pixelartconverter.com/pixel-art-generator">
<meta property="og:image" content="https://pixelartconverter.com/images/pixel-art-preview.png">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Pixel Art Generator">
<meta name="twitter:description" content="Draw pixel art effortlessly using our free and simple Pixel Art Generator. Ideal for game design and digital creativity.">
<meta name="twitter:image" content="https://pixelartconverter.com/images/pixel-art-preview.png">
<style>
:root {
--primary: #6d28d9;
--primary-dark: #5b21b6;
--primary-light: #8b5cf6;
--accent: #ec4899;
--background: #f9fafb;
--card: #ffffff;
--text: #111827;
--text-light: #6b7280;
--border: #e5e7eb;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
background-color: var(--background);
color: var(--text);
margin: 0;
padding: 0;
line-height: 1.6;
}
header {
background-color: var(--primary);
color: white;
text-align: center;
padding: 2rem 1rem;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}
h1 {
margin: 0;
font-size: 2.5rem;
}
.tagline {
font-size: 1.1rem;
margin-top: 0.5rem;
opacity: 0.9;
}
.container {
max-width: 1200px;
margin: 2rem auto;
padding: 0 1rem;
display: flex;
flex-direction: column;
gap: 2rem;
}
.panel {
background-color: var(--card);
border-radius: 0.5rem;
padding: 1.5rem;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}
.input-panel {
display: flex;
flex-direction: column;
gap: 1rem;
}
.controls {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1.5rem;
}
.control-group {
display: flex;
flex-direction: column;
gap: 0.75rem;
}
.control-group h3 {
margin: 0;
font-size: 1.1rem;
color: var(--primary);
}
.control-item {
display: flex;
flex-direction: column;
gap: 0.25rem;
}
label {
font-weight: 500;
font-size: 0.9rem;
color: var(--text-light);
}
select, input[type="range"], input[type="number"] {
padding: 0.5rem;
border: 1px solid var(--border);
border-radius: 0.25rem;
font-size: 1rem;
background-color: white;
}
input[type="range"] {
width: 100%;
}
.slider-container {
display: flex;
align-items: center;
gap: 1rem;
}
.slider-value {
min-width: 3rem;
text-align: center;
font-weight: 500;
}
.upload-zone {
border: 2px dashed var(--border);
padding: 2rem;
text-align: center;
border-radius: 0.5rem;
background-color: rgba(0, 0, 0, 0.02);
cursor: pointer;
transition: all 0.2s ease;
}
.upload-zone:hover {
border-color: var(--primary-light);
background-color: rgba(139, 92, 246, 0.05);
}
.upload-zone.active {
border-color: var(--primary);
background-color: rgba(139, 92, 246, 0.1);
}
#file-input {
display: none;
}
.upload-icon {
font-size: 2.5rem;
color: var(--primary);
margin-bottom: 1rem;
}
.button {
background-color: var(--primary);
color: white;
border: none;
padding: 0.75rem 1.5rem;
border-radius: 0.25rem;
font-size: 1rem;
font-weight: 500;
cursor: pointer;
transition: background-color 0.2s ease;
}
.button:hover {
background-color: var(--primary-dark);
}
.button:disabled {
background-color: var(--text-light);
cursor: not-allowed;
opacity: 0.7;
}
.button-secondary {
background-color: white;
color: var(--primary);
border: 1px solid var(--primary);
}
.button-secondary:hover {
background-color: rgba(109, 40, 217, 0.05);
}
.button-group {
display: flex;
gap: 1rem;
margin-top: 1rem;
}
.output-panel {
display: flex;
flex-direction: column;
gap: 1rem;
}
.canvas-container {
display: flex;
justify-content: center;
align-items: center;
gap: 2rem;
flex-wrap: wrap;
margin-top: 1rem;
}
.canvas-wrapper {
display: flex;
flex-direction: column;
gap: 0.5rem;
align-items: center;
}
.canvas-wrapper h3 {
margin: 0;
font-size: 1rem;
}
canvas {
max-width: 100%;
border: 1px solid var(--border);
background-color: white;
image-rendering: pixelated; /* Critical for pixel art */
image-rendering: crisp-edges;
}
.options {
display: flex;
justify-content: center;
gap: 1rem;
margin-top: 1rem;
}
@media (max-width: 768px) {
.controls {
grid-template-columns: 1fr;
}
.canvas-container {
flex-direction: column;
}
}
.loading {
display: none;
text-align: center;
margin: 1rem 0;
}
.spinner {
display: inline-block;
width: 2rem;
height: 2rem;
border: 3px solid rgba(109, 40, 217, 0.3);
border-radius: 50%;
border-top-color: var(--primary);
animation: spin 1s ease-in-out infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
/* Navigation Bar Styles */
.nav-bar {
background-color: white;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
position: sticky;
top: 0;
z-index: 1000;
}
.nav-container {
max-width: 1200px;
margin: 0 auto;
padding: 0.5rem 1rem;
display: flex;
justify-content: space-between;
align-items: center;
}
.nav-logo {
display: flex;
align-items: center;
font-size: 1.5rem;
font-weight: 700;
color: var(--primary);
text-decoration: none;
}
.nav-logo svg {
margin-right: 0.5rem;
}
.nav-links {
list-style: none;
display: flex;
gap: 1.5rem;
}
.nav-links a {
text-decoration: none;
color: var(--text);
font-weight: 500;
transition: color 0.2s ease;
}
.nav-links a:hover {
color: var(--primary);
}
/* Features Section */
.features-section {
padding: 4rem 1rem;
background-color: var(--background);
}
.section-title {
text-align: center;
font-size: 2.5rem;
margin-bottom: 2rem;
color: var(--primary);
}
.feature-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 2rem;
}
.feature-card {
background-color: var(--card);
border-radius: 0.5rem;
padding: 2rem;
text-align: center;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}
.feature-icon {
font-size: 3rem;
margin-bottom: 1rem;
}
/* FAQ Section */
.faq-section {
padding: 4rem 1rem;
background-color: var(--background);
}
.faq-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 2rem;
}
.faq-item {
background-color: var(--card);
border-radius: 0.5rem;
padding: 2rem;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}
.faq-item h3 {
margin: 0 0 1rem 0;
font-size: 1.2rem;
color: var(--primary);
}
/* Footer Styles */
.footer {
background-color: var(--primary);
color: white;
padding: 2rem 1rem;
margin-top: 2rem;
}
.footer-content {
max-width: 1200px;
margin: 0 auto;
display: grid;
grid-template-columns: 2fr 1fr 1fr 1fr; /* Adjust the first column to be wider */
gap: 3rem;
padding: 0 1rem;
}
/* Add media query for mobile devices */
@media (max-width: 768px) {
.footer-content {
grid-template-columns: 1fr; /* Stack vertically on mobile */
gap: 2rem;
}
}
/* Update other footer styles for better alignment */
.footer-section {
min-width: 0; /* Prevent overflow */
}
.footer-section h3 {
margin: 0 0 1rem 0;
font-size: 1.25rem;
font-weight: 600;
}
.footer-section h4 {
margin: 0 0 1rem 0;
font-size: 1.1rem;
font-weight: 600;
}
.footer-links {
margin: 0;
padding: 0;
}
.social-links {
display: flex;
gap: 1rem;
margin-top: 1rem;
}
.social-links a {
color: white;
font-size: 1.5rem;
transition: color 0.2s ease;
}
.social-links a:hover {
color: rgba(255, 255, 255, 0.7);
}
.copyright {
text-align: center;
margin-top: 2rem;
font-size: 0.875rem;
}
</style>
<link rel="me" href="https://www.blogger.com/profile/13995564742062554546" />
<meta name='google-adsense-platform-account' content='ca-host-pub-1556223355139109'/>
<meta name='google-adsense-platform-domain' content='blogspot.com'/>
<link rel="me" href="https://www.blogger.com/profile/16441879658146806408" />
<meta name='google-adsense-platform-account' content='ca-host-pub-1556223355139109'/>
<meta name='google-adsense-platform-domain' content='blogspot.com'/>
<link rel="me" href="https://www.blogger.com/profile/16441879658146806408" />
<meta name='google-adsense-platform-account' content='ca-host-pub-1556223355139109'/>
<meta name='google-adsense-platform-domain' content='blogspot.com'/>
</head>
<body>
<header>
<h1>Pixel Art Generator</h1>
<p class="tagline">Transform your images into awesome pixel art creations</p>
</header>
<nav class="nav-bar">
<div class="nav-container">
<a href="#" class="nav-logo">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor">
<rect x="3" y="3" width="18" height="18" rx="2" />
<rect x="7" y="7" width="4" width="4" fill="currentColor" />
<rect x="13" y="7" width="4" width="4" fill="currentColor" />
<rect x="7" y="13" width="4" width="4" fill="currentColor" />
<rect x="13" y="13" width="4" width="4" fill="currentColor" />
</svg>
PixelArtify
</a>
<ul class="nav-links">
<li><a href="#features">Features</a></li>
<li><a href="#how-it-works">How It Works</a></li>
</ul>
</div>
</nav>
<div class="container">
<div class="panel input-panel">
<div class="upload-zone" id="drop-zone">
<div class="upload-icon">📁</div>
<h2>Upload Your Image</h2>
<p>Click or drag & drop an image file here</p>
<input type="file" id="file-input" accept="image/*">
</div>
<div id="controls" class="controls" style="display: none;">
<div class="control-group">
<h3>Pixel Resolution</h3>
<div class="control-item">
<label for="pixel-size">Pixel Size</label>
<div class="slider-container">
<input type="range" id="pixel-size" min="1" max="20" value="1" step="1">
<span class="slider-value" id="pixel-size-value">3px</span>
</div>
</div>
</div>
<div class="control-group">
<h3>Color Options</h3>
<div class="control-item">
<label for="color-reduction">Color Palette Size</label>
<div class="slider-container">
<input type="range" id="color-reduction" min="2" max="64" value="64" step="1">
<span class="slider-value" id="color-reduction-value">16</span>
</div>
</div>
<div class="control-item">
<label for="dithering">Dithering</label>
<select id="dithering">
<option value="none">None</option>
<option value="floyd-steinberg" selected>Floyd-Steinberg</option>
<option value="ordered">Ordered</option>
</select>
</div>
</div>
<div class="control-group">
<h3>Output Size</h3>
<div class="control-item">
<label for="output-scale">Output Scale</label>
<div class="slider-container">
<input type="range" id="output-scale" min="1" max="5" value="5" step="1">
<span class="slider-value" id="output-scale-value">2x</span>
</div>
</div>
</div>
</div>
<div id="loading" class="loading">
<div class="spinner"></div>
<p>Converting your image...</p>
</div>
<div id="process-buttons" class="button-group" style="display: none;">
<button id="process-button" class="button">Generate Pixel Art</button>
<button id="reset-button" class="button button-secondary">Reset</button>
</div>
</div>
<div id="output-panel" class="panel output-panel" style="display: none;">
<h2>Your Pixel Art</h2>
<div class="canvas-container">
<div class="canvas-wrapper">
<h3>Original Image</h3>
<canvas id="original-canvas"></canvas>
</div>
<div class="canvas-wrapper">
<h3>Pixel Art</h3>
<canvas id="pixel-canvas"></canvas>
</div>
</div>
<div class="options">
<button id="download-button" class="button">Download as PNG</button>
</div>
</div>
<!-- Features Section -->
<section id="features" class="features-section">
<div class="container">
<h2 class="section-title">Features</h2>
<div class="feature-grid">
<div class="feature-card">
<div class="feature-icon">🎨</div>
<h3>Smart Color Reduction</h3>
<p>Intelligently reduces colors while maintaining image quality using advanced algorithms.</p>
</div>
<div class="feature-card">
<div class="feature-icon">✨</div>
<h3>Multiple Dithering Options</h3>
<p>Choose from various dithering methods to achieve your desired pixel art style.</p>
</div>
<div class="feature-card">
<div class="feature-icon">📏</div>
<h3>Customizable Resolution</h3>
<p>Adjust pixel size and output scale to create the perfect pixelated look.</p>
</div>
<div class="feature-card">
<div class="feature-icon">💾</div>
<h3>Easy Export</h3>
<p>Download your pixel art creations in high-quality PNG format.</p>
</div>
</div>
</div>
</section>
<!-- FAQ Section -->
<section id="faq" class="faq-section">
<div class="container">
<h2 class="section-title">Frequently Asked Questions</h2>
<div class="faq-grid">
<div class="faq-item">
<h3>What is pixel art?</h3>
<p>Pixel art is a form of digital art where images are created and edited at the pixel level, typically using a limited color palette.</p>
</div>
<div class="faq-item">
<h3>What file types are supported?</h3>
<p>Our tool supports common image formats including PNG, JPEG, GIF, and WebP files.</p>
</div>
<div class="faq-item">
<h3>What is dithering?</h3>
<p>Dithering is a technique used to create the illusion of color depth in images with a limited color palette.</p>
</div>
<div class="faq-item">
<h3>Is there a file size limit?</h3>
<p>Yes, we recommend using images under 5MB for optimal performance.</p>
</div>
</div>
</div>
</section>
</div>
<footer class="footer">
<div class="footer-content">
<div class="footer-section">
<h3>PixelArtify</h3>
<p>Transform your images into beautiful pixel art with our easy-to-use online tool.</p>
<div class="social-links">
<a href="#" aria-label="Twitter">
<svg width="24" height="24" fill="currentColor" viewBox="0 0 24 24">
<path d="M23 3a10.9 10.9 0 0 1-3.14 1.53 4.48 4.48 0 0 0-7.86 3v1A10.66 10.66 0 0 1 3 4s-4 9 5 13a11.64 11.64 0 0 1-7 2c9 5 20 0 20-11.5a4.5 4.5 0 0 0-.08-.83A7.72 7.72 0 0 0 23 3z"></path>
</svg>
</a>
<a href="#" aria-label="GitHub">
<svg width="24" height="24" fill="currentColor" viewBox="0 0 24 24">
<path d="M9 19c-5 1.5-5-2.5-7-3m14 6v-3.87a3.37 3.37 0 0 0-.94-2.61c3.14-.35 6.44-1.54 6.44-7A5.44 5.44 0 0 0 20 4.77 5.07 5.07 0 0 0 19.91 1S18.73.65 16 2.48a13.38 13.38 0 0 0-7 0C6.27.65 5.09 1 5.09 1A5.07 5.07 0 0 0 5 4.77a5.44 5.44 0 0 0-1.5 3.78c0 5.42 3.3 6.61 6.44 7A3.37 3.37 0 0 0 9 18.13V22"></path>
</svg>
</a>
</div>
</div>
<div class="footer-section">
<h4>Quick Links</h4>
<ul class="footer-links">
<li><a href="#features">Features</a></li>
<li><a href="#how-it-works">How It Works</a></li>
<li><a href="#faq">FAQ</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</div>
<div class="footer-section">
<h4>Resources</h4>
<ul class="footer-links">
<li><a href="#">Documentation</a></li>
<li><a href="#">Tutorials</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Support</a></li>
</ul>
</div>
<div class="footer-section">
<h4>Legal</h4>
<ul class="footer-links">
<li><a href="#">Privacy Policy</a></li>
<li><a href="#">Terms of Service</a></li>
<li><a href="#">Cookie Policy</a></li>
</ul>
</div>
</div>
<div class="copyright">
<p>© 2024 PixelArtify. All rights reserved.</p>
</div>
</footer>
<script>
// DOM Elements
const fileInput = document.getElementById('file-input');
const dropZone = document.getElementById('drop-zone');
const controls = document.getElementById('controls');
const processButtons = document.getElementById('process-buttons');
const processButton = document.getElementById('process-button');
const resetButton = document.getElementById('reset-button');
const outputPanel = document.getElementById('output-panel');
const originalCanvas = document.getElementById('original-canvas');
const pixelCanvas = document.getElementById('pixel-canvas');
const downloadButton = document.getElementById('download-button');
const loading = document.getElementById('loading');
// Sliders
const pixelSizeSlider = document.getElementById('pixel-size');
const pixelSizeValue = document.getElementById('pixel-size-value');
const colorReductionSlider = document.getElementById('color-reduction');
const colorReductionValue = document.getElementById('color-reduction-value');
const outputScaleSlider = document.getElementById('output-scale');
const outputScaleValue = document.getElementById('output-scale-value');
const ditheringSelect = document.getElementById('dithering');
// Global variables
let originalImage = null;
// Event listeners for drag and drop
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropZone.addEventListener(eventName, preventDefaults, false);
});
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
['dragenter', 'dragover'].forEach(eventName => {
dropZone.addEventListener(eventName, highlight, false);
});
['dragleave', 'drop'].forEach(eventName => {
dropZone.addEventListener(eventName, unhighlight, false);
});
function highlight() {
dropZone.classList.add('active');
}
function unhighlight() {
dropZone.classList.remove('active');
}
dropZone.addEventListener('drop', handleDrop, false);
function handleDrop(e) {
const dt = e.dataTransfer;
const files = dt.files;
if (files.length) {
fileInput.files = files;
handleFiles(files);
}
}
// Click to upload
dropZone.addEventListener('click', () => {
fileInput.click();
});
fileInput.addEventListener('change', () => {
if (fileInput.files.length) {
handleFiles(fileInput.files);
}
});
// Handle the uploaded files
function handleFiles(files) {
const file = files[0];
if (file.type.match('image.*')) {
const reader = new FileReader();
reader.onload = function(e) {
loadImage(e.target.result);
};
reader.readAsDataURL(file);
} else {
alert('Please upload an image file');
}
}
// Load image from data URL
function loadImage(dataUrl) {
const img = new Image();
img.onload = function() {
originalImage = img;
drawOriginalImage();
controls.style.display = 'grid';
processButtons.style.display = 'flex';
};
img.src = dataUrl;
}
// Draw the original image on canvas
function drawOriginalImage() {
const ctx = originalCanvas.getContext('2d');
// Calculate dimensions while maintaining aspect ratio
const maxWidth = 400;
const maxHeight = 400;
let width = originalImage.width;
let height = originalImage.height;
if (width > height) {
if (width > maxWidth) {
height = height * (maxWidth / width);
width = maxWidth;
}
} else {
if (height > maxHeight) {
width = width * (maxHeight / height);
height = maxHeight;
}
}
originalCanvas.width = width;
originalCanvas.height = height;
ctx.drawImage(originalImage, 0, 0, width, height);
}
// Update slider value displays
pixelSizeSlider.addEventListener('input', () => {
pixelSizeValue.textContent = `${pixelSizeSlider.value}px`;
});
colorReductionSlider.addEventListener('input', () => {
colorReductionValue.textContent = colorReductionSlider.value;
});
outputScaleSlider.addEventListener('input', () => {
outputScaleValue.textContent = `${outputScaleSlider.value}x`;
});
// Process button click handler
processButton.addEventListener('click', processImage);
function processImage() {
if (!originalImage) return;
loading.style.display = 'block';
// Use setTimeout to allow the UI to update before the heavy processing
setTimeout(() => {
const pixelSize = parseInt(pixelSizeSlider.value);
const colorCount = parseInt(colorReductionSlider.value);
const outputScale = parseInt(outputScaleSlider.value);
const ditheringMethod = ditheringSelect.value;
// Create pixelated image
createPixelArt(pixelSize, colorCount, outputScale, ditheringMethod);
// Show output panel
outputPanel.style.display = 'block';
loading.style.display = 'none';
// Scroll to output
outputPanel.scrollIntoView({ behavior: 'smooth' });
}, 50);
}
// Reset button handler
resetButton.addEventListener('click', () => {
originalImage = null;
fileInput.value = '';
controls.style.display = 'none';
processButtons.style.display = 'none';
outputPanel.style.display = 'none';
});
// Download button handler
downloadButton.addEventListener('click', () => {
const link = document.createElement('a');
link.download = 'pixel-art.png';
link.href = pixelCanvas.toDataURL('image/png');
link.click();
});
// Create pixel art from original image
function createPixelArt(pixelSize, colorCount, outputScale, ditheringMethod) {
// Calculate the downscaled dimensions
const scaledWidth = Math.floor(originalCanvas.width / pixelSize);
const scaledHeight = Math.floor(originalCanvas.height / pixelSize);
// Create a temporary canvas for the downscaled image
const tempCanvas = document.createElement('canvas');
tempCanvas.width = scaledWidth;
tempCanvas.height = scaledHeight;
const tempCtx = tempCanvas.getContext('2d');
// Draw the downscaled image
tempCtx.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight);
// Get the pixel data
const imageData = tempCtx.getImageData(0, 0, scaledWidth, scaledHeight);
const pixels = imageData.data;
// Generate a color palette
const palette = generatePalette(imageData, colorCount);
// Apply color mapping and dithering
if (ditheringMethod === 'floyd-steinberg') {
applyFloydSteinbergDithering(imageData, palette);
} else if (ditheringMethod === 'ordered') {
applyOrderedDithering(imageData, palette);
} else {
applyNearestColorMapping(imageData, palette);
}
// Put the processed image data back
tempCtx.putImageData(imageData, 0, 0);
// Set the output canvas size
pixelCanvas.width = scaledWidth * outputScale;
pixelCanvas.height = scaledHeight * outputScale;
const pixelCtx = pixelCanvas.getContext('2d');
// Draw the pixelated and scaled image
pixelCtx.imageSmoothingEnabled = false; // Disable anti-aliasing
pixelCtx.drawImage(tempCanvas, 0, 0, pixelCanvas.width, pixelCanvas.height);
}
// Generate a color palette using median cut algorithm
function generatePalette(imageData, colorCount) {
const pixels = imageData.data;
const colors = [];
// Extract unique colors
for (let i = 0; i < pixels.length; i += 4) {
const r = pixels[i];
const g = pixels[i + 1];
const b = pixels[i + 2];
// Skip transparent pixels
if (pixels[i + 3] < 128) continue;
colors.push([r, g, b]);
}
// If we have fewer unique colors than requested, return those
if (colors.length <= colorCount) {
return colors;
}
// Use a simplified quantization approach for this demo
// We'll use K-means clustering
return kMeansQuantization(colors, colorCount);
}
// K-means quantization algorithm
function kMeansQuantization(colors, k) {
if (colors.length === 0) return [];
// Initialize centroids randomly from the colors
const centroids = [];
const colorsCopy = [...colors];
// Pick initial centroids with some distance between them
for (let i = 0; i < k && colorsCopy.length > 0; i++) {
const randomIndex = Math.floor(Math.random() * colorsCopy.length);
centroids.push([...colorsCopy[randomIndex]]);
colorsCopy.splice(randomIndex, 1);
}
// If we couldn't get enough centroids, duplicate some
while (centroids.length < k) {
centroids.push([...centroids[Math.floor(Math.random() * centroids.length)]]);
}
const MAX_ITERATIONS = 10;
let iterations = 0;
let changed = true;
while (changed && iterations < MAX_ITERATIONS) {
// Reset clusters
const clusters = Array(k).fill().map(() => []);
// Assign colors to nearest centroid
for (const color of colors) {
let minDist = Infinity;
let closestCentroid = 0;
for (let i = 0; i < centroids.length; i++) {
const dist = colorDistance(color, centroids[i]);
if (dist < minDist) {
minDist = dist;
closestCentroid = i;
}
}
clusters[closestCentroid].push(color);
}
// Update centroids
changed = false;
for (let i = 0; i < k; i++) {
if (clusters[i].length === 0) continue;
const newCentroid = calculateCentroid(clusters[i]);
if (colorDistance(newCentroid, centroids[i]) > 1) {
centroids[i] = newCentroid;
changed = true;
}
}
iterations++;
}
return centroids;
}
// Calculate the centroid of a cluster
function calculateCentroid(cluster) {
const sum = [0, 0, 0];