-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
522 lines (436 loc) · 15.7 KB
/
script.js
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
// Constants
const GRID = 16;
// Global variables
let isHexToEmoji = true;
let zoomWindow = null;
let updateTimer = null;
let zoomEnabled = false;
let isDarkMode = false;
// Utility functions
function showCopyNotification(element) {
const notification = element.querySelector('.copy-notification');
notification.style.opacity = '1';
setTimeout(() => {
notification.style.opacity = '0';
}, 1000);
}
function updateCopyButtonState() {
const output = document.getElementById('converterOutput');
const copyButton = document.getElementById('copyButton');
copyButton.disabled = output.value.trim() === '';
}
function toggleDarkMode() {
isDarkMode = !isDarkMode;
const glyphCard = document.querySelector('.card:has(#glyph-output)');
const glyphCardHeader = glyphCard.querySelector('.card-header');
glyphCard.classList.toggle('dark-mode', isDarkMode);
glyphCardHeader.classList.toggle('dark-mode', isDarkMode);
const darkModeToggle = document.getElementById('darkModeToggle');
darkModeToggle.innerHTML = isDarkMode
? '<i class="fas fa-sun"></i> Toggle Light Mode'
: '<i class="fas fa-moon"></i> Toggle Dark Mode';
renderGlyphs();
}
// Glyph related functions
function Glyph(glyph = "E0") {
const filename = `glyph_${glyph}`;
const startChar = parseInt(filename.split("_").pop() + "00", 16);
let markdownContent = ``;
for (let i = 0; i < GRID * GRID; i++) {
const row = Math.floor(i / GRID) + 1;
const col = (i % GRID) + 1;
const charCode = startChar + i;
const char = String.fromCodePoint(charCode);
const hexCode = charCode.toString(16).toUpperCase().padStart(4, '0');
markdownContent += `<div data-hex="0x${hexCode}" data-char="${char}" data-position="(${col};${row})" style="color: ${isDarkMode ? '#ffffff' : '#000000'}">
${char}
<span class="tooltip">Position: (${col};${row}) - Hex: 0x${hexCode}</span>
<span class="copy-notification">Copied</span>
</div>`;
}
document.getElementById('glyph-output').innerHTML = markdownContent;
addClickEventToGlyphs();
}
function initializeGlyph() {
const glyphOutput = document.getElementById('glyph-output');
if (glyphOutput.innerHTML.trim() === '') {
Glyph("E0");
}
}
function addClickEventToGlyphs() {
document.querySelectorAll('#glyph-output div').forEach(div => {
div.addEventListener('click', function () {
const char = this.getAttribute('data-char');
navigator.clipboard.writeText(char).then(() => {
showCopyNotification(this);
});
});
});
}
// Conversion functions
function convertHexToEmoji(input) {
try {
const codePoint = parseInt(input, 16);
document.getElementById('converterOutput').value = String.fromCodePoint(codePoint);
document.getElementById('successMsg').textContent = 'Converted successfully!';
document.getElementById('successMsg').classList.remove('d-none');
} catch (error) {
document.getElementById('errorMsg').textContent = 'Invalid Unicode code point.';
document.getElementById('errorMsg').classList.remove('d-none');
}
}
function convertEmojiToHex(input) {
const hexValue = input.codePointAt(0).toString(16).toUpperCase().padStart(4, '0');
document.getElementById('converterOutput').value = `0x${hexValue}`;
document.getElementById('successMsg').textContent = 'Converted successfully!';
document.getElementById('successMsg').classList.remove('d-none');
}
function convert() {
const input = document.getElementById('converterInput').value.trim();
const errorMsg = document.getElementById('errorMsg');
const successMsg = document.getElementById('successMsg');
const output = document.getElementById('converterOutput');
errorMsg.classList.add('d-none');
successMsg.classList.add('d-none');
output.value = '';
if (/^[0-9A-Fa-f]{1,6}$/.test(input)) {
convertHexToEmoji(input);
} else if (input.length === 1) {
convertEmojiToHex(input);
} else {
errorMsg.textContent = 'Invalid input. Please enter a hex value or a single emoji/symbol.';
errorMsg.classList.remove('d-none');
}
updateCopyButtonState();
}
function copyOutput() {
const output = document.getElementById('converterOutput');
const copyButton = document.getElementById('copyButton');
if (output.value.trim() === '') {
return;
}
navigator.clipboard.writeText(output.value).then(() => {
const originalText = copyButton.innerHTML;
copyButton.innerHTML = '<i class="fas fa-check me-2"></i>Copied';
copyButton.disabled = true;
setTimeout(() => {
copyButton.innerHTML = originalText;
copyButton.disabled = false;
}, 2000);
});
}
// Glyph upload and processing
function renderGlyphs() {
const glyphOutput = document.getElementById('glyph-output');
const glyphs = glyphOutput.querySelectorAll('div');
glyphs.forEach(glyph => {
glyph.style.backgroundColor = isDarkMode ? '#2a2a2a' : '#ffffff';
glyph.style.color = isDarkMode ? '#ffffff' : '#000000';
const backgroundImage = glyph.style.backgroundImage;
if (backgroundImage) {
const img = new Image();
img.onload = function () {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const data = imageData.data;
for (let i = 0; i < data.length; i += 4) {
if (data[i + 3] === 0) { // If pixel is transparent
if (isDarkMode) {
data[i] = 50; // R
data[i + 1] = 50; // G
data[i + 2] = 50; // B
data[i + 3] = 128; // A (semi-transparent)
} else {
data[i] = 200; // R
data[i + 1] = 200; // G
data[i + 2] = 200; // B
data[i + 3] = 64; // A (semi-transparent)
}
}
}
ctx.putImageData(imageData, 0, 0);
glyph.style.backgroundImage = `url(${canvas.toDataURL()})`;
};
img.src = backgroundImage.slice(5, -2); // Remove 'url("")' from backgroundImage
}
// Update glyph background color
if (glyph.classList.contains('transparent')) {
glyph.style.backgroundColor = isDarkMode ? 'rgba(50, 50, 50, 0.5)' : 'rgba(200, 200, 200, 0.25)';
}
const tooltip = glyph.querySelector('.tooltip');
if (tooltip) {
tooltip.style.backgroundColor = isDarkMode ? '#4a4a4a' : '#333';
tooltip.style.color = isDarkMode ? '#ffffff' : '#ffffff';
}
const copyNotification = glyph.querySelector('.copy-notification');
if (copyNotification) {
copyNotification.style.backgroundColor = isDarkMode ? 'rgba(255, 255, 255, 0.8)' : 'rgba(0, 0, 0, 0.8)';
copyNotification.style.color = isDarkMode ? '#000000' : '#ffffff';
}
});
}
function processGlyph(img, hexValue) {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const unicodeSize = Math.floor(Math.min(img.width, img.height) / 16);
canvas.width = unicodeSize * 16;
canvas.height = unicodeSize * 16;
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
let markdownContent = '';
const startChar = parseInt(hexValue + "00", 16);
for (let i = 0; i < 256; i++) {
const row = Math.floor(i / 16) + 1;
const col = (i % 16) + 1;
const charCode = startChar + i;
const char = String.fromCodePoint(charCode);
const hexCode = charCode.toString(16).toUpperCase().padStart(4, '0');
const x = (i % 16) * unicodeSize;
const y = Math.floor(i / 16) * unicodeSize;
const unicodeCanvas = document.createElement('canvas');
unicodeCanvas.width = unicodeSize;
unicodeCanvas.height = unicodeSize;
const unicodeCtx = unicodeCanvas.getContext('2d');
unicodeCtx.imageSmoothingEnabled = false;
unicodeCtx.drawImage(canvas, x, y, unicodeSize, unicodeSize, 0, 0, unicodeSize, unicodeSize);
const imageData = unicodeCtx.getImageData(0, 0, unicodeSize, unicodeSize);
const isTransparent = imageData.data.every((value, index) => (index + 1) % 4 === 0 || value === 0);
const transparentClass = isTransparent ? 'transparent' : '';
markdownContent += `<div class="${transparentClass}" data-hex="0x${hexCode}" data-char="${char}"
data-position="(${col};${row})"
style="background-image: url(${unicodeCanvas.toDataURL()}); background-size: 100% 100%;">
<span class="tooltip">Position: (${col};${row}) - Hex: 0x${hexCode}</span>
<span class="copy-notification">Copied</span>
</div>`;
}
document.getElementById('glyph-output').innerHTML = markdownContent;
addClickEventToGlyphs();
removeZoomEvents();
zoomEnabled = false;
if (img.width > 0 || img.height > 0) {
zoomEnabled = true;
addZoomEvents(unicodeSize);
} else {
hideZoomWindow();
}
document.getElementById('glyph-output').innerHTML = markdownContent;
addClickEventToGlyphs();
renderGlyphs();
}
// Zoom related functions
function removeZoomEvents() {
const glyphOutput = document.getElementById('glyph-output');
if (glyphOutput.zoomHandlers) {
glyphOutput.removeEventListener('mouseover', glyphOutput.zoomHandlers.mouseover);
glyphOutput.removeEventListener('mousemove', glyphOutput.zoomHandlers.mousemove);
glyphOutput.removeEventListener('mouseout', glyphOutput.zoomHandlers.mouseout);
delete glyphOutput.zoomHandlers;
}
}
function addZoomEvents(unicodeSize) {
const glyphOutput = document.getElementById('glyph-output');
function zoomMouseoverHandler(e) {
const target = e.target.closest('div[data-hex]');
if (target && zoomEnabled) {
showZoomWindow(unicodeSize);
updateZoomWindowContent(target, unicodeSize);
}
}
function zoomMousemoveHandler(e) {
const target = e.target.closest('div[data-hex]');
if (target && zoomWindow && zoomEnabled) {
clearTimeout(updateTimer);
updateTimer = setTimeout(() => {
updateZoomWindowContent(target, unicodeSize);
}, 50);
}
}
function zoomMouseoutHandler(e) {
if (!e.relatedTarget || !e.relatedTarget.closest('#glyph-output')) {
hideZoomWindow();
}
}
glyphOutput.addEventListener('mouseover', zoomMouseoverHandler);
glyphOutput.addEventListener('mousemove', zoomMousemoveHandler);
glyphOutput.addEventListener('mouseout', zoomMouseoutHandler);
glyphOutput.zoomHandlers = {
mouseover: zoomMouseoverHandler,
mousemove: zoomMousemoveHandler,
mouseout: zoomMouseoutHandler
};
}
function updateZoomWindowPosition(e) {
const padding = 20;
let left = e.pageX + padding;
let top = e.pageY + padding;
const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;
if (left + zoomWindow.offsetWidth > windowWidth) {
left = e.pageX - zoomWindow.offsetWidth - padding;
}
if (top + zoomWindow.offsetHeight > windowHeight) {
top = e.pageY - zoomWindow.offsetHeight - padding;
}
zoomWindow.style.left = `${left}px`;
zoomWindow.style.top = `${top}px`;
}
function updateZoomWindowContent(target, unicodeSize) {
if (!target || !zoomWindow) return;
const hexCode = target.getAttribute('data-hex');
const position = target.getAttribute('data-position');
const backgroundImage = target.style.backgroundImage;
const zoomCanvas = zoomWindow.querySelector('canvas');
const zoomCtx = zoomCanvas.getContext('2d');
zoomCtx.imageSmoothingEnabled = false;
zoomCtx.clearRect(0, 0, zoomCanvas.width, zoomCanvas.height);
if (backgroundImage) {
const img = new Image();
img.onload = function () {
const scale = Math.min(zoomCanvas.width / unicodeSize, zoomCanvas.height / unicodeSize);
const scaledWidth = unicodeSize * scale;
const scaledHeight = unicodeSize * scale;
const offsetX = (zoomCanvas.width - scaledWidth) / 2;
const offsetY = (zoomCanvas.height - scaledHeight) / 2;
zoomCtx.drawImage(img, 0, 0, unicodeSize, unicodeSize,
offsetX, offsetY, scaledWidth, scaledHeight);
};
img.src = backgroundImage.slice(5, -2);
}
const info = zoomWindow.querySelector('.zoom-info');
info.textContent = `Hex: ${hexCode} - Position: ${position}`;
}
function showZoomWindow(unicodeSize) {
if (!zoomWindow) {
zoomWindow = createZoomWindow(unicodeSize);
document.body.appendChild(zoomWindow);
}
zoomWindow.style.display = 'block';
}
function hideZoomWindow() {
if (zoomWindow) {
zoomWindow.style.display = 'none';
}
}
function createZoomWindow(unicodeSize) {
const zoomWindow = document.createElement('div');
zoomWindow.className = 'zoom-window';
zoomWindow.style.position = 'fixed';
zoomWindow.style.right = '20px';
zoomWindow.style.bottom = '20px';
zoomWindow.style.zIndex = '1000';
zoomWindow.style.background = 'white';
zoomWindow.style.border = '1px solid #ccc';
zoomWindow.style.boxShadow = '0 0 10px rgba(0,0,0,0.3)';
zoomWindow.style.padding = '5px';
zoomWindow.style.display = 'none';
const zoomCanvas = document.createElement('canvas');
zoomCanvas.width = 256;
zoomCanvas.height = 256;
zoomWindow.appendChild(zoomCanvas);
const info = document.createElement('div');
info.className = 'zoom-info';
zoomWindow.appendChild(info);
return zoomWindow;
}
// Event listeners
window.onload = () => {
initializeGlyph();
};
document.getElementById('glyph-input').addEventListener('input', function () {
const glyphInput = this.value.trim();
const glyphSuccessMsg = document.getElementById('glyphSuccessMsg');
const glyphErrorMsg = document.getElementById('glyphErrorMsg');
if (/^[A-Fa-f0-9]{1,2}$/.test(glyphInput)) {
Glyph(glyphInput || "E0");
glyphSuccessMsg.textContent = 'Glyph generated successfully!';
glyphSuccessMsg.classList.remove('d-none');
glyphErrorMsg.classList.add('d-none');
renderGlyphs();
} else {
glyphErrorMsg.textContent = 'Please enter a valid hex value (1-2 hex digits).';
glyphErrorMsg.classList.remove('d-none');
glyphSuccessMsg.classList.add('d-none');
}
});
document.getElementById('conversionModeButton').addEventListener('click', function () {
isHexToEmoji = !isHexToEmoji;
this.textContent = isHexToEmoji ? "Hex to Emoji" : "Emoji to Hex";
document.getElementById('inputPrefix').style.display = isHexToEmoji ? "inline-block" : "none";
document.getElementById('converterInput').placeholder = "Enter hex value or emoji/symbol";
document.getElementById('converterOutput').value = '';
updateCopyButtonState();
});
document.getElementById('convertButton').addEventListener('click', convert);
document.getElementById('copyButton').addEventListener('click', copyOutput);
document.getElementById('converterInput').addEventListener('input', convert);
document.getElementById('darkModeToggle').addEventListener('click', toggleDarkMode);
document.getElementById('glyphUpload').addEventListener('change', function (e) {
const file = e.target.files[0];
if (!file) return;
const fileNameRegex = /^glyph_([0-9A-F]{2})\.png$/i;
const match = file.name.match(fileNameRegex);
if (!match) {
alert('Invalid file name. Please use the format glyph_XX.png where XX is a hex value from 00 to FF.');
return;
}
const hexValue = match[1].toUpperCase();
const reader = new FileReader();
reader.onload = function (event) {
const img = new Image();
img.onload = function () {
processGlyph(img, hexValue);
};
img.src = event.target.result;
};
reader.readAsDataURL(file);
});
window.addEventListener('scroll', function () {
hideZoomWindow();
});
window.addEventListener('scroll', function () {
if (zoomWindow) {
zoomWindow.style.bottom = '20px';
}
});
// Add CSS for zoom window
const style = document.createElement('style');
style.textContent = `
.zoom-window {
border-radius: 5px;
background: white;
padding: 10px;
box-shadow: 0 0 15px rgba(0,0,0,0.2);
}
.zoom-window canvas {
display: block;
image-rendering: pixelated;
width: 200px;
height: 200px;
}
.zoom-window .zoom-info {
margin-top: 5px;
font-size: 12px;
text-align: center;
}
#glyph-output div {
background-repeat: no-repeat;
image-rendering: pixelated;
}
`;
document.head.appendChild(style);
// Mobile Alert functionality
document.addEventListener('DOMContentLoaded', function () {
const mobileAlert = document.getElementById('mobileAlert');
function checkScreenSize() {
if (window.innerWidth < 768) {
mobileAlert.style.display = 'block';
} else {
mobileAlert.style.display = 'none';
}
}
checkScreenSize();
window.addEventListener('resize', checkScreenSize);
});