-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tile.js
39 lines (39 loc) · 1.11 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
export default class Tile {
#tileElement;
#x;
#y;
#value;
constructor(gridElement, value = Math.random() < 0.9 ? 2 : 4) {
this.#tileElement = document.createElement('div')
this.#tileElement.classList.add('tile')
gridElement.append(this.#tileElement)
this.value = value
}
get value() {
return this.#value
}
set value(v) {
this.#tileElement.textContent = v
this.#value = 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 })
})
}
}