-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chessboard.js
58 lines (52 loc) · 1.55 KB
/
Chessboard.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
const Chessboard = function(domNode) {
const cols = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
let selected;
let pieces = [];
const getSquareSize = (board) => {
const rect = board.getBoundingClientRect();
return {
cell: {
width: Math.floor(rect.width / 8),
height: Math.floor(rect.height / 8)
},
left: rect.left,
top: rect.top
}
}
domNode.addEventListener("click", (event) => {
const { left, top, cell } = getSquareSize(event.currentTarget)
const x = Math.floor((event.clientX - left) / cell.width)
const y = Math.ceil((event.clientY - top) / cell.height)
if (event.target !== event.currentTarget) {
if (!selected) {
selected = event.target;
selected.classList.add("selected")
} else if (selected !== event.target) {
selected.classList.remove("selected");
selected = event.target;
selected.classList.add("selected");
} else {
selected.classList.remove("selected");
selected = undefined;
}
} else {
if (selected) {
const currentPiece = pieces.find( piece =>
piece.domEl().classList.value === selected.classList.value
);
if(currentPiece) currentPiece.to(cols[x]+y)
// selected.style.gridArea = `${cols[x] + y}`;
selected.classList.remove("selected");
selected = undefined;
}
}
})
const add = (piece) => {
pieces = [...pieces, piece]
domNode.appendChild(piece.domEl() );
}
return {
add
}
}
export default Chessboard;