-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1073 lines (921 loc) · 37 KB
/
script.js
File metadata and controls
1073 lines (921 loc) · 37 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
// ============================================================================
// IMAGE STYLER - Image to ASCII Art / Paint Style Converter
// ============================================================================
// A lightweight image processing tool supporting two conversion modes:
// 1. ASCII Art: Converts image to text-based ASCII characters
// 2. Paint Style: Applies artistic effects (pixelation, brush, watercolor, etc.)
//
// Architecture:
// - Dual Canvas System:
// * originalCanvas: Pristine, unmodified copy of loaded image (conversion source)
// * exportCanvas: Working copy showing current conversion result
// * outputCanvas: 800x600 preview canvas (letterboxed display)
// - History Stack: Tracks up to 12 conversion states for undo functionality
// - State Management: Single global loadedImage + canvas-based image storage
// - No dependencies: Pure HTML5/Canvas/JavaScript
//
// CRITICAL: Always read from originalCanvas for conversions to prevent
// repeated conversions from degrading the image.
// ============================================================================
// ============================================================================
// DOM ELEMENTS & CONSTANTS
// ============================================================================
// Input/Output Elements
const fileElem = document.getElementById('fileElem');
const dropArea = document.getElementById('drop-area');
const modeSelect = document.getElementById('modeSelect');
const convertBtn = document.getElementById('convertBtn');
const downloadBtn = document.getElementById('downloadBtn');
// Preview Canvas (800x600 display area with letterboxing)
const outputCanvas = document.getElementById('outputCanvas');
const ctx = outputCanvas.getContext('2d');
// Make the canvas DPI-aware and responsive. We'll resize the canvas
// internal pixel buffer to match its CSS size * devicePixelRatio and
// set the drawing transform so coordinates map to CSS pixels.
function resizeOutputCanvas() {
const dpr = window.devicePixelRatio || 1;
const rect = outputCanvas.getBoundingClientRect();
const cssW = Math.max(200, Math.round(rect.width));
const cssH = Math.max(150, Math.round(rect.height));
// Set internal pixel size
outputCanvas.width = Math.max(1, Math.round(cssW * dpr));
outputCanvas.height = Math.max(1, Math.round(cssH * dpr));
// Ensure CSS size is explicit to avoid layout jitter
outputCanvas.style.width = cssW + 'px';
outputCanvas.style.height = cssH + 'px';
// Map drawing coordinates to CSS pixels
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
}
// Resize on load and on window resize to keep UI sharp and consistent
window.addEventListener('resize', () => {
try { resizeOutputCanvas(); renderPreviewFromExport(); } catch (e) { /* ignore */ }
});
// Initial placeholder in preview
try {
resizeOutputCanvas();
const PREVIEW_W = outputCanvas.clientWidth || 800;
const PREVIEW_H = outputCanvas.clientHeight || 600;
ctx.fillStyle = '#f0f0f0';
ctx.fillRect(0, 0, PREVIEW_W, PREVIEW_H);
ctx.fillStyle = '#999';
ctx.font = '18px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText('📁 Upload an image to get started', PREVIEW_W / 2, PREVIEW_H / 2);
} catch (e) {
console.warn('Could not draw placeholder preview', e);
}
// Working canvas: stores conversion output being previewed
const exportCanvas = document.createElement('canvas');
const exportCtx = exportCanvas.getContext('2d');
// CRITICAL: Pristine source canvas - NEVER MODIFIED after initial load
// Always read from this for conversions to prevent degradation on repeated calls
const originalCanvas = document.createElement('canvas');
const originalCtx = originalCanvas.getContext('2d');
// UI Controls - Settings and Sliders
const fontSizeInput = document.getElementById('fontSize');
const pixelSizeInput = document.getElementById('pixelSize');
const resetBtn = document.getElementById('resetBtn');
const fontSizeValue = document.getElementById('fontSizeValue');
const pixelSizeValue = document.getElementById('pixelSizeValue');
const asciiColsInput = document.getElementById('asciiCols');
let lastAsciiOutput = '';
const copyAsciiBtn = document.getElementById('copyAsciiBtn');
const previewAsciiBtn = document.getElementById('previewAsciiBtn');
const undoBtn = document.getElementById('undoBtn');
// ASCII character set: ordered from darkest to lightest
// Used to map image brightness to characters
const ASCII_CHARS = '@%#*+=-:. ';
// Application State
let loadedImage = null; // Current loaded image element
let originalImageData = null; // Base64 PNG of original for reset
let asciiConversionCount = 0; // Counter for secret easter egg (triggers at 10)
const historyStack = []; // Undo stack: stores PNG data URLs
const MAX_HISTORY = 12; // Maximum undo states
// ============================================================================
// EVENT LISTENERS & HANDLERS
// ============================================================================
// Prevent default drag/drop behavior on the drop area
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
// Drag and drop events
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, preventDefaults, false);
});
// Visual feedback for drag-over state
dropArea.addEventListener('dragover', () => dropArea.classList.add('highlight'));
dropArea.addEventListener('dragleave', () => dropArea.classList.remove('highlight'));
// Handle dropped files
dropArea.addEventListener('drop', (e) => {
dropArea.classList.remove('highlight');
const dt = e.dataTransfer;
if (!dt) return;
const file = dt.files && dt.files[0];
if (file) handleFile(file);
});
// Handle file input selection
fileElem.addEventListener('change', (e) => {
const f = e.target.files && e.target.files[0];
if (f) handleFile(f);
});
// Tab switching for settings
const tabBtns = document.querySelectorAll('.tab-btn');
const tabContents = document.querySelectorAll('.tab-content');
tabBtns.forEach(btn => {
btn.addEventListener('click', () => {
const tab = btn.getAttribute('data-tab');
// Deactivate all tabs
tabBtns.forEach(b => b.classList.remove('active'));
tabContents.forEach(c => c.classList.remove('active'));
// Activate selected tab
btn.classList.add('active');
document.getElementById(tab + '-tab').classList.add('active');
// Update mode selector to match tab
modeSelect.value = tab;
modeSelect.dispatchEvent(new Event('change'));
});
});
// Mode selector: sync with tab switching
modeSelect.addEventListener('change', () => {
const mode = modeSelect.value;
// Update tab buttons
tabBtns.forEach(btn => {
btn.classList.toggle('active', btn.getAttribute('data-tab') === mode);
});
// Update tab contents
tabContents.forEach(content => {
content.classList.toggle('active', content.id === mode + '-tab');
});
});
// Main action buttons
convertBtn.addEventListener('click', convert);
// Show modal dialog to choose filename before downloading
downloadBtn.addEventListener('click', showDownloadDialog);
resetBtn.addEventListener('click', resetToOriginal);
// Slider value displays
if (fontSizeInput) fontSizeInput.addEventListener('input', (e) => { if (fontSizeValue) fontSizeValue.textContent = e.target.value; });
if (pixelSizeInput) pixelSizeInput.addEventListener('input', (e) => { if (pixelSizeValue) pixelSizeValue.textContent = e.target.value; });
// ASCII Control: Copy the full ASCII output stored in `lastAsciiOutput`
if (copyAsciiBtn) copyAsciiBtn.addEventListener('click', async () => {
try { await navigator.clipboard.writeText(lastAsciiOutput || ''); alert('ASCII copied to clipboard'); }
catch (e) { console.error('Copy failed', e); alert('Copy failed: ' + e.message); }
});
// ASCII preview modal handlers
const asciiPreviewModal = document.getElementById('asciiPreviewModal');
const asciiPreviewText = document.getElementById('asciiPreviewText');
const asciiPreviewCopyBtn = document.getElementById('asciiPreviewCopyBtn');
const asciiPreviewCloseBtn = document.getElementById('asciiPreviewCloseBtn');
function showAsciiPreview() {
if (!asciiPreviewModal) return;
asciiPreviewText.textContent = lastAsciiOutput || 'No ASCII generated yet. Click Convert to generate.';
asciiPreviewModal.classList.remove('hidden');
asciiPreviewModal.setAttribute('aria-hidden', 'false');
setTimeout(() => {
asciiPreviewText.focus();
fitAsciiFont();
}, 50);
}
function closeAsciiPreview() {
if (!asciiPreviewModal) return;
asciiPreviewModal.classList.add('hidden');
asciiPreviewModal.setAttribute('aria-hidden', 'true');
previewAsciiBtn && previewAsciiBtn.focus();
}
// Adjust font-size of the ASCII preview to fit the modal while preserving aspect ratio
function fitAsciiFont() {
if (!asciiPreviewText) return;
const text = lastAsciiOutput || '';
const lines = text.split('\n');
const rows = Math.max(1, lines.length);
let cols = 0;
for (let l of lines) if (l.length > cols) cols = l.length;
// If no content, reset to default
if (cols === 0 || rows === 0) {
asciiPreviewText.style.fontSize = '';
asciiPreviewText.style.lineHeight = '';
return;
}
// Measure monospace character metrics at a baseline font size
const baseline = 12; // px
const meas = document.createElement('span');
meas.style.fontFamily = getComputedStyle(asciiPreviewText).fontFamily || "'Courier New', monospace";
meas.style.fontSize = baseline + 'px';
meas.style.position = 'absolute';
meas.style.visibility = 'hidden';
meas.style.whiteSpace = 'nowrap';
meas.textContent = 'M';
document.body.appendChild(meas);
const charW = meas.getBoundingClientRect().width; // px at baseline
const charH = meas.getBoundingClientRect().height; // px at baseline
meas.remove();
if (charW <= 0 || charH <= 0) return;
const kW = charW / baseline; // width per font-size unit
const kH = charH / baseline; // height per font-size unit
// Available drawing area inside the pre element
const padX = parseFloat(getComputedStyle(asciiPreviewText).paddingLeft || 12) + parseFloat(getComputedStyle(asciiPreviewText).paddingRight || 12);
const padY = parseFloat(getComputedStyle(asciiPreviewText).paddingTop || 12) + parseFloat(getComputedStyle(asciiPreviewText).paddingBottom || 12);
const availW = Math.max(20, asciiPreviewText.clientWidth - padX);
const availH = Math.max(20, asciiPreviewText.clientHeight - padY);
// Compute font-size that fits cols and rows
const sizeW = availW / (cols * kW);
const sizeH = availH / (rows * kH);
let fontSize = Math.floor(Math.min(sizeW, sizeH));
// Clamp to sensible range
fontSize = Math.max(6, Math.min(28, fontSize));
asciiPreviewText.style.fontSize = fontSize + 'px';
asciiPreviewText.style.lineHeight = Math.max(1, (kH * fontSize) / (kW * fontSize)) + '';
}
// Refit on window resize while modal open
window.addEventListener('resize', () => {
if (asciiPreviewModal && !asciiPreviewModal.classList.contains('hidden')) {
fitAsciiFont();
}
});
if (previewAsciiBtn) previewAsciiBtn.addEventListener('click', showAsciiPreview);
if (asciiPreviewCopyBtn) asciiPreviewCopyBtn.addEventListener('click', async () => {
try { await navigator.clipboard.writeText(lastAsciiOutput || ''); alert('ASCII copied to clipboard'); }
catch (e) { console.error('Copy failed', e); alert('Copy failed: ' + e.message); }
});
if (asciiPreviewCloseBtn) asciiPreviewCloseBtn.addEventListener('click', closeAsciiPreview);
// Do not close ascii preview modal by clicking outside; require explicit Close action.
// Undo button: restore previous conversion state
if (undoBtn) undoBtn.addEventListener('click', () => {
if (historyStack.length === 0) return;
const prev = historyStack.pop();
const img = new Image();
img.onload = () => {
exportCanvas.width = img.width; exportCanvas.height = img.height;
exportCtx.clearRect(0,0,img.width,img.height);
exportCtx.drawImage(img,0,0);
renderPreviewFromExport();
if (historyStack.length === 0) undoBtn.disabled = true;
};
img.src = prev;
});
// ---------------------------
// Download modal behavior
// ---------------------------
const downloadModal = document.getElementById('downloadModal');
const downloadNameInput = document.getElementById('downloadName');
const downloadNowBtn = document.getElementById('downloadNowBtn');
const downloadCancelBtn = document.getElementById('downloadCancelBtn');
function showDownloadDialog() {
if (!downloadModal) {
// fallback: immediate download
downloadImage();
return;
}
downloadNameInput.value = downloadNameInput.value || 'converted.png';
downloadModal.classList.remove('hidden');
downloadModal.setAttribute('aria-hidden', 'false');
// focus and select filename for quick edit
setTimeout(() => {
downloadNameInput.focus();
downloadNameInput.select();
}, 50);
}
function closeDownloadDialog() {
if (!downloadModal) return;
downloadModal.classList.add('hidden');
downloadModal.setAttribute('aria-hidden', 'true');
downloadBtn.focus();
}
function downloadImageWithName(fileName) {
try {
const url = exportCanvas.toDataURL('image/png');
const a = document.createElement('a');
a.href = url;
a.download = fileName || 'converted.png';
document.body.appendChild(a);
a.click();
a.remove();
} catch (e) {
console.error('Download failed', e);
// fallback to original download behavior
downloadImage();
}
closeDownloadDialog();
}
if (downloadNowBtn) {
downloadNowBtn.addEventListener('click', () => {
let name = (downloadNameInput && downloadNameInput.value) || 'converted.png';
name = name.trim() || 'converted.png';
if (!/\.[a-zA-Z0-9]{1,5}$/.test(name)) name += '.png';
downloadImageWithName(name);
});
}
if (downloadCancelBtn) downloadCancelBtn.addEventListener('click', closeDownloadDialog);
// Do not close download modal by clicking outside; require explicit Cancel/Close.
// Keyboard support: Enter to download, Esc to cancel
document.addEventListener('keydown', (e) => {
if (!downloadModal || downloadModal.classList.contains('hidden')) return;
if (e.key === 'Escape') closeDownloadDialog();
if (e.key === 'Enter' && document.activeElement === downloadNameInput) {
downloadNowBtn && downloadNowBtn.click();
}
});
// ============================================================================
// PREVIEW RENDERING
// ============================================================================
/**
* Render the export canvas into the preview canvas with proper letterboxing.
* Scales the image to fit within the preview dimensions while maintaining aspect ratio.
* Centers the image and fills remaining space with white background.
* @returns {void}
*/
function renderPreviewFromExport() {
const startTime = performance.now();
console.log('renderPreviewFromExport called, exportCanvas:', exportCanvas.width, 'x', exportCanvas.height);
try {
// Ensure output canvas internal size matches its CSS size and dpr
resizeOutputCanvas();
const PREVIEW_W = outputCanvas.clientWidth || 800;
const PREVIEW_H = outputCanvas.clientHeight || 600;
ctx.clearRect(0, 0, PREVIEW_W, PREVIEW_H);
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, PREVIEW_W, PREVIEW_H);
if (exportCanvas.width === 0 || exportCanvas.height === 0) {
console.warn('Export canvas has no dimensions, skipping draw');
return;
}
// Use 'contain' scaling so the entire image is visible and not cropped.
// Apply a small padding factor so the image doesn't touch container edges.
const srcW = exportCanvas.width || exportCanvas.clientWidth || 1;
const srcH = exportCanvas.height || exportCanvas.clientHeight || 1;
const paddingFactor = 0.95; // leave slight margin inside preview
let scale = Math.min(PREVIEW_W / srcW, PREVIEW_H / srcH);
// Prevent excessive upscaling on very small images; cap scale to 1 (no upscale)
scale = Math.min(scale, 1) * paddingFactor;
const dw = Math.round(srcW * scale);
const dh = Math.round(srcH * scale);
const dx = Math.round((PREVIEW_W - dw) / 2);
const dy = Math.round((PREVIEW_H - dh) / 2);
console.log('Drawing preview (contain) at', dx, dy, 'size', dw, 'x', dh, 'scale', scale.toFixed(2));
ctx.drawImage(exportCanvas, 0, 0, srcW, srcH, dx, dy, dw, dh);
const endTime = performance.now();
console.log('renderPreviewFromExport completed in', (endTime - startTime).toFixed(2), 'ms');
} catch (error) {
console.error('Error in renderPreviewFromExport:', error);
}
}
// animateGifPreview removed (no animated GIF support)
// ============================================================================
// IMAGE LOADING
// ============================================================================
/**
* Handle file upload - validates and reads image file as DataURL.
* Clears conversion history and loads the image for processing.
* Stores image in localStorage for persistence (static image only).
* @param {File} file - The uploaded image file
* @returns {void}
*/
function handleFile(file) {
console.log('=== handleFile START ===');
console.log('File details:', {
name: file.name,
type: file.type,
size: file.size,
lastModified: file.lastModified
});
// Validate file
if (!file.type.startsWith('image/')) {
console.warn('File validation failed: not an image type');
alert('Please upload an image file (JPG, PNG, etc.)');
console.warn('Invalid file type:', file.type);
return;
}
console.log('File type validation passed');
const reader = new FileReader();
reader.onerror = () => {
console.error('FileReader error:', reader.error);
alert('Error reading file: ' + reader.error);
downloadBtn.disabled = true;
};
reader.onload = (e) => {
try {
const dataURL = e.target.result;
// Clear history when loading a new image
historyStack.length = 0;
undoBtn.disabled = true;
loadStaticImage(dataURL);
// Save to localStorage (static image only)
try {
localStorage.setItem('savedImage', dataURL);
console.log('Image saved to localStorage');
} catch (e) {
console.warn('Could not save to localStorage', e);
}
} catch (error) {
console.error('Error in onload handler:', error, error.stack);
alert('Error processing image: ' + error.message);
downloadBtn.disabled = true;
}
};
try {
reader.readAsDataURL(file);
} catch (error) {
console.error('Error calling readAsDataURL:', error);
alert('Error reading file: ' + error.message);
downloadBtn.disabled = true;
}
console.log('=== handleFile END ===');
}
/**
* Load a static image from DataURL.
* Creates both exportCanvas (for conversion output) and originalCanvas (pristine source).
* Stores original image data for reset functionality.
* Renders preview and enables download button.
* @param {string} dataURL - Base64-encoded image data URL
* @returns {void}
*/
function loadStaticImage(dataURL) {
console.log('loadStaticImage called');
// Clear undo history when loading new image handled elsewhere
const img = new Image();
img.crossOrigin = 'anonymous';
img.onload = () => {
try {
console.log('Image onload fired, dimensions:', img.width, 'x', img.height);
if (img.width === 0 || img.height === 0) {
console.error('Invalid image dimensions:', img.width, 'x', img.height);
alert('Error: Image has invalid dimensions');
downloadBtn.disabled = true;
return;
}
loadedImage = img;
// Initialize export canvas with proper dimensions
console.log('Setting exportCanvas dimensions to', img.width, 'x', img.height);
exportCanvas.width = img.width;
exportCanvas.height = img.height;
console.log('Clearing exportCanvas context');
exportCtx.clearRect(0, 0, img.width, img.height);
console.log('Drawing image to exportCanvas');
exportCtx.drawImage(img, 0, 0);
// Also save to originalCanvas for conversion source (pristine copy)
originalCanvas.width = img.width;
originalCanvas.height = img.height;
originalCtx.clearRect(0, 0, img.width, img.height);
originalCtx.drawImage(img, 0, 0);
// Store original for reset
originalImageData = exportCanvas.toDataURL('image/png');
console.log('Original image data saved');
// Render preview
console.log('Calling renderPreviewFromExport');
renderPreviewFromExport();
downloadBtn.disabled = false;
console.log('Image preview rendered successfully');
} catch (error) {
console.error('Error in img.onload:', error, error.stack);
alert('Error processing image: ' + error.message);
downloadBtn.disabled = true;
}
};
img.onerror = (error) => {
console.error('Image failed to load', error);
alert('Unable to load image. Check browser console (F12) for details.');
downloadBtn.disabled = true;
};
console.log('Setting image src to data URL (length:', dataURL.length, ')');
console.log('Data URL preview:', dataURL.substring(0, 50) + '...');
img.src = dataURL;
console.log('Image src set');
}
// ============================================================================
// CONVERSION ORCHESTRATION
// ============================================================================
/**
* Check for secret easter egg - triggers on 10th ASCII conversion.
* Increments conversion counter and performs easter egg action.
* @returns {void}
*/
function checkEasterEgg() {
asciiConversionCount++;
if (asciiConversionCount === 10) {
triggerEasterEgg();
asciiConversionCount = 0;
}
}
/**
* Trigger surprise easter egg display.
* Shows celebratory text and attempts to load a neutral surprise image from web.
* Plays optional celebratory audio accompaniment.
* @async
* @returns {void}
*/
async function triggerEasterEgg() {
// Create a surprise display sized to current preview
resizeOutputCanvas();
const PREVIEW_W = outputCanvas.clientWidth || 800;
const PREVIEW_H = outputCanvas.clientHeight || 600;
ctx.clearRect(0, 0, PREVIEW_W, PREVIEW_H);
ctx.fillStyle = '#1a1a1a';
ctx.fillRect(0, 0, PREVIEW_W, PREVIEW_H);
// Draw surprise text
ctx.fillStyle = '#FFD700';
ctx.font = 'bold 64px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText('🎉 SURPRISE! 🎉', PREVIEW_W / 2, PREVIEW_H / 2 - 80);
// Draw celebratory message
ctx.fillStyle = '#00FF00';
ctx.font = 'bold 32px Arial';
ctx.fillText("You've unlocked a surprise!", PREVIEW_W / 2, PREVIEW_H / 2);
// Draw friendly message
ctx.fillStyle = '#FF1493';
ctx.font = '24px Arial';
ctx.fillText('Enjoy the moment!', PREVIEW_W / 2, PREVIEW_H / 2 + 80);
// Try to load and show a neutral surprise image
const surpriseImageUrls = [
'https://via.placeholder.com/400x300.png?text=Surprise',
'https://picsum.photos/400/300'
];
// Try each URL until one works
for (let url of surpriseImageUrls) {
const img = new Image();
img.crossOrigin = 'anonymous';
img.onload = () => {
// Draw image if successfully loaded
const ratio = img.width / img.height;
let w = PREVIEW_W * 0.6;
let h = w / ratio;
if (h > PREVIEW_H * 0.4) {
h = PREVIEW_H * 0.4;
w = h * ratio;
}
const x = (PREVIEW_W - w) / 2;
const y = PREVIEW_H / 2 + 120;
if (y + h <= PREVIEW_H) {
ctx.drawImage(img, x, y, w, h);
}
};
img.onerror = () => {
console.warn('Could not load surprise image from', url);
};
img.src = url;
}
// Play celebratory sound (optional)
playEasterAudio();
}
/**
* Play celebratory audio from external source.
* Handles audio load failures gracefully.
* @returns {void}
*/
function playEasterAudio() {
try {
const audio = new Audio('https://cdn.pixabay.com/download/audio/2021/08/27/audio_a2c5d8b34e.mp3');
audio.volume = 0.3;
audio.play().catch(() => {
console.log('Could not play audio');
});
} catch (e) {
console.log('Audio not available');
}
}
/**
* Main conversion orchestration function.
* Validates loaded image, saves current state to undo history,
* routes to ASCII or Paint conversion based on selected mode.
* Renders preview after conversion.
* @returns {void}
*/
function convert() {
if (!loadedImage) {
alert('Please upload an image first.');
return;
}
const mode = modeSelect.value;
// Push current state to history for undo
try {
const snapshot = exportCanvas.toDataURL('image/png');
historyStack.push(snapshot);
if (historyStack.length > MAX_HISTORY) historyStack.shift();
undoBtn.disabled = false;
} catch (e) {
console.warn('Could not push history', e);
}
if (mode === 'ascii') {
checkEasterEgg();
convertToASCII(Number(fontSizeInput.value), Number(asciiColsInput.value));
} else {
const paintOptions = {
pixel: document.getElementById('stylePixel').checked,
brush: document.getElementById('styleBrush').checked,
gallery: document.getElementById('styleGallery').checked,
impression: document.getElementById('styleImpression').checked,
watercolor: document.getElementById('styleWatercolor').checked,
pixelSize: Number(pixelSizeInput.value),
brushStrength: Number(document.getElementById('brushStrength').value) / 100,
textureStrength: Number(document.getElementById('textureStrength').value) / 100
};
convertToPaint(paintOptions);
}
renderPreviewFromExport();
}
/**
* Reset export canvas to original pristine image.
* Clears undo history and resets easter egg counter.
* @returns {void}
*/
function resetToOriginal() {
asciiConversionCount = 0;
if (!originalImageData) {
alert('No image loaded to reset.');
return;
}
const img = new Image();
img.crossOrigin = 'anonymous';
img.onload = () => {
exportCanvas.width = img.width;
exportCanvas.height = img.height;
exportCtx.clearRect(0, 0, img.width, img.height);
exportCtx.drawImage(img, 0, 0);
renderPreviewFromExport();
// Clear history after reset
historyStack.length = 0;
undoBtn.disabled = true;
};
img.onerror = () => {
alert('Could not load original image.');
};
img.src = originalImageData;
}
/**
* Download the converted image as PNG file.
* Creates temporary download link and triggers native download dialog.
* File name: 'converted.png'
* @async
* @returns {void}
*/
async function downloadImage() {
const url = exportCanvas.toDataURL('image/png');
const a = document.createElement('a');
a.href = url;
a.download = 'converted.png';
document.body.appendChild(a);
a.click();
a.remove();
}
// ============================================================================
// PAINT CONVERSION
// ============================================================================
/**
* Convert image to painterly style with selectable effects.
* Always reads from originalCanvas (pristine source) to prevent
* degradation on repeated conversions.
* Applies pixelation, brush strokes, impressionist, watercolor, and texture effects.
* @param {Object} opts - Paint conversion options
* @param {boolean} opts.pixel - Apply pixelation effect
* @param {boolean} opts.brush - Apply brush stroke effect
* @param {boolean} opts.gallery - Apply gallery/texture effect
* @param {boolean} opts.impression - Apply impressionist effect
* @param {boolean} opts.watercolor - Apply watercolor effect
* @param {number} opts.pixelSize - Size of pixels (2-128)
* @param {number} opts.brushStrength - Brush opacity (0-1)
* @param {number} opts.textureStrength - Texture opacity (0-1)
* @returns {void}
*/
function convertToPaint(opts) {
// Always read from original (pristine) canvas
const srcCanvas = originalCanvas.width > 0 ? originalCanvas : exportCanvas;
const pixelSize = Math.max(2, Math.round(opts.pixelSize || 8));
const smallW = Math.max(Math.floor(srcCanvas.width / pixelSize), 1);
const smallH = Math.max(Math.floor(srcCanvas.height / pixelSize), 1);
const small = document.createElement('canvas');
small.width = smallW;
small.height = smallH;
const sctx = small.getContext('2d');
sctx.drawImage(srcCanvas, 0, 0, srcCanvas.width, srcCanvas.height, 0, 0, smallW, smallH);
const smallData = sctx.getImageData(0, 0, smallW, smallH).data;
// Apply paint effects to export canvas
applyPaintEffects(exportCanvas, smallData, smallW, smallH, pixelSize, opts);
// Update preview
renderPreviewFromExport();
}
/**
* Apply paint effects to canvas.
* Renders pixelated blocks or applies brush/artistic effects.
* Uses original color data to paint effects onto export canvas.
* Handles edge stretching to avoid white borders.
* @param {HTMLCanvasElement} canvas - Target canvas to paint effects onto
* @param {Uint8ClampedArray} smallData - Pixel data from downsampled image
* @param {number} smallW - Width of downsampled image
* @param {number} smallH - Height of downsampled image
* @param {number} pixelSize - Size of each pixel block
* @param {Object} opts - Paint effect options
* @returns {void}
*/
function applyPaintEffects(canvas, smallData, smallW, smallH, pixelSize, opts) {
const ctx = canvas.getContext('2d');
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, canvasWidth, canvasHeight);
ctx.imageSmoothingEnabled = false;
if (opts.pixel) {
for (let y = 0; y < smallH; y++) {
for (let x = 0; x < smallW; x++) {
const i = (y * smallW + x) * 4;
const r = smallData[i];
const g = smallData[i + 1];
const b = smallData[i + 2];
const a = smallData[i + 3] / 255;
ctx.fillStyle = `rgba(${r},${g},${b},${a})`;
// Calculate rectangle size to fill remaining space on edges
let rectW = pixelSize;
let rectH = pixelSize;
let rectX = x * pixelSize;
let rectY = y * pixelSize;
// Stretch the last column to fill remaining width
if (x === smallW - 1) {
rectW = canvasWidth - rectX;
}
// Stretch the last row to fill remaining height
if (y === smallH - 1) {
rectH = canvasHeight - rectY;
}
ctx.fillRect(rectX, rectY, rectW, rectH);
}
}
}
if (opts.brush || opts.impression || opts.watercolor || opts.gallery) {
const brushAlpha = opts.brushStrength || 0.7;
for (let y = 0; y < smallH; y++) {
for (let x = 0; x < smallW; x++) {
const i = (y * smallW + x) * 4;
const r = smallData[i];
const g = smallData[i + 1];
const b = smallData[i + 2];
const a = smallData[i + 3] / 255;
// Calculate center position accounting for edge stretching
let rectX = x * pixelSize;
let rectY = y * pixelSize;
let rectW = pixelSize;
let rectH = pixelSize;
if (x === smallW - 1) {
rectW = canvasWidth - rectX;
}
if (y === smallH - 1) {
rectH = canvasHeight - rectY;
}
const cx = rectX + rectW / 2;
const cy = rectY + rectH / 2;
if (opts.brush) {
const strokes = Math.max(1, Math.round(pixelSize / 2));
ctx.globalCompositeOperation = 'source-over';
for (let s = 0; s < strokes; s++) {
const jitterX = (Math.random() - 0.5) * pixelSize * 0.6;
const jitterY = (Math.random() - 0.5) * pixelSize * 0.6;
const radius = pixelSize * (0.45 + Math.random() * 0.4);
ctx.beginPath();
ctx.fillStyle = `rgba(${r},${g},${b},${a * brushAlpha})`;
ctx.arc(cx + jitterX, cy + jitterY, radius, 0, Math.PI * 2);
ctx.fill();
}
}
if (opts.impression) {
if (Math.random() < 0.25) {
ctx.beginPath();
ctx.fillStyle = `rgba(${Math.min(255, r + 20)},${Math.min(255, g + 10)},${b},${a * 0.9})`;
ctx.arc(
cx + (Math.random() - 0.5) * pixelSize,
cy + (Math.random() - 0.5) * pixelSize,
pixelSize * 0.8,
0,
Math.PI * 2
);
ctx.fill();
}
}
if (opts.watercolor) {
ctx.globalCompositeOperation = 'lighter';
ctx.beginPath();
ctx.fillStyle = `rgba(${r},${g},${b},${0.12 * (a + 0.2)})`;
ctx.arc(cx, cy, pixelSize, 0, Math.PI * 2);
ctx.fill();
ctx.globalCompositeOperation = 'source-over';
}
}
}
ctx.globalAlpha = 1;
}
if (opts.gallery || opts.textureStrength) {
const tex = document.createElement('canvas');
tex.width = canvas.width;
tex.height = canvas.height;
const t = tex.getContext('2d');
const imgd = t.createImageData(tex.width, tex.height);
for (let i = 0; i < imgd.data.length; i += 4) {
const v = 230 + Math.floor(Math.random() * 25);
imgd.data[i] = imgd.data[i + 1] = imgd.data[i + 2] = v;
imgd.data[i + 3] = Math.floor(10 + (opts.textureStrength || 0) * 40);
}
t.putImageData(imgd, 0, 0);
ctx.globalCompositeOperation = 'overlay';
ctx.globalAlpha = Math.min(0.95, 0.3 + (opts.textureStrength || 0) * 0.7);
ctx.drawImage(tex, 0, 0);
ctx.globalAlpha = 1;
ctx.globalCompositeOperation = 'source-over';
}
}
// ============================================================================
// ASCII CONVERSION
// ============================================================================
// ============================================================================
// ASCII CONVERSION
// ============================================================================
/**
* Convert image to ASCII art with user-controlled size.
* Always reads from originalCanvas (pristine source) to prevent
* degradation on repeated conversions.
* Generates both visual ASCII text on canvas and plain-text output in textarea.
* Uses brightness calculation to map pixels to ASCII characters.
* @param {number} fontSize - Font size in pixels
* @param {number} cols - Number of ASCII columns (character width)
* @returns {void}
*/
function convertToASCII(fontSize, cols) {
cols = Number(cols) || 120;
// Ensure cols is even to avoid half-character columns
cols = Math.max(2, Math.round(cols));
if (cols % 2 !== 0) cols++;
// Always read from original (pristine) canvas
const srcCanvas = originalCanvas.width > 0 ? originalCanvas : exportCanvas;
// Store original dimensions
const origWidth = srcCanvas.width;
const origHeight = srcCanvas.height;
// Compute rows based on aspect ratio and a char aspect correction
const charAspect = 0.5; // approximate character height/width ratio
let rows = Math.max(4, Math.round((cols * origHeight / origWidth) * charAspect));
// Ensure rows is even to keep character grid balanced
if (rows % 2 !== 0) rows++;
const temp = document.createElement('canvas');
temp.width = cols;
temp.height = rows;
const tctx = temp.getContext('2d');