-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
280 lines (269 loc) · 14.8 KB
/
index.html
File metadata and controls
280 lines (269 loc) · 14.8 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Img2Ansi</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;700&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400&display=swap" rel="stylesheet">
<script src="Lib/img2ansi.js"></script>
<style>
body {
font-family: 'Inter', sans-serif;
background-color: #f0f4f8;
}
.ansi-output-container {
white-space: pre;
overflow-x: auto;
word-break: normal;
overflow-wrap: normal;
max-height: 350px;
font-family: 'Roboto Mono', monospace;
line-height: 1;
font-size: 10px;
background-color: #1a202c;
color: #e2e8f0;
padding: 0.75rem 1rem;
border-radius: 0.5rem;
box-shadow: inset 0 2px 4px 0 rgba(0, 0, 0, 0.06);
}
.ansi-output-container::-webkit-scrollbar { width: 8px; height: 8px; }
.ansi-output-container::-webkit-scrollbar-track { background: #2d3748; border-radius: 4px; }
.ansi-output-container::-webkit-scrollbar-thumb { background: #4a5568; border-radius: 4px; }
.ansi-output-container::-webkit-scrollbar-thumb:hover { background: #718096; }
.ansi-rendered-art span { display: inline-block; }
.input-group { margin-bottom: 1rem; }
.input-group label { display: block; margin-bottom: 0.25rem; font-weight: 500; color: #4a5568;}
.input-group input, .input-group select {
width: 100%;
padding: 0.5rem 0.75rem;
border: 1px solid #cbd5e0;
border-radius: 0.375rem;
box-shadow: sm;
}
</style>
</head>
<body class="text-gray-800">
<div class="container mx-auto p-4 sm:p-6 md:p-8 max-w-5xl">
<header class="mb-8 text-center">
<h1 class="text-3xl sm:text-4xl font-bold text-blue-600">Advanced Image to ANSI Art</h1>
<p class="text-sm text-gray-600 mt-1">Customize your ANSI art generation with more options!</p>
</header>
<section class="mb-6 p-6 bg-white rounded-xl shadow-lg">
<h2 class="text-xl font-semibold text-gray-700 mb-4 border-b pb-2">1. Upload Image</h2>
<input type="file" id="imageUpload" accept="image/*" class="block w-full text-sm text-gray-600 file:mr-4 file:py-2 file:px-4 file:rounded-lg file:border-0 file:text-sm file:font-semibold file:bg-blue-50 file:text-blue-700 hover:file:bg-blue-100 cursor-pointer transition-colors duration-150"/>
<div class="mt-4 text-center">
<img id="imagePreview" src="https://placehold.co/400x300/e2e8f0/94a3b8?text=Image+Preview" alt="Image Preview" class="mt-2 rounded-md max-w-full sm:max-w-md mx-auto shadow-md border border-gray-200"/>
</div>
</section>
<section class="mb-6 p-6 bg-white rounded-xl shadow-lg">
<h2 class="text-xl font-semibold text-gray-700 mb-4 border-b pb-2">2. Output Settings</h2>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<div class="input-group">
<label for="outputWidth">Width (characters, optional):</label>
<input type="number" id="outputWidth" value="80" min="10" max="500" placeholder="e.g., 80">
</div>
<div class="input-group">
<label for="outputHeight">Height (rows, optional):</label>
<input type="number" id="outputHeight" min="5" max="300" placeholder="e.g., 40">
</div>
<div class="input-group md:col-span-2">
<label for="qualitySelect">Quality:</label>
<select id="qualitySelect" class="focus:ring-blue-500 focus:border-blue-500">
<option value="high" selected>High (24-bit color)</option>
<option value="perfect">Perfect (24-bit, precise scaling)</option>
<option value="medium">Medium (Quantized 24-bit)</option>
<option value="low">Low (Highly Quantized 24-bit)</option>
</select>
<p class="text-xs text-gray-500 mt-1">"Perfect" uses user-defined width/height directly. "High" maintains aspect ratio if one dimension is missing. "Medium" and "Low" reduce color fidelity.</p>
</div>
</div>
</section>
<section class="mb-6 text-center">
<button id="generateButton" class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-3 px-6 rounded-lg shadow-md hover:shadow-lg transform hover:scale-105 transition-all duration-150 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-opacity-50 disabled:opacity-50 disabled:cursor-not-allowed" disabled>
Generate ANSI Art
</button>
<div id="loadingIndicator" class="mt-3 text-sm text-blue-600 hidden flex items-center justify-center">
<svg class="animate-spin -ml-1 mr-3 h-5 w-5 text-blue-600" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg>
Processing...
</div>
<div id="errorDisplay" role="alert" class="mt-3 p-3 bg-red-100 text-red-700 rounded-md hidden text-sm shadow"></div>
</section>
<section id="resultContainer" class="hidden mb-8">
<h2 class="text-xl font-semibold mb-2 text-gray-700 border-b pb-2">3. Generated ANSI Art</h2>
<h3 class="text-lg font-medium mb-1 text-gray-600 mt-4">Rendered Preview:</h3>
<div id="ansiRenderedPreview" class="ansi-output-container ansi-rendered-art mb-4"></div>
<h3 class="text-lg font-medium mb-1 text-gray-600">Copyable `printf` Command:</h3>
<button id="copyButton" class="mb-2 bg-green-500 hover:bg-green-600 text-white font-bold py-2 px-4 rounded-md text-sm shadow hover:shadow-md transform hover:scale-105 transition-all duration-150 focus:outline-none focus:ring-2 focus:ring-green-500 focus:ring-opacity-50">
Copy Command
</button>
<pre id="ansiRawOutput" class="ansi-output-container"></pre>
</section>
<footer class="text-center mt-12 text-xs text-gray-500">
<p>ANSI Art Converter</p>
</footer>
</div>
<script>
const imageUpload = document.getElementById('imageUpload');
const imagePreview = document.getElementById('imagePreview');
const outputWidthInput = document.getElementById('outputWidth');
const outputHeightInput = document.getElementById('outputHeight');
const qualitySelect = document.getElementById('qualitySelect');
const generateButton = document.getElementById('generateButton');
const loadingIndicator = document.getElementById('loadingIndicator');
const errorDisplay = document.getElementById('errorDisplay');
const resultContainer = document.getElementById('resultContainer');
const ansiRenderedPreview = document.getElementById('ansiRenderedPreview');
const ansiRawOutput = document.getElementById('ansiRawOutput');
const copyButton = document.getElementById('copyButton');
let uploadedImage = null;
const ACTUAL_ESC = '\u001b';
const PRINTF_ESC = '\\e';
// Initialize the Img2Ansi library
const img2ansi = new Img2Ansi({
escChar: ACTUAL_ESC,
lineEnding: '\n',
quality: 'high'
});
imageUpload.addEventListener('change', (event) => {
const file = event.target.files[0];
if (file) {
if (!file.type.startsWith('image/')) {
showError('Please upload a valid image file.');
uploadedImage = null;
imagePreview.src = "https://placehold.co/400x300/e2e8f0/94a3b8?text=Image+Preview";
generateButton.disabled = true;
return;
}
const reader = new FileReader();
reader.onload = (e) => {
imagePreview.src = e.target.result;
uploadedImage = new Image();
uploadedImage.onload = () => {
generateButton.disabled = false;
hideError();
};
uploadedImage.onerror = () => {
showError('Failed to load image.');
uploadedImage = null;
imagePreview.src = "https://placehold.co/400x300/e2e8f0/94a3b8?text=Image+Preview";
generateButton.disabled = true;
};
uploadedImage.src = e.target.result;
};
reader.onerror = () => {
showError('Failed to read file.');
uploadedImage = null;
imagePreview.src = "https://placehold.co/400x300/e2e8f0/94a3b8?text=Image+Preview";
generateButton.disabled = true;
};
reader.readAsDataURL(file);
} else {
uploadedImage = null;
imagePreview.src = "https://placehold.co/400x300/e2e8f0/94a3b8?text=Image+Preview";
generateButton.disabled = true;
}
});
generateButton.addEventListener('click', async () => {
if (!uploadedImage || !uploadedImage.complete || uploadedImage.naturalHeight === 0) {
showError('Please upload and wait for a valid image to load.');
return;
}
loadingIndicator.classList.remove('hidden');
resultContainer.classList.add('hidden');
generateButton.disabled = true;
hideError();
await new Promise(resolve => setTimeout(resolve, 50));
try {
let desiredAnsiWidth = parseInt(outputWidthInput.value, 10);
let desiredAnsiHeight = parseInt(outputHeightInput.value, 10);
const quality = qualitySelect.value;
if (!desiredAnsiWidth && !desiredAnsiHeight) {
desiredAnsiWidth = 80;
}
// Use the Img2Ansi library to convert the image
const rawAnsiForRendering = img2ansi.convertToAnsi({
img: uploadedImage,
width: desiredAnsiWidth,
height: desiredAnsiHeight,
quality: quality,
lineEnding: '\n',
escChar: ACTUAL_ESC
});
// Render the ANSI art to HTML
img2ansi.renderToHtml(rawAnsiForRendering, ansiRenderedPreview);
// Generate the printf command
const printfCommandString = img2ansi.toPrintfCommand(rawAnsiForRendering);
ansiRawOutput.textContent = printfCommandString;
resultContainer.classList.remove('hidden');
} catch (err) {
console.error("Error generating ANSI art:", err);
showError(`Error: ${err.message}`);
} finally {
loadingIndicator.classList.add('hidden');
generateButton.disabled = !uploadedImage || !uploadedImage.complete || uploadedImage.naturalHeight === 0;
}
});
function copyTextToClipboard(text, buttonElement) {
if (!text) return;
const textarea = document.createElement('textarea');
textarea.value = text;
textarea.style.position = 'fixed';
textarea.style.opacity = '0';
document.body.appendChild(textarea);
textarea.select();
try {
document.execCommand('copy');
const originalText = buttonElement.textContent;
const originalBgMatch = buttonElement.className.match(/bg-[\w-]+-\d+/);
const originalBg = originalBgMatch ? originalBgMatch[0] : 'bg-gray-500';
buttonElement.textContent = 'Copied!';
buttonElement.classList.remove(originalBg);
buttonElement.classList.add('bg-green-600');
setTimeout(() => {
buttonElement.textContent = originalText;
buttonElement.classList.remove('bg-green-600');
buttonElement.classList.add(originalBg);
}, 1500);
} catch (err) {
console.error('Failed to copy text using execCommand: ', err);
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard.writeText(text).then(() => {
const originalText = buttonElement.textContent;
const originalBgMatch = buttonElement.className.match(/bg-[\w-]+-\d+/);
const originalBg = originalBgMatch ? originalBgMatch[0] : 'bg-gray-500';
buttonElement.textContent = 'Copied!';
buttonElement.classList.remove(originalBg);
buttonElement.classList.add('bg-green-600');
setTimeout(() => {
buttonElement.textContent = originalText;
buttonElement.classList.remove('bg-green-600');
buttonElement.classList.add(originalBg);
}, 1500);
}).catch(clipErr => {
showError('Failed to copy. Please copy manually.');
console.error('navigator.clipboard.writeText failed: ', clipErr);
});
} else {
showError('Failed to copy. Please select the text and copy manually.');
}
}
document.body.removeChild(textarea);
}
copyButton.addEventListener('click', () => copyTextToClipboard(ansiRawOutput.textContent, copyButton));
function showError(message) {
errorDisplay.textContent = message;
errorDisplay.classList.remove('hidden');
errorDisplay.setAttribute('aria-live', 'assertive');
}
function hideError() {
errorDisplay.classList.add('hidden');
errorDisplay.removeAttribute('aria-live');
}
</script>
</body>
</html>