-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
292 lines (251 loc) · 9.61 KB
/
main.js
File metadata and controls
292 lines (251 loc) · 9.61 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
// constants for quick modifications
const CANVAS_SCALE = 10;
const PREVIEW_SCALE = 10;
// set your photo
const IMAGE_URL = 'photo.jpg';
// set your background and LED colors in hex
const BACKGROUND_COLOR = '#820075';
const LED_COLOR = '#FFFFFF';
// set your displays superimposed onto the image
const DISPLAYS = [
{
id: 'eye',
width: 16,
height: 8,
transform: 'translateX(-54.2px) translateY(-11px) rotateX(-9deg) rotateY(-50deg) rotateZ(10deg) scaleX(0.342) scaleY(0.31)'
},
{
id: 'nose',
width: 8,
height: 8,
transform: 'translateX(47px) translateY(18px) rotateX(-8deg) rotateY(-50deg) rotateZ(10deg) scaleX(0.35) scaleY(0.31)'
},
{
id: 'mouth',
width: 32,
height: 8,
transform: 'translateX(-112px) translateY(51px) rotateX(-9deg) rotateY(-50deg) rotateZ(10deg) scaleX(0.335) scaleY(0.31)'
},
];
const messageSection = document.getElementById('message');
const canvasSection = document.getElementById('canvases');
const photoSection = document.getElementById('photo');
photoSection.style.backgroundImage = 'url(' + IMAGE_URL + ')';
// state and events
let isDrawing = false;
let drawColor = LED_COLOR;
DISPLAYS.forEach(display => {
// create canvas
const canvas = document.createElement('canvas');
canvas.id = display.id;
canvas.width = display.width;
canvas.height = display.height;
canvas.style.width = display.width * CANVAS_SCALE + 'px';
canvas.style.height = display.height * CANVAS_SCALE + 'px';
const ctx = canvas.getContext('2d');
resetCanvas(canvas);
canvas.addEventListener('mousemove', drawPixel);
canvas.addEventListener('mousedown', (e) => {
drawColor = e.button === 2 ? BACKGROUND_COLOR : LED_COLOR;
isDrawing = true;
drawPixel(e);
});
// functions
function drawPixel(e) {
if (!isDrawing) return;
const rect = canvas.getBoundingClientRect();
const scaleX = canvas.width / rect.width;
const scaleY = canvas.height / rect.height;
const x = Math.floor((e.clientX - rect.left) * scaleX);
const y = Math.floor((e.clientY - rect.top) * scaleY);
ctx.fillStyle = drawColor;
ctx.fillRect(x, y, 1, 1);
updatePreview(canvas);
}
// create DOM for each canvas
{
// copy button
const copyBtn = document.createElement('button');
copyBtn.textContent = 'copy';
copyBtn.onclick = () => {
copyCanvas(canvas, canvas.id);
}
// save button
const saveBtn = document.createElement('button');
saveBtn.textContent = 'save';
saveBtn.onclick = () => {
saveCanvas(canvas, display.id);
}
// paste button
const loadBtn = document.createElement('button');
loadBtn.textContent = 'load';
loadBtn.onclick = () => {
resetCanvas(canvas);
loadCanvas(canvas, canvas.id);
}
// reset button
const resetBtn = document.createElement('button');
resetBtn.classList.add('reset');
resetBtn.textContent = 'reset';
resetBtn.onclick = () => {
resetCanvas(canvas);
showMessage(canvas.id + ' has been reset.');
updatePreview(canvas);
}
// canvas button section
const btnContainer = document.createElement('div');
btnContainer.classList.add('container', 'btn-container');
btnContainer.appendChild(copyBtn);
btnContainer.appendChild(saveBtn);
btnContainer.appendChild(getSpacer());
btnContainer.appendChild(loadBtn);
btnContainer.appendChild(resetBtn);
// canvas section
const canvasContainer = document.createElement('div');
canvasContainer.classList.add('canvas-container');
// title section
const title = document.createElement('p');
title.classList.add('bold');
title.innerHTML = canvas.id;
const size = document.createElement('p');
size.innerHTML = canvas.width+'x'+canvas.height;
const titleContainer = document.createElement('div');
titleContainer.classList.add('container');
titleContainer.appendChild(title);
titleContainer.appendChild(getSpacer());
titleContainer.appendChild(size);
// main canvas container
canvasContainer.appendChild(titleContainer);
canvasContainer.appendChild(canvas);
canvasContainer.appendChild(btnContainer);
canvasSection.appendChild(canvasContainer);
// preview
const preview = document.createElement('div');
preview.className = 'preview';
preview.style.transform = display.transform;
const img = document.createElement('img');
img.id = display.id + '-preview';
img.width = display.width;
img.height = display.height;
img.style.width = display.width * PREVIEW_SCALE + 'px';
img.style.height = display.height * PREVIEW_SCALE + 'px';
img.src = canvas.toDataURL();
preview.appendChild(img);
photoSection.appendChild(preview);
}
});
function getSpacer() {
const spacer = document.createElement('div');
spacer.classList.add('spacer');
return spacer;
}
function updatePreview(canvas) {
const img = document.getElementById(canvas.id + '-preview');
img.src = canvas.toDataURL();
}
function resetCanvas(canvas) {
const ctx = canvas.getContext('2d');
ctx.fillStyle = BACKGROUND_COLOR;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
function saveCanvas(canvas, name) {
const tempCanvas = document.createElement('canvas');
tempCanvas.width = canvas.width;
tempCanvas.height = canvas.height;
const ctx = tempCanvas.getContext('2d');
ctx.drawImage(canvas, 0, 0);
const imageData = ctx.getImageData(0, 0, tempCanvas.width, tempCanvas.height);
const data = imageData.data;
for (let i = 0; i < data.length; i += 4) {
const hex = '#' + ((data[i] << 16) | (data[i+1] << 8) | data[i+2]).toString(16);
for(let j = 0; j < 3; j++) {
data[i+j] = (hex.toUpperCase() == LED_COLOR.toUpperCase())
? 0xff
: 0x00;
}
data[i+3] = 0xff;
}
ctx.putImageData(imageData, 0, 0);
const image = tempCanvas.toDataURL('image/png');
const link = document.createElement('a');
link.download = name + ".png";
link.href = image;
link.click();
}
function copyCanvas(canvas, id = '') {
try {
const ctx = canvas.getContext('2d');
const bytes = [];
for(let row = 0; row < canvas.width; row++) {
let result = 0;
for(let bit = 7; bit >= 0; bit--) {
const point = matrixToPoint(bit, row);
const data = ctx.getImageData(point[0], point[1], 1, 1).data;
const hex = '#' + ((data[0] << 16) | (data[1] << 8) | data[2]).toString(16);
result = (result << 1) | ((hex.toUpperCase() == LED_COLOR.toUpperCase()) ? 1 : 0);
}
bytes.push(result);
}
let result = '';
if(getBitmapFormat() == 'binary') {
result = bytes.map(byte => {
return '0b' + byte.toString(2).padStart(8, '0');
}).join(',\n');
} else {
result = bytes.map(byte => {
return '0x' + byte.toString(16).padStart(2, '0');
}).join(', ');
}
navigator.clipboard.writeText(result);
showMessage('Copied ' + id + ' to clipboard: <br><br>'+result.replaceAll('\n', '<br>'));
} catch (err) {
showMessage(err);
}
}
function loadCanvas(canvas, id = '') {
navigator.clipboard.readText()
.then(text => {
// parse text
const values = text.match(/\b0[bB][01]+|\b0[xX][0-9a-fA-F]+\b/g);
const bytes = values.map(value => {
if (/^0[bB][01]+$/.test(value)) return parseInt(value.replace(/^0[bB]/, ''), 2);
if (/^0[xX][0-9a-fA-F]+$/.test(value)) return parseInt(value, 16);
return 0;
});
// display on cavnas
const ctx = canvas.getContext('2d');
ctx.fillStyle = LED_COLOR;
for(const row in bytes) {
const byte = bytes[row];
for(let i = 0; i < 8; i++) {
const point = matrixToPoint(i, row);
if((byte >> i) & 1) ctx.fillRect(point[0], point[1], 1, 1);
}
}
updatePreview(canvas);
showMessage(
(bytes.length % 8) == 0
? 'Successfully loaded ' + id + ' from clipboard'
: 'Successfully loaded ' + id + ' from clipboard, but bitmap row count is not divisible by 8. Did you copy the whole bitmap?'
);
})
.catch(err => {
showMessage(err);
})
}
function showMessage(message) {
messageSection.innerHTML = message;
}
function getBitmapFormat() {
return document.getElementById('binary').checked ? 'binary' : 'hex';
}
function matrixToPoint(bit, row) {
const offset = Math.floor(row / 8) * 8;
return [(7 - bit + offset), (row - offset)];
}
document.addEventListener('contextmenu', e => e.preventDefault());
document.addEventListener('mouseup', () => isDrawing = false);
document.addEventListener('mousedown', (e) => {
drawColor = e.button === 2 ? BACKGROUND_COLOR : LED_COLOR;
isDrawing = true;
});