cm-chessboard 7.7.8 → 7.7.11
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 +71 -54
- package/examples/extensions/markers-extension.html +27 -21
- package/index.html +4 -1
- package/package.json +1 -1
- package/src/Chessboard.js +10 -0
- package/src/extensions/arrows/Arrows.js +0 -2
- package/src/lib/Svg.js +6 -2
package/README.md
CHANGED
|
@@ -8,7 +8,9 @@ in [chess-console](https://shaack.com/projekte/chess-console/examples/load-pgn.h
|
|
|
8
8
|
[cm-fen-editor](https://shaack.com/projekte/cm-fen-editor/). They are all nice written ES6 Modules to handle different
|
|
9
9
|
aspects of chess games.
|
|
10
10
|
|
|
11
|
-
> Note: With version 7, I made a heavy allover refactoring. The chessboard props have been changed and the files
|
|
11
|
+
> Note: With version 7, I made a heavy allover refactoring. The chessboard props have been changed and the files
|
|
12
|
+
> structure also. Version 7 of the cm-chessboard will not work out of the box after an update from a previous version.
|
|
13
|
+
> Also with version 7 comes the move cancelling via secondary mouse button.
|
|
12
14
|
|
|
13
15
|
## Features
|
|
14
16
|
|
|
@@ -16,17 +18,24 @@ aspects of chess games.
|
|
|
16
18
|
- [Can handle moves input via click or drag](https://shaack.com/projekte/cm-chessboard/examples/validate-moves.html)
|
|
17
19
|
- [Styleable via css and supports multiple piece sets](https://shaack.com/projekte/cm-chessboard/examples/different-styles.html)
|
|
18
20
|
- Uses SVG for rendering
|
|
19
|
-
- [Allows adding **extensions
|
|
21
|
+
- [Allows adding **extensions
|
|
22
|
+
** to extend the functionality](https://shaack.com/projekte/cm-chessboard/examples/extensions/arrows-extension.html)
|
|
20
23
|
|
|
21
24
|
## Extensions
|
|
22
25
|
|
|
23
|
-
The core of cm-chessboard is small, fast and reduced to the essentials. You can extend its functionality with
|
|
26
|
+
The core of cm-chessboard is small, fast and reduced to the essentials. You can extend its functionality with
|
|
27
|
+
extensions.
|
|
24
28
|
|
|
25
|
-
- [Accessibility Extension](https://shaack.com/projekte/cm-chessboard/examples/extensions/accessibility-extension.html) -
|
|
26
|
-
|
|
27
|
-
- [
|
|
28
|
-
|
|
29
|
-
- [
|
|
29
|
+
- [Accessibility Extension](https://shaack.com/projekte/cm-chessboard/examples/extensions/accessibility-extension.html) -
|
|
30
|
+
makes the chessboard more accessible
|
|
31
|
+
- [Arrows Extension](https://shaack.com/projekte/cm-chessboard/examples/extensions/arrows-extension.html) - renders
|
|
32
|
+
arrows on the chessboard
|
|
33
|
+
- [Markers Extension](https://shaack.com/projekte/cm-chessboard/examples/extensions/markers-extension.html) 🆕 - create
|
|
34
|
+
markers on specific squares
|
|
35
|
+
- [PromotionDialog Extension](https://shaack.com/projekte/cm-chessboard/examples/extensions/promotion-dialog-extension.html)
|
|
36
|
+
🆕 - shows a dialog to select the piece to promote to
|
|
37
|
+
- [RenderVideo Extension](https://shaack.com/projekte/cm-chessboard/examples/extensions/render-video-extension.html)
|
|
38
|
+
🆕 - renders a video from the pieces movement on the board
|
|
30
39
|
|
|
31
40
|
## Demo and repository
|
|
32
41
|
|
|
@@ -79,25 +88,25 @@ Below is the default configuration
|
|
|
79
88
|
|
|
80
89
|
```javascript
|
|
81
90
|
this.props = {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
91
|
+
position: FEN.empty, // set position as fen, use FEN.start or FEN.empty as shortcuts
|
|
92
|
+
orientation: COLOR.white, // white on bottom
|
|
93
|
+
responsive: true, // resize the board automatically to the size of the context element
|
|
94
|
+
language: navigator.language.substring(0, 2).toLowerCase(), // supports "de" and "en" for now, used for pieces naming
|
|
95
|
+
assetsUrl: "./assets/", // put all css and sprites in this folder, will be ignored for absolute urls of assets files
|
|
96
|
+
assetsCache: true, // cache sprites
|
|
97
|
+
style: {
|
|
98
|
+
cssClass: "default", // set the css theme of the board, try "green", "blue" or "chess-club"
|
|
99
|
+
showCoordinates: true, // show ranks and files
|
|
100
|
+
borderType: BORDER_TYPE.none, // "thin" thin border, "frame" wide border with coordinates in it, "none" no border
|
|
101
|
+
aspectRatio: 1, // height/width of the board
|
|
102
|
+
pieces: {
|
|
103
|
+
type: PIECES_FILE_TYPE.svgSprite, // pieces are in an SVG sprite, no other type supported for now
|
|
104
|
+
file: "pieces/standard.svg", // the filename of the sprite in `assetsUrl` or an absolute url like `https://…` or `/…`
|
|
105
|
+
tileSize: 40 // the tile size in the sprite
|
|
106
|
+
},
|
|
107
|
+
animationDuration: 300 // pieces animation duration in milliseconds. Disable all animations with `0`.
|
|
97
108
|
},
|
|
98
|
-
|
|
99
|
-
},
|
|
100
|
-
extensions: [ /* {class: ExtensionClass, props: { ... }} */] // add extensions here
|
|
109
|
+
extensions: [ /* {class: ExtensionClass, props: { ... }} */] // add extensions here
|
|
101
110
|
}
|
|
102
111
|
```
|
|
103
112
|
|
|
@@ -129,13 +138,13 @@ Move a piece from `squareFrom` to `squareTo`. Returns a **Promise**, which is re
|
|
|
129
138
|
|
|
130
139
|
### setPosition(fen, animated = false)
|
|
131
140
|
|
|
132
|
-
Sets the position as `fen`. Returns a **Promise**, which is resolved, after the animation finished.
|
|
141
|
+
Sets the position as `fen` or only the position part of a `fen`. Returns a **Promise**, which is resolved, after the animation finished.
|
|
133
142
|
|
|
134
143
|
[Example for **setPosition**](https://shaack.com/projekte/cm-chessboard/examples/pieces-animation.html)
|
|
135
144
|
|
|
136
145
|
### getPosition()
|
|
137
146
|
|
|
138
|
-
Returns the board position
|
|
147
|
+
Returns the board position in form of the position part of a `fen`.
|
|
139
148
|
|
|
140
149
|
### addMarker(type, square)
|
|
141
150
|
|
|
@@ -208,24 +217,27 @@ board.enableMoveInput((event) => {
|
|
|
208
217
|
|
|
209
218
|
The event has the following **`event.type`**:
|
|
210
219
|
|
|
211
|
-
- **`INPUT_EVENT_TYPE.moveInputStarted`**: User started the move input, `event.squareFrom` contains the coordinates.
|
|
212
|
-
|
|
213
|
-
- **`INPUT_EVENT_TYPE.
|
|
220
|
+
- **`INPUT_EVENT_TYPE.moveInputStarted`**: User started the move input, `event.squareFrom` contains the coordinates.
|
|
221
|
+
Return true or false to validate the start square.
|
|
222
|
+
- **`INPUT_EVENT_TYPE.validateMoveInput`**: User finished the move input, `event.squareFrom` and `event.squareTo`
|
|
223
|
+
contain the coordinates. Return true or false to validate the move input.
|
|
224
|
+
- **`INPUT_EVENT_TYPE.moveInputCanceled`**: User canceled the move with clicking again on the start square or clicking
|
|
225
|
+
outside the board.
|
|
214
226
|
|
|
215
227
|
```javascript
|
|
216
228
|
chessboard.enableMoveInput((event) => {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
+
switch (event.type) {
|
|
230
|
+
case INPUT_EVENT_TYPE.moveInputStarted:
|
|
231
|
+
console.log(`moveInputStarted: ${event.squareFrom}`)
|
|
232
|
+
// return `true`, if input is accepted/valid, `false` aborts the interaction, the piece will not move
|
|
233
|
+
return true
|
|
234
|
+
case INPUT_EVENT_TYPE.validateMoveInput:
|
|
235
|
+
console.log(`validateMoveInput: ${event.squareFrom}-${event.squareTo}`)
|
|
236
|
+
// return true, if input is accepted/valid, `false` takes the move back
|
|
237
|
+
return true
|
|
238
|
+
case INPUT_EVENT_TYPE.moveInputCanceled:
|
|
239
|
+
console.log(`moveInputCanceled`)
|
|
240
|
+
}
|
|
229
241
|
}, COLOR.white)
|
|
230
242
|
```
|
|
231
243
|
|
|
@@ -235,6 +247,8 @@ Disables moves via user input.
|
|
|
235
247
|
|
|
236
248
|
### enableSquareSelect(eventHandler)
|
|
237
249
|
|
|
250
|
+
> `enableSquareSelect` is deprecated and will be removed in future versions, because you can directly add events to the `chessboard.context` and then read the square from `event.target.getAttribute("data-square")`.
|
|
251
|
+
|
|
238
252
|
Enables primary and secondary pointer events on squares.
|
|
239
253
|
On desktop devices this means left and right click on squares.
|
|
240
254
|
|
|
@@ -249,7 +263,7 @@ board.enableSquareSelect((event) => {
|
|
|
249
263
|
})
|
|
250
264
|
```
|
|
251
265
|
|
|
252
|
-
[Example for
|
|
266
|
+
[Example for pointer events handling](https://shaack.com/projekte/cm-chessboard/examples/extensions/markers-extension.html)
|
|
253
267
|
|
|
254
268
|
**`event.square`** contains the coordinates of the user input.
|
|
255
269
|
|
|
@@ -290,7 +304,8 @@ Important is the id "markerCircle". You can set the marker
|
|
|
290
304
|
with `board.addMarker({class: "markerSquare", slice: "markerSquare"}, "e4")`
|
|
291
305
|
"emphasize" is the css class, which defines the color and opacity of the marker. "slice" is the id of the marker in the
|
|
292
306
|
SVG. This is
|
|
293
|
-
also demonstrated in
|
|
307
|
+
also demonstrated in
|
|
308
|
+
the [mark squares example](https://shaack.com/projekte/cm-chessboard/examples/extensions/markers-extension.html)
|
|
294
309
|
.
|
|
295
310
|
|
|
296
311
|
The color and stroke-width of the marker is defined in the css (or scss). You could also define your marker completely
|
|
@@ -353,13 +368,13 @@ Currently possible extension points are defined in `Extension.js`.
|
|
|
353
368
|
|
|
354
369
|
```js
|
|
355
370
|
export const EXTENSION_POINT = {
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
371
|
+
positionChanged: "positionChanged", // the positions of the pieces was changed
|
|
372
|
+
boardChanged: "boardChanged", // the board (orientation) was changed
|
|
373
|
+
moveInputToggled: "moveInputToggled", // move input was enabled or disabled
|
|
374
|
+
moveInput: "moveInput", // move started, moving over a square, validating or canceled
|
|
375
|
+
redrawBoard: "redrawBoard", // called after redrawing the board
|
|
376
|
+
animation: "animation", // called on animation start, end and on every animation frame
|
|
377
|
+
destroy: "destroy" // called, before the board is destroyed
|
|
363
378
|
}
|
|
364
379
|
```
|
|
365
380
|
|
|
@@ -455,8 +470,10 @@ To get all arrows, call `chessboard.getArrows()` without parameters, as with `re
|
|
|
455
470
|
|
|
456
471
|
- Works with **Vue** out of the box
|
|
457
472
|
- Works with **Svelte** out of the box
|
|
458
|
-
- I don't use **React**, but there exists a ticket from someone who is using cm-chessboard with
|
|
459
|
-
|
|
473
|
+
- I don't use **React**, but there exists a ticket from someone who is using cm-chessboard with
|
|
474
|
+
react: https://github.com/shaack/cm-chessboard/issues/20
|
|
475
|
+
- It should work also with **all other JS frameworks**, because cm-chessboard is written in standard ES6 and has **no
|
|
476
|
+
dependencies**.
|
|
460
477
|
|
|
461
478
|
## Licenses
|
|
462
479
|
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
<div style="clear: both"></div>
|
|
18
18
|
<button onclick="window.removeDots()">Remove dots</button>
|
|
19
19
|
<script type="module">
|
|
20
|
-
import {Chessboard
|
|
20
|
+
import {Chessboard} from "../../src/Chessboard.js"
|
|
21
21
|
import {FEN} from "../../src/model/Position.js"
|
|
22
22
|
import {MARKER_TYPE, Markers} from "../../src/extensions/markers/Markers.js"
|
|
23
23
|
|
|
@@ -26,21 +26,23 @@
|
|
|
26
26
|
assetsUrl: "../../assets/",
|
|
27
27
|
extensions: [{class: Markers}]
|
|
28
28
|
})
|
|
29
|
-
board1.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
29
|
+
board1.context.addEventListener("contextmenu", (event) => {
|
|
30
|
+
event.preventDefault()
|
|
31
|
+
})
|
|
32
|
+
board1.context.addEventListener("mousedown", (event) => {
|
|
33
|
+
console.log("mousedown board1", event)
|
|
34
|
+
let markerType
|
|
35
|
+
if (event.button === 0) {
|
|
36
|
+
markerType = MARKER_TYPE.dot
|
|
37
|
+
} else {
|
|
38
|
+
markerType = MARKER_TYPE.circlePrimary
|
|
39
|
+
}
|
|
40
|
+
const square = event.target.getAttribute("data-square")
|
|
41
|
+
const markersOnSquare = board1.getMarkers(markerType, square)
|
|
42
|
+
if (markersOnSquare.length > 0) {
|
|
43
|
+
board1.removeMarkers(markerType, square)
|
|
44
|
+
} else {
|
|
45
|
+
board1.addMarker(markerType, square)
|
|
44
46
|
}
|
|
45
47
|
})
|
|
46
48
|
window.removeDots = () => {
|
|
@@ -58,13 +60,17 @@
|
|
|
58
60
|
})
|
|
59
61
|
// define your own marker
|
|
60
62
|
const myOwnMarker = {class: "marker-circle-danger", slice: "markerCircle"}
|
|
61
|
-
board2.
|
|
62
|
-
|
|
63
|
-
|
|
63
|
+
board2.context.addEventListener("contextmenu", (event) => {
|
|
64
|
+
event.preventDefault()
|
|
65
|
+
})
|
|
66
|
+
board2.context.addEventListener("mousedown", (event) => {
|
|
67
|
+
if (event.button === 2) {
|
|
68
|
+
const square = event.target.getAttribute("data-square")
|
|
69
|
+
const markersOnSquare = board2.getMarkers(myOwnMarker, square)
|
|
64
70
|
if (markersOnSquare.length > 0) {
|
|
65
|
-
board2.removeMarkers(myOwnMarker,
|
|
71
|
+
board2.removeMarkers(myOwnMarker, square)
|
|
66
72
|
} else {
|
|
67
|
-
board2.addMarker(myOwnMarker,
|
|
73
|
+
board2.addMarker(myOwnMarker, square)
|
|
68
74
|
}
|
|
69
75
|
}
|
|
70
76
|
})
|
package/index.html
CHANGED
|
@@ -48,8 +48,11 @@ It works, it is cool and it is easy to use. 👍</p>
|
|
|
48
48
|
const interval = setInterval(() => {
|
|
49
49
|
makeRandomMove()
|
|
50
50
|
board.setPosition(chess.fen(), true)
|
|
51
|
-
},
|
|
51
|
+
}, 500)
|
|
52
52
|
function makeRandomMove() {
|
|
53
|
+
if(chess.game_over()) {
|
|
54
|
+
chess.reset()
|
|
55
|
+
}
|
|
53
56
|
const possibleMoves = chess.moves()
|
|
54
57
|
if (possibleMoves.length === 0) {
|
|
55
58
|
clearInterval(interval)
|
package/package.json
CHANGED
package/src/Chessboard.js
CHANGED
|
@@ -22,6 +22,7 @@ export const INPUT_EVENT_TYPE = {
|
|
|
22
22
|
moveInputCanceled: "moveInputCanceled",
|
|
23
23
|
moveInputFinished: "moveInputFinished"
|
|
24
24
|
}
|
|
25
|
+
/** @deprecated */
|
|
25
26
|
export const SQUARE_SELECT_TYPE = {
|
|
26
27
|
primary: "primary",
|
|
27
28
|
secondary: "secondary"
|
|
@@ -151,7 +152,13 @@ export class Chessboard {
|
|
|
151
152
|
this.view.disableMoveInput()
|
|
152
153
|
}
|
|
153
154
|
|
|
155
|
+
/**
|
|
156
|
+
* This will be removed in the future, because you can directly assign events
|
|
157
|
+
* to the `chessboard.context` and then read the square from the `event.target`
|
|
158
|
+
* @deprecated
|
|
159
|
+
*/
|
|
154
160
|
enableSquareSelect(eventHandler) {
|
|
161
|
+
console.warn("chessboard.enableSquareSelect() is deprecated will be removed in future versions");
|
|
155
162
|
if (this.squareSelectListener) {
|
|
156
163
|
console.warn("squareSelectListener already existing")
|
|
157
164
|
return
|
|
@@ -179,6 +186,9 @@ export class Chessboard {
|
|
|
179
186
|
this.view.visualizeInputState()
|
|
180
187
|
}
|
|
181
188
|
|
|
189
|
+
/**
|
|
190
|
+
* @deprecated
|
|
191
|
+
*/
|
|
182
192
|
disableSquareSelect() {
|
|
183
193
|
this.context.removeEventListener("contextmenu", this.squareSelectListener)
|
|
184
194
|
this.context.removeEventListener("mousedown", this.squareSelectListener)
|
|
@@ -71,7 +71,6 @@ export class Arrows extends Extension {
|
|
|
71
71
|
href: `${spriteUrl}#${arrow.type.slice}`,
|
|
72
72
|
})
|
|
73
73
|
|
|
74
|
-
|
|
75
74
|
const x1 = sqfrom.x.baseVal.value + (sqfrom.width.baseVal.value / 2)
|
|
76
75
|
const x2 = sqto.x.baseVal.value + (sqto.width.baseVal.value / 2)
|
|
77
76
|
const y1 = sqfrom.y.baseVal.value + (sqfrom.height.baseVal.value / 2)
|
|
@@ -86,7 +85,6 @@ export class Arrows extends Extension {
|
|
|
86
85
|
lineFill.setAttribute('class', 'arrow-line')
|
|
87
86
|
lineFill.setAttribute("marker-end", "url(#" + id + ")")
|
|
88
87
|
lineFill.setAttribute('stroke-width', width + "px")
|
|
89
|
-
|
|
90
88
|
}
|
|
91
89
|
|
|
92
90
|
addArrow(type, from, to) {
|
package/src/lib/Svg.js
CHANGED
|
@@ -24,7 +24,7 @@ export class Svg {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
* Add an Element to
|
|
27
|
+
* Add an Element to an SVG DOM
|
|
28
28
|
* @param parent
|
|
29
29
|
* @param name
|
|
30
30
|
* @param attributes
|
|
@@ -50,10 +50,14 @@ export class Svg {
|
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
/**
|
|
53
|
-
* Remove an
|
|
53
|
+
* Remove an element from an SVG DOM
|
|
54
54
|
* @param element
|
|
55
55
|
*/
|
|
56
56
|
static removeElement(element) {
|
|
57
|
+
if(!element) {
|
|
58
|
+
console.warn("removeElement, element is", element)
|
|
59
|
+
return
|
|
60
|
+
}
|
|
57
61
|
if (element.parentNode) {
|
|
58
62
|
element.parentNode.removeChild(element)
|
|
59
63
|
} else {
|