-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTile.js
58 lines (51 loc) · 1.41 KB
/
Tile.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
export default class {
#tileElement;
#x;
#y;
#value;
constructor(tileContainer, value = Math.random() > 0.5 ? 2 : 4) {
this.#tileElement = document.createElement("div");
this.#tileElement.classList.add("tile");
tileContainer.append(this.#tileElement);
this.value = value;
}
get value() {
return this.#value;
}
set value(v) {
this.#value = v;
this.#tileElement.textContent = v;
const power = Math.log2(v);
const backgroundLightness = 100 - power * 9;
this.#tileElement.style.setProperty(
"--background-lightness",
`${backgroundLightness}%`
);
this.#tileElement.style.setProperty(
"--text-lightness",
`${backgroundLightness <= 50 ? 90 : 10}%`
);
}
set x(value) {
this.#x = value;
this.#tileElement.style.setProperty("--x", value);
}
set y(value) {
this.#y = value;
this.#tileElement.style.setProperty("--y", value);
}
remove() {
this.#tileElement.remove();
}
waitForTransition(animation = false) {
return new Promise((resolve) => {
this.#tileElement.addEventListener(
animation ? "animationend" : "transitionend",
resolve,
{
once: true,
}
);
});
}
}