Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const imageInput = document.getElementById('imageInput');
+ const asciiOutput = document.getElementById('asciiOutput');
+ const copyButton = document.getElementById('copyButton');
+ const widthSlider = document.getElementById('width');
+ const brightnessSlider = document.getElementById('brightness');
+ const widthValue = document.getElementById('widthValue');
+ const brightnessValue = document.getElementById('brightnessValue');
+
+ const ASCII_CHARS = '@%#*+=-:. ';
+ let currentImage = null;
+
+ function updateValueDisplays() {
+ widthValue.textContent = widthSlider.value;
+ brightnessValue.textContent = `${brightnessSlider.value}%`;
+ }
+
+ function handleDragOver(e) {
+ e.preventDefault();
+ e.stopPropagation();
+ e.currentTarget.classList.add('dragover');
+ }
+
+ function handleDragLeave(e) {
+ e.preventDefault();
+ e.stopPropagation();
+ e.currentTarget.classList.remove('dragover');
+ }
+
+ function handleDrop(e) {
+ e.preventDefault();
+ e.stopPropagation();
+ e.currentTarget.classList.remove('dragover');
+
+ const file = e.dataTransfer.files[0];
+ if (file && file.type.startsWith('image/')) {
+ imageInput.files = e.dataTransfer.files;
+ handleImageUpload(file);
+ }
+ }
+
+ function handleImageUpload(file) {
+ const reader = new FileReader();
+ reader.onload = (e) => {
+ const img = new Image();
+ img.onload = () => {
+ currentImage = img;
+ convertToAscii();
+ };
+ img.src = e.target.result;
+ };
+ reader.readAsDataURL(file);
+ }
+
+ function convertToAscii() {
+ if (!currentImage) return;
+
+ const canvas = document.createElement('canvas');
+ const ctx = canvas.getContext('2d');
+ const width = parseInt(widthSlider.value);
+ const height = Math.floor(currentImage.height * width / currentImage.width);
+
+ canvas.width = width;
+ canvas.height = height;
+
+ ctx.drawImage(currentImage, 0, 0, width, height);
+ const imageData = ctx.getImageData(0, 0, width, height);
+ const brightness = parseInt(brightnessSlider.value) / 100;
+
+ let ascii = '';
+ for (let y = 0; y < height; y++) {
+ for (let x = 0; x < width; x++) {
+ const offset = (y * width + x) * 4;
+ const r = imageData.data[offset];
+ const g = imageData.data[offset + 1];
+ const b = imageData.data[offset + 2];
+ const avg = (r + g + b) / 3 * brightness;
+ const charIndex = Math.floor(avg * (ASCII_CHARS.length - 1) / 255);
+ ascii += ASCII_CHARS[charIndex];
+ }
+ ascii += '\n';
+ }
+
+ asciiOutput.textContent = ascii;
+ }
+
+ imageInput.addEventListener('change', (e) => {
+ if (e.target.files[0]) {
+ handleImageUpload(e.target.files[0]);
+ }
+ });
+
+ copyButton.addEventListener('click', () => {
+ navigator.clipboard.writeText(asciiOutput.textContent)
+ .then(() => {
+ copyButton.textContent = 'Copied!';
+ setTimeout(() => {
+ copyButton.textContent = 'Copy';
+ }, 2000);
+ });
+ });
+
+ widthSlider.addEventListener('input', () => {
+ updateValueDisplays();
+ convertToAscii();
+ });
+
+ brightnessSlider.addEventListener('input', () => {
+ updateValueDisplays();
+ convertToAscii();
+ });
+
+ const uploadLabel = document.querySelector('.upload-label');
+ uploadLabel.addEventListener('dragover', handleDragOver);
+ uploadLabel.addEventListener('dragleave', handleDragLeave);
+ uploadLabel.addEventListener('drop', handleDrop);
+
+ updateValueDisplays();
+ });