-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
394 lines (347 loc) · 12.1 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
document.addEventListener("DOMContentLoaded", () => {
const inputCanvas = document.getElementById("input-canvas");
const invertButton = document.getElementById("invert");
const widthInput = document.getElementById("width");
const heightInput = document.getElementById("height");
const downloadButton = document.getElementById("download");
const scaleSelector = document.getElementById("scale-selector");
const saveButton = document.getElementById("save");
const loadButton = document.getElementById("load");
const clearButton = document.getElementById("clear");
const rightArrow = document.getElementById("right-arrow");
const leftArrow = document.getElementById("left-arrow");
const whiteInput = document.getElementById("white-input");
const blackInput = document.getElementById("black-input");
// Easter egg for the close button
const closeButton = document.getElementById("close");
const svg = closeButton.querySelector("svg");
closeButton.addEventListener("click", () => {
svg.style.opacity === "0" ? svg.style.opacity = "1" : svg.style.opacity = "0";
});
const wallpaperCanvas = document.createElement("canvas");
const availablePresetFiles = [
"dots_alternate_inverted.json",
"dots_alternate_dense_inverted.json",
"checkerboard.json",
"dot_grid_inverted.json",
"dense_columns.json",
"ne_dense_diagonal_inverted.json",
"columns.json",
"squares.json",
"rain.json",
"square_grid_dense.json",
"bricks.json",
"dot.json",
"tread_plate.json",
"circle_tiles.json",
"fabric.json",
"trees.json",
"sine_wave.json",
"3d_grid.json",
"amogus.json",
"blank.json",
"dots.json",
"dots_alternate.json",
"dots_alternate_dense.json",
"dot_grid.json",
"dense_rows.json",
"ne_dense_diagonal.json",
"rows.json",
"ne_diagonal.json",
"center_dot_grid.json",
"square_grid.json",
"tiles45.json",
"quilt.json",
"acutes.json",
"wavy.json",
"scales.json",
"roof_tiles.json",
"cherry.json",
"diamond.json",
"black.json",
];
let black = blackInput.value;
let white = whiteInput.value;
blackInput.addEventListener("input", () => {
black = blackInput.value;
document.documentElement.style.setProperty("--black", black);
scheduleUpdate();
});
whiteInput.addEventListener("input", () => {
white = whiteInput.value;
document.documentElement.style.setProperty("--white", white);
scheduleUpdate();
});
let isMouseDown = false;
let drawColor = black;
let needsUpdate = false;
const generateWallpaper = () => {
const scale = parseInt(scaleSelector.value);
const pattern = [];
for (let row of inputCanvas.rows) {
const rowData = [];
for (let cell of row.cells) {
rowData.push(varToHex(cell.style.backgroundColor) === black ? 1 : 0);
}
pattern.push(rowData);
}
// Get the width and height of the inputs
const width = parseInt(widthInput.value);
const height = parseInt(heightInput.value);
// Create off-screen canvas with the 8x8 pattern
const patternCanvas = document.createElement("canvas");
patternCanvas.width = 8;
patternCanvas.height = 8;
const patternCtx = patternCanvas.getContext("2d");
for (let y = 0; y < 8; y++) {
for (let x = 0; x < 8; x++) {
patternCtx.fillStyle = pattern[y][x] ? black : white;
patternCtx.fillRect(x, y, 1, 1);
}
}
// Draw an image that will be used as background for the website
// the image will be half the size of the viewport
const backgroundCanvas = document.createElement("canvas");
backgroundCanvas.width = window.innerWidth / scale;
backgroundCanvas.height = window.innerHeight / scale;
const backgroundCtx = backgroundCanvas.getContext("2d");
for (let y = 0; y < backgroundCanvas.height; y += 8) {
for (let x = 0; x < backgroundCanvas.width; x += 8) {
backgroundCtx.drawImage(patternCanvas, x, y, 8, 8);
}
}
// Draw the background image on the website
// body.style.backgroundImage = `url(${backgroundCanvas.toDataURL()})`;
// save it as a css variable
document.documentElement.style.setProperty("--background-image", `url(${backgroundCanvas.toDataURL()})`);
// Draw the wallpaper on the wallpaperCanvas
wallpaperCanvas.width = width;
wallpaperCanvas.height = height;
const ctx = wallpaperCanvas.getContext("2d");
for (let y = 0; y < height; y += 8) {
for (let x = 0; x < width; x += 8) {
ctx.drawImage(patternCanvas, x, y, 8, 8);
}
}
}
const scheduleUpdate = () => {
if (!needsUpdate) {
needsUpdate = true;
requestAnimationFrame(() => {
generateWallpaper();
needsUpdate = false;
});
}
};
const hexToVar = (hex) => {
return hex === black ? "var(--black)" : "var(--white)";
};
const varToHex = (varString) => {
return varString === "var(--black)" ? black : white;
};
// Initialize 8x8 input canvas
for (let row = 0; row < 8; row++) {
const tr = document.createElement("tr");
for (let col = 0; col < 8; col++) {
const td = document.createElement("td");
td.style.backgroundColor = hexToVar(white);
if ((row % 2 === 0 && col % 2 === 0) || (row % 2 === 1 && col % 2 === 1)) {
td.style.backgroundColor = hexToVar(black);
}
td.addEventListener("mousedown", (event) => {
const initialColor = varToHex(event.target.style.backgroundColor);
drawColor = initialColor === black ? hexToVar(white) : hexToVar(black);
td.style.backgroundColor = drawColor;
isMouseDown = true;
scheduleUpdate();
});
td.addEventListener("mouseenter", (event) => {
if (isMouseDown) {
event.target.style.backgroundColor = drawColor;
scheduleUpdate();
}
});
tr.appendChild(td);
}
inputCanvas.appendChild(tr);
}
document.addEventListener("mouseup", () => {
isMouseDown = false;
});
// Invert the 8x8 design
invertButton.addEventListener("click", () => {
for (let row of inputCanvas.rows) {
for (let cell of row.cells) {
cell.style.backgroundColor = varToHex(cell.style.backgroundColor) === black ?
hexToVar(white) : hexToVar(black);
}
}
scheduleUpdate();
});
//validate input values no less than 8 for width and height on blur
widthInput.addEventListener("blur", () => {
if (widthInput.value < 8) {
widthInput.value = 8;
}
});
heightInput.addEventListener("blur", () => {
if (heightInput.value < 8) {
heightInput.value = 8;
}
});
// Update wallpaper when resizing the input canvas and validate input values
widthInput.addEventListener("input", () => {
if (!isNaN(widthInput.value)) {
scheduleUpdate();
}
});
heightInput.addEventListener("input", () => {
if (!isNaN(heightInput.value)) {
scheduleUpdate();
}
});
// Update wallpaper when changing the scale
scaleSelector.addEventListener("input", scheduleUpdate);
let selectorValueToScale = {
1: 1,
2: 2,
3: 4,
4: 8,
5: 10,
};
// When pressing the download button, download the wallpaperCavas as an image
// with the selected scale, resampling the image with the nearest-neighbor algorithm
downloadButton.addEventListener("click", (event) => {
const scale = selectorValueToScale[scaleSelector.value];
const downloadCanvas = document.createElement("canvas");
downloadCanvas.width = wallpaperCanvas.width * scale;
downloadCanvas.height = wallpaperCanvas.height * scale;
const ctx = downloadCanvas.getContext("2d");
ctx.imageSmoothingEnabled = false;
ctx.drawImage(wallpaperCanvas, 0, 0, downloadCanvas.width, downloadCanvas.height);
const link = document.createElement("a");
link.download = "wallpaper.png";
link.href = downloadCanvas.toDataURL();
link.click();
});
// Save the configuration of the 8x8 design as a JSON file
saveButton.addEventListener("click", (event) => {
event.preventDefault();
const pattern = [];
for (let row of inputCanvas.rows) {
const rowData = [];
for (let cell of row.cells) {
rowData.push(varToHex(cell.style.backgroundColor) === black ? 1 : 0);
}
pattern.push(rowData);
}
const json = JSON.stringify({
width: parseInt(widthInput.value),
height: parseInt(heightInput.value),
pattern,
});
const link = document.createElement("a");
link.download = "pattern.json";
link.href = URL.createObjectURL(
new Blob([json], { type: "application/json" })
);
link.click();
});
// Load a configuration from a JSON file
loadButton.addEventListener("click", () => {
const input = document.createElement("input");
input.type = "file";
input.accept = ".json";
input.onchange = () => {
const file = input.files[0];
const reader = new FileReader();
reader.onload = () => {
const json = JSON.parse(reader.result);
widthInput.value = json.width;
heightInput.value = json.height;
for (let row = 0; row < 8; row++) {
for (let col = 0; col < 8; col++) {
if (json.pattern[row][col] === 1) {
inputCanvas.rows[row].cells[col].style.backgroundColor = hexToVar(black);
} else {
inputCanvas.rows[row].cells[col].style.backgroundColor = hexToVar(white);
}
}
}
scheduleUpdate();
};
reader.readAsText(file);
};
input.click();
});
// Load a preset file when clicking on the arrows (scroll through the available presets)
// the preset files are in "./examplePatterns/macOs_system3_1986/" folder
let currentPresetIndex = -1;
rightArrow.addEventListener("click", () => {
currentPresetIndex = (currentPresetIndex + 1) % availablePresetFiles.length;
const presetFile = availablePresetFiles[currentPresetIndex];
fetch(`./examplePatterns/macOs_system3_1986/${presetFile}`)
.then((response) => response.json())
.then((json) => {
widthInput.value = json.width;
heightInput.value = json.height;
for (let row = 0; row < 8; row++) {
for (let col = 0; col < 8; col++) {
if (json.pattern[row][col] === 1) {
inputCanvas.rows[row].cells[col].style.backgroundColor = hexToVar(black);
} else {
inputCanvas.rows[row].cells[col].style.backgroundColor = hexToVar(white);
}
}
}
scheduleUpdate();
});
});
leftArrow.addEventListener("click", () => {
currentPresetIndex =
(currentPresetIndex - 1 + availablePresetFiles.length) %
availablePresetFiles.length;
const presetFile = availablePresetFiles[currentPresetIndex];
fetch(`./examplePatterns/macOs_system3_1986/${presetFile}`)
.then((response) => response.json())
.then((json) => {
widthInput.value = json.width;
heightInput.value = json.height;
for (let row = 0; row < 8; row++) {
for (let col = 0; col < 8; col++) {
if (json.pattern[row][col] === 1) {
inputCanvas.rows[row].cells[col].style.backgroundColor = hexToVar(black);
} else {
inputCanvas.rows[row].cells[col].style.backgroundColor = hexToVar(white);
}
}
}
scheduleUpdate();
});
});
// Clear the 8x8 design
clearButton.addEventListener("click", () => {
for (let row of inputCanvas.rows) {
for (let cell of row.cells) {
cell.style.backgroundColor = white;
}
}
scheduleUpdate();
});
// select all the text in the input when clicked
widthInput.addEventListener("click", () => {
widthInput.select();
});
heightInput.addEventListener("click", () => {
heightInput.select();
});
// Update on viewport resize
window.addEventListener("resize", scheduleUpdate);
// Initial wallpaper generation
scheduleUpdate();
});
// Set the range value when clicking on the labels
function setRangeValue(value) {
document.getElementById("scale-selector").value = value;
document.getElementById("scale-selector").dispatchEvent(new Event("input"));
}