-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
295 lines (294 loc) · 10.6 KB
/
index.html
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>gchessboard Demo</title>
<script src="https://cdn.tailwindcss.com?plugins=forms"></script>
<script src="https://unpkg.com/chess.js@0.12.0/chess.js"></script>
<script type="module" src="/dist/index.es.js"></script>
<script type="module">
window.addEventListener("DOMContentLoaded", () => {
const board = document.getElementById("board");
let arrows = [
{ from: "c7", to: "f4" },
{ from: "b3", to: "f4", brush: "foobar", weight: "light" },
{ from: "g7", to: "e3", brush: "secondary" },
];
let game = new Chess();
window.placement = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR";
window.turn = "either";
function updateGame() {
if (window.turn === "either") {
return;
}
const turn = window.turn === "white" ? "w" : "b";
const fen = `${window.placement} ${turn} - - 0 1`;
game.load(fen);
}
document.getElementById("btn-flip").addEventListener("click", () => {
board.orientation = board.orientation === "black" ? "white" : "black";
});
document
.getElementById("chk-interactive")
.addEventListener("change", (e) => {
board.interactive = e.target.checked;
});
document.getElementById("chk-svg").addEventListener("change", (e) => {
const svgSquares = ["a3", "c5", "e4"];
window.showSvg = e.target.checked;
if (window.showSvg) {
svgSquares.forEach((s) => {
const square = document
.getElementById(`svg-${s}`)
.content.firstElementChild.cloneNode(true);
square.slot = s;
document.getElementById("board").appendChild(square);
});
board.arrows = arrows;
} else {
svgSquares.forEach((s) => {
document
.getElementById("board")
.removeChild(document.querySelector(`#board [slot="${s}"]`));
});
board.arrows = undefined;
}
board.classList.toggle("custom-queen", e.target.checked);
});
document
.getElementById("sel-coords")
.addEventListener("change", (e) => {
board.coordinates = e.target.value;
});
document
.getElementById("sel-slowdown")
.addEventListener("change", (e) => {
let duration;
switch (e.target.value) {
case "normal":
duration = 150;
break;
case "slow":
duration = 3500;
break;
case "instant":
duration = 0;
break;
default:
duration = 10;
break;
}
board.animationDuration = duration;
});
document.getElementById("sel-turn").addEventListener("change", (e) => {
board.turn = e.target.value === "either" ? undefined : e.target.value;
window.turn = e.target.value;
updateGame();
});
document
.getElementById("btn-position-start")
.addEventListener("click", () => {
window.placement = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR";
board.fen = "start";
updateGame();
});
document
.getElementById("btn-position-puzzle-1")
.addEventListener("click", () => {
const fen = "8/5p2/8/2b2k2/2P4P/4rPP1/R5K1/8";
window.placement = fen;
board.fen = fen;
updateGame();
});
document
.getElementById("btn-position-puzzle-2")
.addEventListener("click", () => {
const fen = "r3kb1r/ppp2ppp/8/8/1PPp2bq/P3PP2/1B2K2P/RN1Q1B1R";
window.placement = fen;
board.fen = fen;
updateGame();
});
document
.getElementById("btn-position-puzzle-3")
.addEventListener("click", () => {
const fen = "3r1r1k/pp5P/2p2p2/5q2/2P2P2/2Q1P3/PP2K1P1/3R3R";
window.placement = fen;
board.fen = fen;
updateGame();
});
board.addEventListener("movestart", (e) => {
console.log(
`movestart: ${e.detail.piece.color} ${e.detail.piece.pieceType} on ${e.detail.from}`
);
if (window.turn !== "either") {
e.detail.setTargets(
game
.moves({ square: e.detail.from, verbose: true })
.map((m) => m.to)
);
}
});
board.addEventListener("movefinished", (e) => {
console.log(
`movefinished: ${e.detail.piece.color} ${e.detail.piece.pieceType} from ${e.detail.from}-${e.detail.to}`
);
window.placement = board.fen;
const movingArrow = { from: e.detail.to, to: "e4" };
if (!window.movingArrowStartSquare) {
arrows = arrows.concat([movingArrow]);
} else {
arrows[arrows.length - 1] = movingArrow;
}
window.movingArrowStartSquare = e.detail.to;
if (window.showSvg) {
board.arrows = arrows;
}
updateGame();
});
});
</script>
<style type="text/tailwindcss">
.btn {
@apply bg-green-800 text-gray-100 text-sm px-4 py-2 rounded-md hover:bg-green-700 active:bg-green-600 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-green-400 focus-visible:ring-offset-gray-100 dark:focus-visible:ring-offset-gray-900 dark:focus-visible:ring-green-300/75;
}
.input-label {
@apply inline-flex items-center mx-2 text-sm gap-x-2;
}
.chk {
@apply rounded text-green-600 border-gray-300 focus:ring focus:ring-green-400 focus:ring-offset-1 focus:ring-offset-gray-100 dark:border-transparent dark:focus:ring-offset-gray-900 dark:focus:ring-green-300/75;
}
.sel {
@apply rounded-md px-2 py-1 w-24 text-gray-900 text-sm border-gray-300 focus:border-green-300 focus:ring focus:ring-green-400 focus:ring-offset-1 focus:ring-offset-gray-100 dark:border-transparent dark:focus:border-transparent dark:focus:ring-offset-gray-900 dark:focus:ring-green-300/75;
}
</style>
<style>
g-chess-board.custom-queen::part(piece-bq) {
background-image: none;
display: flex;
justify-content: center;
align-items: center;
}
g-chess-board.custom-queen::part(piece-bq)::before {
color: black;
font-size: 1.5rem;
/* Black queen unicode */
content: "\2655";
}
g-chess-board.custom-queen::part(arrow-foobar) {
color: salmon;
opacity: 0.8;
}
</style>
</head>
<body class="text-gray-900 bg-gray-100 dark:bg-gray-900 dark:text-gray-100">
<template id="svg-a3">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
role="img"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
</template>
<template id="svg-c5">
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
role="img"
fill="currentColor"
class="text-orange-300"
>
<path
d="M11 17a1 1 0 001.447.894l4-2A1 1 0 0017 15V9.236a1 1 0 00-1.447-.894l-4 2a1 1 0 00-.553.894V17zM15.211 6.276a1 1 0 000-1.788l-4.764-2.382a1 1 0 00-.894 0L4.789 4.488a1 1 0 000 1.788l4.764 2.382a1 1 0 00.894 0l4.764-2.382zM4.447 8.342A1 1 0 003 9.236V15a1 1 0 00.553.894l4 2A1 1 0 009 17v-5.764a1 1 0 00-.553-.894l-4-2z"
/>
</svg>
</template>
<template id="svg-e4">
<svg
xmlns="http://www.w3.org/2000/svg"
class="top-auto left-auto w-1/3 bottom-1 right-1"
role="img"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
</template>
<main
class="flex flex-col items-center max-w-2xl min-h-screen px-3 py-8 mx-auto font-sans gap-y-5"
>
<h1 class="max-w-lg text-xl font-semibold text-center sm:text-2xl">
<code class="font-mono"><g-chess-board></code>
development demo
</h1>
<g-chess-board
id="board"
class="max-w-md"
fen="start"
animation-duration="150"
interactive
></g-chess-board>
<div class="flex flex-wrap justify-center py-2 gap-x-3 gap-y-3">
<button id="btn-flip" class="btn">Flip</button>
<label class="input-label">
<input
id="chk-interactive"
type="checkbox"
class="chk"
checked
autocomplete="off"
/>
<span>Interactive</span>
</label>
<label class="input-label">
<input id="chk-svg" type="checkbox" class="chk" autocomplete="off" />
<span>Show SVGs</span>
</label>
<label class="input-label">
<span>Coordinates</span>
<select id="sel-coords" class="sel" autocomplete="off">
<option>inside</option>
<option>outside</option>
<option>hidden</option>
</select>
</label>
<label class="input-label">
<span>Animation speed</span>
<select id="sel-slowdown" class="sel" autocomplete="off">
<option>normal</option>
<option>slow</option>
<option>instant</option>
</select>
</label>
<label class="input-label">
<span>Side to play</span>
<select id="sel-turn" class="sel" autocomplete="off">
<option>either</option>
<option>white</option>
<option>black</option>
</select>
</label>
</div>
<div class="flex flex-wrap justify-center py-2 gap-x-3 gap-y-2">
<button id="btn-position-start" class="btn">Start position</button>
<button id="btn-position-puzzle-1" class="btn">Puzzle 1</button>
<button id="btn-position-puzzle-2" class="btn">Puzzle 2</button>
<button id="btn-position-puzzle-3" class="btn">Puzzle 3</button>
</div>
</main>
</body>
</html>