-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
103 lines (82 loc) · 3.38 KB
/
app.js
File metadata and controls
103 lines (82 loc) · 3.38 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
const LS_KEY_PREFIX = "v1.editor.aruco.fodi.be";
const LS_KEY_SUFFIX_RESOLUTION = ".resolution";
let currentResolution = parseInt(localStorage.getItem(LS_KEY_PREFIX + LS_KEY_SUFFIX_RESOLUTION)) || 5; // try to load last used resolution from local storage or fall back to default value
let currentMode; // current painting mode, either "dark" or "light"
let currentCodeBin, currentCodeHex; // binary / hexadecimal representation of the current marker
// utility functions
const hex2bin = (hexString, numberOfBits) => parseInt(hexString, 16).toString(2).padStart(numberOfBits, "0");
const bin2hex = (binString) => "0x" + parseInt(binString, 2).toString(16) + "UL";
// table setup
const initTable = (resolution) => {
if (!parseInt(resolution)) { // fail if requested resolution cannot be parsed as an integer
alert("Invalid resolution.");
return;
}
currentResolution = resolution;
localStorage.setItem(LS_KEY_PREFIX + LS_KEY_SUFFIX_RESOLUTION, currentResolution); // save current resolution to local storage it can be automatically set on a page refresh
document.querySelector(":root").style.setProperty("--grid-resolution", resolution);
// generate table HTML
let tableHTML = "<table>";
for (let y = 0; y < resolution; y++) {
tableHTML += "<tr>";
for (let x = 0; x < resolution; x++) {
tableHTML += "<td id='cell" + (x + y) + "'></td>";
}
tableHTML += "</tr>";
}
tableHTML += "</table>";
// add table HTML to DOM
document.querySelector("#grid").innerHTML = tableHTML;
// paint and rig table cells for interaction
document.querySelectorAll("td").forEach((el) => {
el.classList.add("dark"); // set default mode for all cells
const ondown = (e) => {
currentMode = e.target.classList.contains("dark") ? "light" : "dark";
paintCell(e.target);
el.onpointermove = onmove;
el.setPointerCapture(e.pointerId);
};
const onup = (e) => {
el.onpointermove = null;
el.releasePointerCapture(e.pointerId);
updateCode();
};
const onmove = (e) => {
elRealTarget = document.elementFromPoint(e.x, e.y);
if (e.buttons && elRealTarget.localName === 'td') {
paintCell(elRealTarget);
}
};
el.onpointerdown = ondown;
el.onpointerup = onup;
});
updateCode();
};
// set one cell to the current mode (dark or light)
const paintCell = (el) => {
if (currentMode === "light") {
el.classList.remove("dark");
el.classList.add("light");
} else {
el.classList.remove("light");
el.classList.add("dark");
}
};
// generate and display the hexadecimal representation of the current marker
const updateCode = () => {
currentCodeBin = "";
document.querySelectorAll("td").forEach((el) => {
currentCodeBin += el.classList.contains("light") ? "1" : "0";
});
currentCodeHex = bin2hex(currentCodeBin);
document.getElementById("grid-info").textContent = currentCodeHex;
};
// prompt user for new resolution and reinit
const setResolution = () => {
const newResolution = prompt("This will clear the current code. Enter new resolution:", currentResolution);
if (newResolution) {
initTable(parseInt(newResolution));
}
};
// init on load
initTable(currentResolution);