-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.js
51 lines (44 loc) · 1.54 KB
/
ui.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
import Board from './board.js';
let start;
let end;
function showPath(tiles, delay) {
let gap = 70;
let offset = Math.floor(Math.random() * (360 - gap));
let colorIncrement = gap / tiles.length;
for (const [i, t] of tiles.entries()){
setTimeout(() => {
t.style['background-color'] = `hsl(${Math.floor(i * colorIncrement) + offset}, 30%, ${i * 4 + 30}%)`;
}, delay * i);
}
const tooltip = document.getElementById('tooltip');
setTimeout(() => tooltip.style.opacity = 1, 3000);
}
function trevail(start, end) {
const path = Board.knightMoves(start, end);
let tilePath = [];
path.forEach((node) => {
const tile = document.querySelector(`[data-file="${node.x}"]` + `[data-rank="${node.y}"]`);
tilePath.push(tile);
})
return tilePath;
}
function onTileClick(tile, cover) {
if (start === undefined) {
start = [Number(tile.dataset.file), Number(tile.dataset.rank)];
tile.classList.add('start');
cover.style.display = 'none';
} else {
end = [Number(tile.dataset.file), Number(tile.dataset.rank)];
cover.style.display = 'block';
let tilePath = trevail(start, end);
showPath(tilePath.reverse(), 180);
}
}
const board = document.getElementById('board');
const cover = document.getElementById('cover');
(function addListeners() {
board.childNodes.forEach((tile) => {
tile.addEventListener('click', () => onTileClick(tile, cover));
});
cover.addEventListener('click', () => location.reload());
})();