-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.js
64 lines (56 loc) · 1.57 KB
/
interface.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
let cellSizes = [32, 64, 128];
let cellSizePixels = 64;
let artboard = {};
let grid = {};
const loadInterface = () => {
const canvas = document.querySelector("canvas");
const palette = selectPalette();
artboard = new Artboard({
canvas,
palette,
dpr: window.devicePixelRatio,
});
grid = createGrid({
cellSizePixels,
gridWidthPixels: artboard.width,
gridHeightPixels: artboard.height,
});
setPalette();
document.querySelector("#render").addEventListener("click", (event) => {
event.preventDefault();
[flipPalette, swapPalette][Math.floor(Math.random() * 2)]();
swapCellSize();
grid.resize(cellSizePixels);
artboard.clear();
render({ artboard, grid });
});
document.querySelector("#download").addEventListener("click", (event) => {
event.preventDefault();
let imageData = canvas.toDataURL();
let downloader = document.createElement("a");
downloader.download = "artgrid.png";
downloader.href = imageData;
downloader.click();
});
};
const setPalette = () => {
const root = document.querySelector(":root");
root.style.setProperty("--bg-color", artboard.bgColor());
root.style.setProperty("--fg-color", artboard.fgColor());
};
const swapPalette = () => {
artboard.swapPalette();
setPalette();
};
const flipPalette = () => {
artboard.flipPalette();
setPalette();
};
const swapCellSize = () => {
cellSizePixels = cellSizes[Math.floor(Math.random() * cellSizes.length)];
grid = createGrid({
cellSizePixels,
gridWidthPixels: artboard.width,
gridHeightPixels: artboard.height,
});
};