-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tile.js
42 lines (39 loc) · 1.16 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
export default class Tile{
#tileElement
#x
#y
#value
constructor(tileContainer, value = Math.random() > .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 backgroundColor = 100 - power*9;
this.#tileElement.style.setProperty('--background-lightness', `${backgroundColor}%`)
this.#tileElement.style.setProperty('--tile-lightness', `${backgroundColor <= 50 ? 90 : 10}%`)
}
set x(value){
this.#x = value;
this.#tileElement.style.setProperty('--x', value)
}
set y(value){
this.#x = value;
this.#tileElement.style.setProperty('--y', value)
}
remove(){
this.#tileElement.remove()
}
waitForTransition(){
return new Promise(resolve => {
this.#tileElement.addEventListener('trasitionend', resolve, { once: true, })
})
}
}