-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunicode-canvas.html
More file actions
447 lines (414 loc) · 19.1 KB
/
Copy pathunicode-canvas.html
File metadata and controls
447 lines (414 loc) · 19.1 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Unicode Art Canvas</title>
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body { background: #fff; color: #111; font-family: monospace; min-height: 100vh; padding: 20px; box-sizing: border-box; }
h1 { font-size: 15px; font-weight: 500; letter-spacing: 0.05em; margin-bottom: 14px; opacity: 0.45; }
#toolbar { display: none; align-items: center; gap: 18px; margin-bottom: 10px; flex-wrap: wrap; }
#toolbar label { font-size: 12px; opacity: 0.6; display: flex; align-items: center; gap: 5px; }
#toolbar input[type=range] { width: 80px; accent-color: #111; }
#toolbar span.val { font-size: 12px; font-weight: 600; min-width: 20px; opacity: 1; }
#toolbar button {
font-size: 12px; font-family: monospace;
padding: 3px 11px; border: 1px solid #ccc;
border-radius: 6px; background: #fff; color: #111; cursor: pointer;
}
#toolbar button:hover { background: #f0f0f0; }
#status { font-size: 11px; opacity: 0.38; margin-bottom: 6px; min-height: 15px; letter-spacing: 0.02em; }
#group-bar {
display: none; gap: 4px; margin-bottom: 8px;
flex-wrap: wrap; align-items: center;
}
.group-pill {
font-size: 10px; font-family: monospace;
padding: 2px 9px; border-radius: 10px;
border: 1px solid #ddd; color: #bbb;
cursor: pointer; user-select: none;
transition: background 0.12s, color 0.12s, border-color 0.12s;
}
.group-pill:hover { border-color: #999; color: #777; }
/* selected = included in active cycling set */
.group-pill.selected { background: #111; color: #fff; border-color: #111; }
.group-pill.selected:hover { background: #333; border-color: #333; }
/* "all" pill gets a separator after it */
#pill-all { font-weight: 700; margin-right: 4px; }
/* which group the needle is currently inside while cycling */
.group-pill.cycling { box-shadow: 0 0 0 2px #888; }
#loading {
display: flex; flex-direction: column; align-items: center; justify-content: center;
padding: 56px 24px; gap: 14px; border: 1px solid #e0e0e0; border-radius: 10px;
}
#loading-label { font-size: 13px; opacity: 0.5; }
#progress-bar-wrap { width: 280px; height: 3px; background: #eee; border-radius: 2px; overflow: hidden; }
#progress-bar { height: 100%; width: 0%; background: #111; border-radius: 2px; transition: width 0.1s; }
#progress-pct { font-size: 11px; opacity: 0.35; }
#canvas-wrap {
display: none; border: 1px solid #e0e0e0; border-radius: 10px;
overflow: auto; background: #fff; cursor: crosshair;
max-height: 75vh; padding: 6px; user-select: none;
width: 100%;
}
#art-grid { display: block; line-height: 1; }
.art-row { display: block; white-space: nowrap; overflow: hidden; }
.cell {
display: inline-block; width: 1ch; line-height: 1.35; height: 1.35em;
font-family: monospace; text-align: center; vertical-align: top;
overflow: hidden; color: #000;
}
.tooltip {
position: fixed; background: #fff; border: 1px solid #ccc; border-radius: 7px;
padding: 5px 10px; font-size: 11px; color: #555; pointer-events: none;
z-index: 100; white-space: nowrap; display: none; font-family: monospace;
}
</style>
</head>
<body>
<h1>unicode art canvas</h1>
<div id="toolbar">
<label>font <input type="range" id="fontSlider" min="8" max="32" value="14" step="1"> <span class="val" id="fontVal">14</span>px</label>
<label>rows <input type="range" id="rowsSlider" min="3" max="60" value="20" step="1"> <span class="val" id="rowsVal">20</span></label>
<label>speed <input type="range" id="speedSlider" min="1" max="10" value="4" step="1"> <span class="val" id="speedVal">4</span></label>
<button id="clearBtn">clear</button>
<button id="randomBtn">scatter</button>
<button id="loopBtn">loop: on</button>
<button id="copyBtn">copy text</button>
</div>
<div id="group-bar"></div>
<div id="status"></div>
<div id="loading">
<div id="loading-label">measuring pixel density of every terminal glyph…</div>
<div id="progress-bar-wrap"><div id="progress-bar"></div></div>
<div id="progress-pct">0%</div>
</div>
<div id="canvas-wrap"><div id="art-grid"></div></div>
<div class="tooltip" id="tooltip"></div>
<script>
(function () {
// ── Character group definitions ───────────────────────────────────────────
const GROUP_DEFS = [
{ name: 'space', ranges: [[0x0020, 0x0020]] },
{ name: 'dots & dashes', codepoints: [0x2E,0x2C,0x27,0x60,0x2D,0x5F,0x3A,0x3B,0x7C,0x21,0x7E,0x22,0x5E] },
{ name: 'ascii symbols', codepoints: [0x23,0x24,0x25,0x26,0x28,0x29,0x2A,0x2B,0x2F,0x3C,0x3D,0x3E,0x3F,0x40,0x5B,0x5C,0x5D,0x7B,0x7D] },
{ name: 'digits', ranges: [[0x30, 0x39]] },
{ name: 'lowercase', ranges: [[0x61, 0x7A]] },
{ name: 'uppercase', ranges: [[0x41, 0x5A]] },
{ name: 'latin ext', ranges: [[0x00A1, 0x00FF]] },
{ name: 'arrows & math', ranges: [[0x2190, 0x21FF], [0x2200, 0x22FF], [0x2300, 0x23FF]] },
{ name: 'box light', ranges: [[0x2500, 0x254F]] },
{ name: 'box heavy', ranges: [[0x2550, 0x257F]] },
{ name: 'geometric', ranges: [[0x25A0, 0x25FF]] },
{ name: 'braille', ranges: [[0x2800, 0x28FF]] },
{ name: 'blocks', ranges: [[0x2580, 0x259F]] },
];
// Build raw char lists per group
const groups = GROUP_DEFS.map(def => {
const chars = [], seen = new Set();
function add(cp) {
if (seen.has(cp)) return; seen.add(cp);
try { chars.push({ cp, ch: String.fromCodePoint(cp) }); } catch(e) {}
}
if (def.ranges) def.ranges.forEach(([s, e]) => { for (let c = s; c <= e; c++) add(c); });
if (def.codepoints) def.codepoints.forEach(add);
return { name: def.name, chars };
});
// ── Measurement ───────────────────────────────────────────────────────────
const CSIZE = 20;
const mCanvas = document.createElement('canvas');
mCanvas.width = CSIZE; mCanvas.height = CSIZE;
const mCtx = mCanvas.getContext('2d', { willReadFrequently: true });
mCtx.textAlign = 'center'; mCtx.textBaseline = 'middle';
mCtx.font = `${CSIZE - 2}px monospace`;
function measureChar(ch) {
mCtx.clearRect(0, 0, CSIZE, CSIZE);
mCtx.fillStyle = 'black';
mCtx.fillText(ch, CSIZE / 2, CSIZE / 2);
const data = mCtx.getImageData(0, 0, CSIZE, CSIZE).data;
let px = 0;
for (let j = 3; j < data.length; j += 4) if (data[j] > 8) px++;
return px;
}
async function measureAll() {
const total = groups.reduce((s, g) => s + g.chars.length, 0);
let done = 0;
for (const group of groups) {
const measured = [];
for (let i = 0; i < group.chars.length; i++) {
const { cp, ch } = group.chars[i];
measured.push({ cp, ch, px: measureChar(ch) });
done++;
if (i % 300 === 0) {
const pct = Math.min(100, Math.round(done / total * 100));
document.getElementById('progress-bar').style.width = pct + '%';
document.getElementById('progress-pct').textContent = pct + '%';
await new Promise(r => setTimeout(r, 0));
}
}
measured.sort((a, b) => a.px - b.px || a.cp - b.cp);
group.chars = measured;
}
document.getElementById('progress-bar').style.width = '100%';
document.getElementById('progress-pct').textContent = '100%';
await new Promise(r => setTimeout(r, 0));
}
// ── State ────────────────────────────────────────────────────────────────
const artGrid = document.getElementById('art-grid');
const statusEl = document.getElementById('status');
const tooltip = document.getElementById('tooltip');
const toolbar = document.getElementById('toolbar');
const wrap = document.getElementById('canvas-wrap');
const groupBar = document.getElementById('group-bar');
let cols = 10, rows = 20, speed = 4, fontSize = 14;
let cells = [], cellData = [];
let activeIdx = -1, activeTimer = null, mouseDown = false;
let loopMode = true;
// Selection state: Set of group indices that are "on"
// null means ALL mode (all groups on, toggled via the "all" pill)
let selectedGroups = new Set(groups.map((_, i) => i)); // start: all selected
let allMode = true; // true = "all" pill is active
// The active cycling sequence (flat array, rebuilt when selection changes)
let sequence = [];
function buildSequence() {
sequence = [];
const active = allMode ? groups.map((_, i) => i) : [...selectedGroups].sort((a,b)=>a-b);
active.forEach(gi => {
groups[gi].chars.forEach(({ cp, ch, px }) => {
sequence.push({ cp, ch, px, groupIdx: gi, groupName: groups[gi].name });
});
});
}
// ── Group pill bar ────────────────────────────────────────────────────────
let pillEls = []; // indexed by group index
let allPillEl;
let cyclingPill = -1; // which group pill has the cycling ring
function buildGroupBar() {
groupBar.innerHTML = '';
pillEls = [];
// "all" pill
allPillEl = document.createElement('span');
allPillEl.className = 'group-pill all-pill selected';
allPillEl.id = 'pill-all';
allPillEl.textContent = 'all';
allPillEl.title = 'cycle through all groups';
allPillEl.addEventListener('click', () => {
allMode = true;
selectedGroups = new Set(groups.map((_, i) => i));
allPillEl.classList.add('selected');
pillEls.forEach(p => p.classList.add('selected'));
buildSequence();
// reset cells that were mid-cycle
resetCellProgress();
});
groupBar.appendChild(allPillEl);
// Individual group pills
groups.forEach((g, gi) => {
const pill = document.createElement('span');
pill.className = 'group-pill selected'; // start all selected
pill.textContent = g.name;
pill.title = `${g.chars.length} chars`;
pill.addEventListener('click', () => {
if (selectedGroups.has(gi)) {
// Deselect — but keep at least one
if (selectedGroups.size <= 1) return;
selectedGroups.delete(gi);
pill.classList.remove('selected');
} else {
selectedGroups.add(gi);
pill.classList.add('selected');
}
// If not all are selected, turn off allMode
allMode = selectedGroups.size === groups.length;
if (allMode) allPillEl.classList.add('selected');
else allPillEl.classList.remove('selected');
buildSequence();
resetCellProgress();
});
groupBar.appendChild(pill);
pillEls.push(pill);
});
groupBar.style.display = 'flex';
}
function setCyclingPill(groupIdx) {
if (cyclingPill === groupIdx) return;
if (cyclingPill >= 0 && pillEls[cyclingPill]) pillEls[cyclingPill].classList.remove('cycling');
cyclingPill = groupIdx;
if (groupIdx >= 0 && pillEls[groupIdx]) pillEls[groupIdx].classList.add('cycling');
}
function clearCyclingPill() {
if (cyclingPill >= 0 && pillEls[cyclingPill]) pillEls[cyclingPill].classList.remove('cycling');
cyclingPill = -1;
}
// ── Grid ──────────────────────────────────────────────────────────────────
function computeColsFit() {
artGrid.style.fontSize = fontSize + 'px';
const probe = document.createElement('span');
probe.className = 'cell';
probe.textContent = ' ';
probe.setAttribute('aria-hidden', 'true');
Object.assign(probe.style, {
position: 'absolute',
visibility: 'hidden',
pointerEvents: 'none',
left: '-9999px',
top: '0',
});
artGrid.appendChild(probe);
const cellW = Math.max(4, probe.getBoundingClientRect().width);
probe.remove();
const cw = wrap.clientWidth;
const cs = window.getComputedStyle(wrap);
const pad =
parseFloat(cs.paddingLeft) + parseFloat(cs.paddingRight);
const inner = Math.max(0, cw - pad);
const MIN_COLS = 10;
const MAX_COLS = 400;
return Math.min(MAX_COLS, Math.max(MIN_COLS, Math.floor(inner / cellW)));
}
function buildGrid() {
stopActive();
cols = computeColsFit();
artGrid.innerHTML = '';
cells = []; cellData = [];
artGrid.style.fontSize = fontSize + 'px';
for (let r = 0; r < rows; r++) {
const row = document.createElement('div');
row.className = 'art-row';
for (let c = 0; c < cols; c++) {
const el = document.createElement('span');
el.className = 'cell';
el.textContent = ' ';
row.appendChild(el);
cells.push(el);
cellData.push({ charIdx: 0 });
}
artGrid.appendChild(row);
}
}
function resetCellProgress() {
// Sequence changed — reset charIdx so next paint starts from 0,
// but never touch the displayed character (keep the canvas as-is).
cellData.forEach(d => { d.charIdx = 0; });
}
function stopActive() {
if (activeTimer) { clearTimeout(activeTimer); activeTimer = null; }
activeIdx = -1;
clearCyclingPill();
}
function startCell(idx) {
if (idx === activeIdx) return;
stopActive();
if (idx < 0 || sequence.length === 0) return;
activeIdx = idx;
const interval = Math.max(16, 200 - speed * 18);
function tick() {
if (activeIdx !== idx) return;
const TOTAL = sequence.length;
const d = cellData[idx];
const el = cells[idx];
const next = d.charIdx + Math.max(1, Math.ceil(TOTAL / 800 * (speed / 3)));
if (!loopMode && next >= TOTAL) {
d.charIdx = TOTAL - 1;
const sc = sequence[d.charIdx];
el.textContent = sc.ch;
setCyclingPill(sc.groupIdx);
statusEl.textContent = `[${sc.groupName}] U+${sc.cp.toString(16).toUpperCase().padStart(4,'0')} · ${sc.px}px · ${(d.charIdx+1).toLocaleString()} / ${TOTAL.toLocaleString()}`;
stopActive();
return;
}
d.charIdx = next % TOTAL;
const sc = sequence[d.charIdx];
el.textContent = sc.ch;
setCyclingPill(sc.groupIdx);
statusEl.textContent = `[${sc.groupName}] U+${sc.cp.toString(16).toUpperCase().padStart(4,'0')} · ${sc.px}px · ${(d.charIdx+1).toLocaleString()} / ${TOTAL.toLocaleString()}`;
activeTimer = setTimeout(tick, interval);
}
tick();
}
function idxAt(x, y) {
const el = document.elementFromPoint(x, y);
if (!el || !el.classList.contains('cell')) return -1;
return cells.indexOf(el);
}
// ── Events ────────────────────────────────────────────────────────────────
wrap.addEventListener('mousedown', e => { mouseDown = true; startCell(idxAt(e.clientX, e.clientY)); });
wrap.addEventListener('mousemove', e => {
const i = idxAt(e.clientX, e.clientY);
if (mouseDown) startCell(i);
if (i >= 0) {
const d = cellData[i];
const sc = sequence[Math.max(0, Math.min(d.charIdx, sequence.length - 1))];
tooltip.style.display = 'block';
tooltip.style.left = (e.clientX + 14) + 'px';
tooltip.style.top = (e.clientY - 28) + 'px';
tooltip.textContent = (d.charIdx > 0 && sc)
? `[${sc.groupName}] U+${sc.cp.toString(16).toUpperCase().padStart(4,'0')} · ${sc.px}px`
: 'hold to paint';
} else { tooltip.style.display = 'none'; }
});
document.addEventListener('mouseup', () => { mouseDown = false; stopActive(); });
wrap.addEventListener('mouseleave', () => { tooltip.style.display = 'none'; if (mouseDown) stopActive(); });
wrap.addEventListener('touchstart', e => { e.preventDefault(); mouseDown=true; const t=e.touches[0]; startCell(idxAt(t.clientX,t.clientY)); }, {passive:false});
wrap.addEventListener('touchmove', e => { e.preventDefault(); const t=e.touches[0]; startCell(idxAt(t.clientX,t.clientY)); }, {passive:false});
wrap.addEventListener('touchend', () => { mouseDown=false; stopActive(); });
document.getElementById('clearBtn').addEventListener('click', () => {
buildGrid(); statusEl.textContent=''; clearCyclingPill();
});
document.getElementById('randomBtn').addEventListener('click', () => {
const TOTAL = sequence.length;
if (!TOTAL) return;
[...Array(rows*cols).keys()].sort(()=>Math.random()-0.5)
.slice(0, Math.floor(rows*cols*0.4))
.forEach((idx,i) => setTimeout(() => {
if (true) {
cellData[idx].charIdx = Math.floor(Math.random() * TOTAL);
cells[idx].textContent = sequence[cellData[idx].charIdx].ch;
}
}, i * 8));
});
document.getElementById('copyBtn').addEventListener('click', () => {
const lines = [];
for (let r = 0; r < rows; r++) {
let line = '';
for (let c = 0; c < cols; c++) line += cells[r * cols + c].textContent;
lines.push(line);
}
navigator.clipboard.writeText(lines.join('\n')).then(() => {
const btn = document.getElementById('copyBtn');
btn.textContent = 'copied!';
setTimeout(() => btn.textContent = 'copy text', 1500);
});
});
document.getElementById('loopBtn').addEventListener('click', () => {
loopMode = !loopMode;
document.getElementById('loopBtn').textContent = loopMode ? 'loop: on' : 'loop: off';
document.getElementById('loopBtn').style.opacity = loopMode ? '1' : '0.45';
});
document.getElementById('rowsSlider').addEventListener('input', function(){ rows=+this.value; document.getElementById('rowsVal').textContent=rows; buildGrid(); });
document.getElementById('speedSlider').addEventListener('input', function(){ speed=+this.value; document.getElementById('speedVal').textContent=speed; });
document.getElementById('fontSlider').addEventListener('input', function(){ fontSize=+this.value; document.getElementById('fontVal').textContent=fontSize; buildGrid(); });
// ── Boot ──────────────────────────────────────────────────────────────────
measureAll().then(() => {
buildSequence();
document.getElementById('loading').style.display = 'none';
toolbar.style.display = 'flex';
wrap.style.display = 'block';
buildGroupBar();
buildGrid();
new ResizeObserver(() => {
if (wrap.clientWidth <= 0) return;
requestAnimationFrame(() => {
const next = computeColsFit();
if (next === cols) return;
buildGrid();
});
}).observe(wrap);
const minPx = sequence[0].px, maxPx = sequence[sequence.length-1].px;
statusEl.textContent = `ready · ${sequence.length.toLocaleString()} glyphs · ${groups.length} groups · ${minPx}px → ${maxPx}px`;
});
})();
</script>
</body>
</html>