cm-chessboard 3.15.0 → 3.16.2
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 +10 -4
- package/examples/enable-input.html +1 -0
- package/examples/pieces-animation.html +28 -3
- package/examples/validate-moves.html +1 -0
- package/package.json +1 -1
- package/src/cm-chessboard/Chessboard.js +21 -0
- package/src/cm-chessboard/ChessboardMoveInput.js +6 -5
- package/src/cm-chessboard/ChessboardState.js +2 -2
- package/src/cm-chessboard/ChessboardView.js +3 -2
package/README.md
CHANGED
|
@@ -44,7 +44,7 @@ dependencies.
|
|
|
44
44
|
|
|
45
45
|
Preconditions for using cm-chessboard in a web page:
|
|
46
46
|
|
|
47
|
-
1. **include the css:** `styles/cm-chessboard.css`
|
|
47
|
+
1. **include the css:** `assets/styles/cm-chessboard.css`
|
|
48
48
|
2. **import the ES6 module:** `import {Chessboard} from "PATH/TO/src/cm-chessboard/Chessboard.js"`
|
|
49
49
|
|
|
50
50
|
Example, showing a FEN:
|
|
@@ -106,12 +106,18 @@ Sets a piece on a square. Example: `board.setPiece("e4", PIECE.blackKnight)` or
|
|
|
106
106
|
|
|
107
107
|
Returns the piece on a square or `undefined` if the square is empty.
|
|
108
108
|
|
|
109
|
+
### movePiece(squareFrom, squareTo, animated = true)
|
|
110
|
+
|
|
111
|
+
Move a piece from `squareFrom` to `squareTo`. Returns a **Promise**, which is resolved, when the animation finished.
|
|
112
|
+
|
|
113
|
+
[Example for **movePiece**](https://shaack.com/projekte/cm-chessboard/examples/pieces-animation.html)
|
|
114
|
+
|
|
109
115
|
### setPosition(fen, animated = true)
|
|
110
116
|
|
|
111
117
|
Sets the position as `fen`. Special values are `"start"`, sets the chess start position and
|
|
112
118
|
`"empty"`, sets an empty board. When `animated` is set `false`, the new position will be shown instant.
|
|
113
119
|
|
|
114
|
-
Returns a **Promise
|
|
120
|
+
Returns a **Promise**, which is resolved, when the animation finished.
|
|
115
121
|
|
|
116
122
|
[Example for **setPosition**](https://shaack.com/projekte/cm-chessboard/examples/pieces-animation.html)
|
|
117
123
|
|
|
@@ -262,7 +268,7 @@ Example: The markerCircle is defined in the SVG like this.
|
|
|
262
268
|
It's a circle with the radius 18 and its center at 20/20.
|
|
263
269
|
|
|
264
270
|
Important is the id "markerCircle". You can set the marker
|
|
265
|
-
with `board.addMarker("e4", {class: "
|
|
271
|
+
with `board.addMarker("e4", {class: "markerSquare", slice: "markerSquare"})`
|
|
266
272
|
"emphasize" is the css class, which defines the color and opacity of the marker. "slice" is the id of the marker in the SVG. This is
|
|
267
273
|
also demonstrated in the [mark squares example](https://shaack.com/projekte/cm-chessboard/examples/input-callbacks.html)
|
|
268
274
|
.
|
|
@@ -270,7 +276,7 @@ also demonstrated in the [mark squares example](https://shaack.com/projekte/cm-c
|
|
|
270
276
|
The color and stroke-width of the marker is defined in the css (or scss). You could also define your marker completely
|
|
271
277
|
in the sprite, but then that is not so flexible.
|
|
272
278
|
|
|
273
|
-
These are the css styles of the markers "
|
|
279
|
+
These are the css styles of the markers "markerSquare" and "markerCircleRed".
|
|
274
280
|
|
|
275
281
|
```css
|
|
276
282
|
marker.markerSquare {
|
|
@@ -18,11 +18,23 @@
|
|
|
18
18
|
<button onclick="setPosition('rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR')">Position 1</button>
|
|
19
19
|
<button onclick="setPosition('rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R')">Position 2</button>
|
|
20
20
|
<button onclick="setPosition('rn2k1r1/ppp1pp1p/3p2p1/5bn1/P7/2N2B2/1PPPPP2/2BNK1RR')">Position 3</button>
|
|
21
|
-
<button onclick="switchOrientation()">Switch Orientation
|
|
22
|
-
</button>
|
|
21
|
+
<button onclick="switchOrientation()">Switch Orientation</button>
|
|
23
22
|
<pre><button onclick="window.board.setPosition('rn2k1r1/ppp1pp1p/3p2p1/5bn1/P7/2N2B2/1PPPPP2/2BNK1RR')">Position 3</button></pre>
|
|
23
|
+
<br/>
|
|
24
|
+
Move piece from: <input id="moveFrom" type="text" size="2" value="e2"/> to: <input id="moveTo" type="text" size="2"
|
|
25
|
+
value="e4"/>
|
|
26
|
+
<button onclick="movePiece()">Do move</button>
|
|
27
|
+
<pre>board.movePiece(squareFrom, squareTo)</pre>
|
|
28
|
+
<br/>
|
|
29
|
+
<button onclick="knightMove()">Knight dance</button>
|
|
30
|
+
<pre>
|
|
31
|
+
board.setPiece("e4", PIECE.wn)
|
|
32
|
+
await board.movePiece("e4", "c3")
|
|
33
|
+
await board.movePiece("c3", "d5")
|
|
34
|
+
await board.movePiece("d5", "f4")
|
|
35
|
+
</pre>
|
|
24
36
|
<script type="module">
|
|
25
|
-
import {Chessboard} from "../src/cm-chessboard/Chessboard.js"
|
|
37
|
+
import {Chessboard, PIECE} from "../src/cm-chessboard/Chessboard.js"
|
|
26
38
|
|
|
27
39
|
const board = new Chessboard(document.getElementById("board"),
|
|
28
40
|
{
|
|
@@ -40,6 +52,19 @@
|
|
|
40
52
|
window.switchOrientation = () => {
|
|
41
53
|
board.setOrientation(board.getOrientation() === 'w' ? 'b' : 'w')
|
|
42
54
|
}
|
|
55
|
+
window.movePiece = () => {
|
|
56
|
+
const squareFrom = document.getElementById("moveFrom").value
|
|
57
|
+
const squareTo = document.getElementById("moveTo").value
|
|
58
|
+
console.log("movePiece", squareFrom, squareTo)
|
|
59
|
+
board.movePiece(squareFrom, squareTo)
|
|
60
|
+
}
|
|
61
|
+
window.knightMove = async () => {
|
|
62
|
+
await board.setPosition("empty")
|
|
63
|
+
board.setPiece("e4", PIECE.wn)
|
|
64
|
+
await board.movePiece("e4", "c3")
|
|
65
|
+
await board.movePiece("c3", "d5")
|
|
66
|
+
await board.movePiece("d5", "f4")
|
|
67
|
+
}
|
|
43
68
|
</script>
|
|
44
69
|
</body>
|
|
45
70
|
</html>
|
|
@@ -22,6 +22,7 @@ const chess = new Chess()
|
|
|
22
22
|
function inputHandler(event) {
|
|
23
23
|
console.log("event", event)
|
|
24
24
|
event.chessboard.removeMarkers(undefined, MARKER_TYPE.dot)
|
|
25
|
+
event.chessboard.removeMarkers(undefined, MARKER_TYPE.square)
|
|
25
26
|
if (event.type === INPUT_EVENT_TYPE.moveStart) {
|
|
26
27
|
const moves = chess.moves({square: event.square, verbose: true});
|
|
27
28
|
event.chessboard.addMarker(event.square, MARKER_TYPE.square)
|
package/package.json
CHANGED
|
@@ -114,6 +114,27 @@ export class Chessboard {
|
|
|
114
114
|
return this.state.squares[this.state.squareToIndex(square)]
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
+
movePiece(squareFrom, squareTo, animated = true) {
|
|
118
|
+
return new Promise((resolve, reject) => {
|
|
119
|
+
const prevSquares = this.state.squares.slice(0) // clone
|
|
120
|
+
const pieceFrom = this.getPiece(squareFrom)
|
|
121
|
+
if(!pieceFrom) {
|
|
122
|
+
reject("no piece on square " + squareFrom)
|
|
123
|
+
return
|
|
124
|
+
}
|
|
125
|
+
this.state.squares[this.state.squareToIndex(squareFrom)] = null
|
|
126
|
+
this.state.squares[this.state.squareToIndex(squareTo)] = pieceFrom
|
|
127
|
+
if (animated) {
|
|
128
|
+
this.view.animatePieces(prevSquares, this.state.squares, () => {
|
|
129
|
+
resolve()
|
|
130
|
+
})
|
|
131
|
+
} else {
|
|
132
|
+
this.view.drawPieces(this.state.squares)
|
|
133
|
+
resolve()
|
|
134
|
+
}
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
|
|
117
138
|
setPosition(fen, animated = true) {
|
|
118
139
|
return new Promise((resolve) => {
|
|
119
140
|
if (fen === "start") {
|
|
@@ -21,7 +21,7 @@ export const MOVE_CANCELED_REASON = {
|
|
|
21
21
|
secondClick: "secondClick",
|
|
22
22
|
movedOutOfBoard: "movedOutOfBoard",
|
|
23
23
|
draggedBack: "draggedBack",
|
|
24
|
-
|
|
24
|
+
clickedAnotherPiece: "clickedAnotherPiece"
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
const DRAG_THRESHOLD = 4
|
|
@@ -254,7 +254,7 @@ export class ChessboardMoveInput {
|
|
|
254
254
|
const startPieceName = this.chessboard.getPiece(SQUARE_COORDINATES[this.startIndex])
|
|
255
255
|
const startPieceColor = startPieceName ? startPieceName.substr(0, 1) : undefined
|
|
256
256
|
if (color && startPieceColor === pieceColor) { // https://github.com/shaack/cm-chessboard/issues/40
|
|
257
|
-
this.moveCanceledCallback(MOVE_CANCELED_REASON.
|
|
257
|
+
this.moveCanceledCallback(MOVE_CANCELED_REASON.clickedAnotherPiece, this.startIndex, index)
|
|
258
258
|
if (this.moveStartCallback(index)) {
|
|
259
259
|
this.setMoveInputState(STATE.pieceClickedThreshold, {
|
|
260
260
|
index: index,
|
|
@@ -339,7 +339,7 @@ export class ChessboardMoveInput {
|
|
|
339
339
|
if (this.moveInputState === STATE.clickDragTo) {
|
|
340
340
|
this.chessboard.state.setPiece(this.startIndex, this.movedPiece)
|
|
341
341
|
this.view.setPieceVisibility(this.startIndex)
|
|
342
|
-
this.moveCanceledCallback(MOVE_CANCELED_REASON.draggedBack, index)
|
|
342
|
+
this.moveCanceledCallback(MOVE_CANCELED_REASON.draggedBack, index, index)
|
|
343
343
|
this.setMoveInputState(STATE.reset)
|
|
344
344
|
} else {
|
|
345
345
|
this.setMoveInputState(STATE.clickTo, {index: index})
|
|
@@ -351,12 +351,13 @@ export class ChessboardMoveInput {
|
|
|
351
351
|
this.setMoveInputState(STATE.clickTo, {index: index})
|
|
352
352
|
} else if (this.moveInputState === STATE.secondClickThreshold) {
|
|
353
353
|
this.setMoveInputState(STATE.reset)
|
|
354
|
-
this.moveCanceledCallback(MOVE_CANCELED_REASON.secondClick, index)
|
|
354
|
+
this.moveCanceledCallback(MOVE_CANCELED_REASON.secondClick, index, index)
|
|
355
355
|
}
|
|
356
356
|
} else {
|
|
357
357
|
this.view.drawPieces()
|
|
358
|
+
const moveStartIndex = this.startIndex
|
|
358
359
|
this.setMoveInputState(STATE.reset)
|
|
359
|
-
this.moveCanceledCallback(MOVE_CANCELED_REASON.movedOutOfBoard, undefined)
|
|
360
|
+
this.moveCanceledCallback(MOVE_CANCELED_REASON.movedOutOfBoard, moveStartIndex, undefined)
|
|
360
361
|
}
|
|
361
362
|
} else {
|
|
362
363
|
this.view.drawPieces()
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
export class ChessboardState {
|
|
8
8
|
|
|
9
9
|
constructor() {
|
|
10
|
-
this.squares = new Array(64).fill(
|
|
10
|
+
this.squares = new Array(64).fill(null)
|
|
11
11
|
this.orientation = undefined
|
|
12
12
|
this.markers = []
|
|
13
13
|
this.inputWhiteEnabled = false
|
|
@@ -59,7 +59,7 @@ export class ChessboardState {
|
|
|
59
59
|
})
|
|
60
60
|
for (let c = 0; c < 8; c++) {
|
|
61
61
|
const char = row.substr(c, 1)
|
|
62
|
-
let piece =
|
|
62
|
+
let piece = null
|
|
63
63
|
if (char !== '-') {
|
|
64
64
|
if (char.toUpperCase() === char) {
|
|
65
65
|
piece = `w${char.toLowerCase()}`
|
|
@@ -406,13 +406,14 @@ export class ChessboardView {
|
|
|
406
406
|
}
|
|
407
407
|
}
|
|
408
408
|
|
|
409
|
-
moveCanceledCallback(reason,
|
|
409
|
+
moveCanceledCallback(reason, fromIndex, toIndex) {
|
|
410
410
|
if (this.moveInputCallback) {
|
|
411
411
|
this.moveInputCallback({
|
|
412
412
|
chessboard: this.chessboard,
|
|
413
413
|
type: INPUT_EVENT_TYPE.moveCanceled,
|
|
414
414
|
reason: reason,
|
|
415
|
-
|
|
415
|
+
squareFrom: SQUARE_COORDINATES[fromIndex],
|
|
416
|
+
squareTo: toIndex ? SQUARE_COORDINATES[toIndex] : undefined
|
|
416
417
|
})
|
|
417
418
|
}
|
|
418
419
|
}
|