cm-chessboard 8.0.2 → 8.0.3
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/examples/validate-moves.html +6 -1
- package/package.json +1 -1
- package/src/Chessboard.js +0 -1
- package/src/view/ChessboardView.js +10 -10
- package/test/TestBoard.js +1 -1
|
@@ -25,10 +25,15 @@
|
|
|
25
25
|
|
|
26
26
|
const chess = new Chess()
|
|
27
27
|
|
|
28
|
+
let seed = 71;
|
|
29
|
+
function random() {
|
|
30
|
+
const x = Math.sin(seed++) * 10000;
|
|
31
|
+
return x - Math.floor(x);
|
|
32
|
+
}
|
|
28
33
|
function makeEngineMove(chessboard) {
|
|
29
34
|
const possibleMoves = chess.moves({verbose: true})
|
|
30
35
|
if (possibleMoves.length > 0) {
|
|
31
|
-
const randomIndex = Math.floor(
|
|
36
|
+
const randomIndex = Math.floor(random() * possibleMoves.length)
|
|
32
37
|
const randomMove = possibleMoves[randomIndex]
|
|
33
38
|
setTimeout(() => { // smoother with 500ms delay
|
|
34
39
|
chess.move({from: randomMove.from, to: randomMove.to})
|
package/package.json
CHANGED
package/src/Chessboard.js
CHANGED
|
@@ -18,8 +18,8 @@ export class ChessboardView {
|
|
|
18
18
|
if (chessboard.props.assetsCache) {
|
|
19
19
|
this.cacheSpriteToDiv("cm-chessboard-sprite", this.getSpriteUrl())
|
|
20
20
|
}
|
|
21
|
-
this.
|
|
22
|
-
this.chessboard.context.appendChild(this.
|
|
21
|
+
this.container = document.createElement("div")
|
|
22
|
+
this.chessboard.context.appendChild(this.container)
|
|
23
23
|
if (chessboard.props.responsive) {
|
|
24
24
|
if (typeof ResizeObserver !== "undefined") {
|
|
25
25
|
this.resizeObserver = new ResizeObserver(() => {
|
|
@@ -34,8 +34,8 @@ export class ChessboardView {
|
|
|
34
34
|
this.positionsAnimationTask = Promise.resolve()
|
|
35
35
|
this.pointerDownListener = this.pointerDownHandler.bind(this)
|
|
36
36
|
this.pointerDownListener = this.pointerDownHandler.bind(this)
|
|
37
|
-
this.
|
|
38
|
-
this.
|
|
37
|
+
this.container.addEventListener("mousedown", this.pointerDownListener)
|
|
38
|
+
this.container.addEventListener("touchstart", this.pointerDownListener, {passive: false})
|
|
39
39
|
|
|
40
40
|
this.createSvgAndGroups()
|
|
41
41
|
this.updateMetrics()
|
|
@@ -80,7 +80,7 @@ export class ChessboardView {
|
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
createSvgAndGroups() {
|
|
83
|
-
this.svg = Svg.createSvg(this.
|
|
83
|
+
this.svg = Svg.createSvg(this.container)
|
|
84
84
|
// let description = document.createElement("description")
|
|
85
85
|
// description.innerText = "Chessboard"
|
|
86
86
|
// description.id = "svg-description"
|
|
@@ -101,8 +101,8 @@ export class ChessboardView {
|
|
|
101
101
|
|
|
102
102
|
updateMetrics() {
|
|
103
103
|
const piecesTileSize = this.chessboard.props.style.pieces.tileSize
|
|
104
|
-
this.width = this.
|
|
105
|
-
this.height = this.
|
|
104
|
+
this.width = this.container.clientWidth
|
|
105
|
+
this.height = this.container.clientWidth * (this.chessboard.props.style.aspectRatio || 1)
|
|
106
106
|
if (this.chessboard.props.style.borderType === BORDER_TYPE.frame) {
|
|
107
107
|
this.borderSize = this.width / 25
|
|
108
108
|
} else if (this.chessboard.props.style.borderType === BORDER_TYPE.thin) {
|
|
@@ -120,9 +120,9 @@ export class ChessboardView {
|
|
|
120
120
|
}
|
|
121
121
|
|
|
122
122
|
handleResize() {
|
|
123
|
-
this.
|
|
124
|
-
this.
|
|
125
|
-
if (this.
|
|
123
|
+
this.container.style.width = this.chessboard.context.clientWidth + "px"
|
|
124
|
+
this.container.style.height = (this.chessboard.context.clientWidth * this.chessboard.props.style.aspectRatio) + "px"
|
|
125
|
+
if (this.container.clientWidth !== this.width || this.container.clientHeight !== this.height) {
|
|
126
126
|
this.updateMetrics()
|
|
127
127
|
this.redrawBoard()
|
|
128
128
|
this.redrawPieces()
|
package/test/TestBoard.js
CHANGED
|
@@ -24,7 +24,7 @@ describe("TestBoard", () => {
|
|
|
24
24
|
assetsUrl: "../assets/",
|
|
25
25
|
position: FEN.start
|
|
26
26
|
})
|
|
27
|
-
assert.equal(chessboard.view.
|
|
27
|
+
assert.equal(chessboard.view.container.childNodes.length, 1)
|
|
28
28
|
chessboard.destroy()
|
|
29
29
|
assert.equal(chessboard.state, undefined)
|
|
30
30
|
})
|