@alepot55/chessboardjs 2.1.6 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -37,6 +37,51 @@ const config = {
37
37
  const board = new Chessboard(config);
38
38
  ```
39
39
 
40
+ ## Configuration
41
+
42
+ Configuration is done by passing an object to the constructor. Below is an example with the main attributes and a brief description:
43
+
44
+ ```javascript
45
+ const config = {
46
+ id: 'board', // ID of the HTML element where the chessboard is rendered
47
+ piecesPath: 'path/to/pieces', // Path (or function) to retrieve piece images
48
+ position: 'start', // Initial position ('start' for standard setup or a FEN string)
49
+ size: 400, // Chessboard size (pixels number or 'auto')
50
+ orientation: 'w', // Board orientation: 'w' (white at the bottom) or 'b' (black at the bottom)
51
+ draggable: true, // Enable dragging of pieces
52
+ clickable: true, // Enable piece selection via click
53
+ onlyLegalMoves: true, // Allow only legal moves
54
+ moveAnimation: 'ease', // Transition function for move animations (e.g., 'ease', 'linear')
55
+ moveTime: 'fast', // Duration for move animation ('fast', 'normal', 'slow', etc.)
56
+ snapbackAnimation: 'ease', // Transition function for snapback animation (when an invalid move occurs)
57
+ snapbackTime: 'fast', // Duration for snapback animation
58
+ fadeAnimation: 'ease', // Transition function for fade animations
59
+ fadeTime: 'fast', // Duration for fade animation
60
+ whiteSquare: '#f0d9b5', // Color of white squares
61
+ blackSquare: '#b58863', // Color of black squares
62
+ highlight: 'yellow', // Color used to highlight moves
63
+ selectedSquareWhite: '#ababaa',// Highlight color for selected white squares
64
+ selectedSquareBlack: '#ababaa',// Highlight color for selected black squares
65
+ movedSquareWhite: '#f1f1a0', // Color to indicate moved white squares
66
+ movedSquareBlack: '#e9e981', // Color to indicate moved black squares
67
+ choiceSquare: 'white', // Color used to indicate square selection
68
+ coverSquare: 'black', // Color used to cover squares during certain interactions
69
+ hintColor: '#ababaa', // Color for move hints
70
+ // Optional event callbacks:
71
+ onMove: (move) => { // Called when a move is attempted
72
+ console.log('Move attempted:', move);
73
+ return true;
74
+ },
75
+ onMoveEnd: (move) => { // Called when a move is completed
76
+ console.log('Move executed:', move);
77
+ },
78
+ // ...other callbacks and options...
79
+ };
80
+
81
+ // Usage:
82
+ const board = new Chessboard(config);
83
+ ```
84
+
40
85
  ## API Documentation
41
86
 
42
87
  ### Constructor
@@ -94,7 +139,7 @@ const board = new Chessboard(config);
94
139
  ```javascript
95
140
  board.build();
96
141
  ```
97
-
142
+
98
143
  - **resize(value)**
99
144
  - Dynamically resizes the board. Accepts a number (pixels) or 'auto'.
100
145
  - Example:
@@ -133,4 +178,269 @@ const board = new Chessboard(config);
133
178
  console.log('FEN:', board.fen());
134
179
  ```
135
180
 
136
- For further details, refer to the full API documentation at the project website or within the source code.
181
+ For further details, refer to the full API documentation at the project website or within the source code.
182
+
183
+ # Chessboard.js – User API & Chess.js Integration
184
+
185
+ This document details the functions available to users for interacting with the Chessboard.js instance and the underlying Chess.js game. The functions are grouped by purpose and presented in a logical order.
186
+
187
+ ---
188
+
189
+ ## 1. Board Setup & Initialization
190
+
191
+ - **build()**
192
+ Initializes (or rebuilds) the board and its elements.
193
+ _Example:_
194
+ ```js
195
+ board.build();
196
+ ```
197
+
198
+ - **clear(options = {}, animation = true)**
199
+ Clears the board of all pieces and updates the display.
200
+ _Example:_
201
+ ```js
202
+ board.clear();
203
+ ```
204
+
205
+ - **reset(animation = true)**
206
+ Resets the game to the starting position.
207
+ _Example:_
208
+ ```js
209
+ board.reset();
210
+ ```
211
+
212
+ ---
213
+
214
+ ## 2. Orientation
215
+
216
+ - **getOrientation()**
217
+ Returns the current board orientation.
218
+ _Example:_
219
+ ```js
220
+ console.log("Orientation:", board.getOrientation());
221
+ ```
222
+
223
+ - **setOrientation(color, animation = true)**
224
+ Sets the board’s orientation. If an invalid color is passed, it flips the board.
225
+ _Example:_
226
+ ```js
227
+ try {
228
+ board.setOrientation('w');
229
+ } catch (e) {
230
+ console.error(e.message);
231
+ }
232
+ ```
233
+
234
+ - **flip(animation = true)**
235
+ Flips the board orientation (updates pieces and clears highlights).
236
+ _Example:_
237
+ ```js
238
+ board.flip();
239
+ ```
240
+
241
+ ---
242
+
243
+ ## 3. Game State & Display
244
+
245
+ - **ascii()**
246
+ Returns an ASCII diagram representing the current position.
247
+ _Example:_
248
+ ```js
249
+ console.log(board.ascii());
250
+ ```
251
+
252
+ - **board()**
253
+ Returns the current board as a 2D array.
254
+ _Example:_
255
+ ```js
256
+ console.table(board.board());
257
+ ```
258
+
259
+ - **fen()**
260
+ Retrieves the FEN string for the current board state.
261
+ _Example:_
262
+ ```js
263
+ console.log("FEN:", board.fen());
264
+ ```
265
+
266
+ ---
267
+
268
+ ## 4. Accessors & History
269
+
270
+ - **get(squareId)**
271
+ Returns the piece on the specified square.
272
+ _Example:_
273
+ ```js
274
+ const piece = board.get('e4');
275
+ console.log("Piece at e4:", piece);
276
+ ```
277
+
278
+ - **getCastlingRights(color)**
279
+ Returns the castling rights for the given color.
280
+ _Example:_
281
+ ```js
282
+ console.log("Castling rights for white:", board.getCastlingRights('w'));
283
+ ```
284
+
285
+ - **getComment()** and **getComments()**
286
+ Retrieves a single comment or all comments attached to positions.
287
+ _Example:_
288
+ ```js
289
+ console.log("Comment:", board.getComment());
290
+ ```
291
+
292
+ - **history(options = {})**
293
+ Returns the moves history, optionally verbose.
294
+ _Example:_
295
+ ```js
296
+ console.log("History:", board.history({ verbose: true }));
297
+ ```
298
+
299
+ - **lastMove()**
300
+ Returns the last move made.
301
+ _Example:_
302
+ ```js
303
+ console.log("Last move:", board.lastMove());
304
+ ```
305
+
306
+ - **moveNumber()**
307
+ Returns the current move number.
308
+ _Example:_
309
+ ```js
310
+ console.log("Move number:", board.moveNumber());
311
+ ```
312
+
313
+ - **moves(options = {})**
314
+ Provides a list of legal moves.
315
+ _Example:_
316
+ ```js
317
+ console.log("Legal moves:", board.moves());
318
+ ```
319
+
320
+ - **pgn(options = {})**
321
+ Returns the game in PGN format.
322
+ _Example:_
323
+ ```js
324
+ console.log("PGN:", board.pgn());
325
+ ```
326
+
327
+ - **squareColor(squareId)**
328
+ Returns the color (light or dark) of the specified square.
329
+ _Example:_
330
+ ```js
331
+ console.log("Square e4 is:", board.squareColor('e4'));
332
+ ```
333
+
334
+ - **turn()**
335
+ Indicates which side's turn it is ('w' or 'b').
336
+ _Example:_
337
+ ```js
338
+ console.log("Turn:", board.turn());
339
+ ```
340
+
341
+ ---
342
+
343
+ ## 5. Game State Checks
344
+
345
+ - **isCheckmate()**
346
+ Checks if the current position is checkmate.
347
+ _Example:_
348
+ ```js
349
+ if (board.isCheckmate()) console.log("Checkmate!");
350
+ ```
351
+
352
+ - **isDraw()**
353
+ Checks if the game is drawn.
354
+ - **isDrawByFiftyMoves()**
355
+ - **isInsufficientMaterial()**
356
+ - **isGameOver()**
357
+ - **isStalemate()**
358
+ - **isThreefoldRepetition()**
359
+ Each returns a Boolean indicating the respective state.
360
+ _Example:_
361
+ ```js
362
+ if (board.isGameOver()) console.log("Game over!");
363
+ ```
364
+
365
+ ---
366
+
367
+ ## 6. Game Modification
368
+
369
+ - **load(fen, options = {}, animation = true)**
370
+ Loads a new position from a FEN string.
371
+ _Example:_
372
+ ```js
373
+ board.load('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR');
374
+ ```
375
+
376
+ - **loadPgn(pgn, options = {}, animation = true)**
377
+ Loads a game using a PGN string.
378
+ _Example:_
379
+ ```js
380
+ board.loadPgn(pgnData);
381
+ ```
382
+
383
+ - **put(pieceId, squareId, animation = true)**
384
+ Places a piece on the board.
385
+ _Example:_
386
+ ```js
387
+ board.put({ type: 'p', color: 'w' }, 'd4');
388
+ ```
389
+
390
+ - **remove(squareId, animation = true)**
391
+ Removes the piece from the given square.
392
+ _Example:_
393
+ ```js
394
+ const removed = board.remove('d4');
395
+ console.log("Removed:", removed);
396
+ ```
397
+
398
+ - **removeComment()**, **removeComments()**, **removeHeader(field)**
399
+ Remove comments or PGN headers from the game.
400
+ _Example:_
401
+ ```js
402
+ board.removeComment();
403
+ ```
404
+
405
+ - **setCastlingRights(color, rights)**
406
+ Sets castling rights for a specified color.
407
+ _Example:_
408
+ ```js
409
+ board.setCastlingRights('w', { k: false, q: true });
410
+ ```
411
+
412
+ - **setComment(comment)** and **setHeader(key, value)**
413
+ Attach a comment to the current position or set a PGN header.
414
+ _Example:_
415
+ ```js
416
+ board.setComment("King's pawn opening");
417
+ board.setHeader('White', 'PlayerOne');
418
+ ```
419
+
420
+ ---
421
+
422
+ ## 7. Move Reversal
423
+
424
+ - **undo()**
425
+ Reverts the last move played.
426
+ _Example:_
427
+ ```js
428
+ const undone = board.undo();
429
+ if (undone) console.log("Move undone:", undone);
430
+ ```
431
+
432
+ ---
433
+
434
+ ## 8. Utility
435
+
436
+ - **validateFen(fen)**
437
+ Validates a given FEN string.
438
+ _Example:_
439
+ ```js
440
+ const result = board.validateFen('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1');
441
+ console.log("FEN valid?", result.ok);
442
+ ```
443
+
444
+ ---
445
+
446
+ This documentation groups the API functions by their purpose. Developers can mix these methods to query the game state, update the board, load new positions, and more.