forked from aeonic-dev/pixel-country-picker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (55 loc) · 1.33 KB
/
index.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
let div = document.querySelector("#flag");
x = 15;
y = 9;
let color = "black";
let down = false;
for (i = 0; i < x * y; i++) {
div.appendChild(document.createElement("span")).classList.add("pixel");
}
document.querySelectorAll("#colselect>span").forEach(s => {
s.addEventListener("click", e => {
color = e.target.id;
});
});
document.querySelectorAll(".pixel").forEach(s => {
s.addEventListener("mouseover", e => {
if (down) {
e.target.style.background = color;
document.querySelector("#selected").innerHTML = validate();
}
});
s.addEventListener("mousedown", e => {
e.target.style.background = color;
document.querySelector("#selected").innerHTML = validate();
});
});
div.addEventListener("mousedown", e => {
down = true;
});
div.addEventListener("mouseup", e => {
down = false;
});
function getArr() {
ret = [];
document.querySelectorAll(".pixel").forEach(s => {
ret.push(s.style.background);
});
return ret;
}
function validate() {
a = getArr().map(x => {
return x == "" ? "white" : x;
});
for (i = 0; i < flags.length; i++) {
if (
JSON.stringify(
flags[i].map(x => {
return x == "" ? "white" : x;
})
) == JSON.stringify(a.map(x => x.replace(" none repeat scroll 0% 0%", "")))
) {
return flagnames[i];
}
}
return "none";
}