A JavaScript chessboard which is lightweight, ES6 module based, responsive, SVG rendered and without dependencies.
npm install cm-chessboardA JavaScript chessboard which is lightweight, ES6 module based, responsive, SVG rendered and without dependencies.
cm-chessboard is the main chessboard of
chessmail.eu and chessmail.de. It is also used
in chess-console and in
cm-fen-editor. They are all nice written ES6 Modules to handle different aspects of chess games.
- No dependencies, just clean ES6
- Can handle moves input via click or drag
- Styleable via css and supports multiple piece sets
- Uses SVG for rendering
- Allows adding extensions to extend the
functionality
- Supports chess960 (Freestyle) castling input
The core of cm-chessboard is small, fast and reduced to the essentials. You can extend its functionality with extensions.
- RightClickAnnotator ⇨ Uses Markers Extension and Arrows Extension. Adds the handling of mouse events to draw them on the board.
- Markers Extension ⇨ create markers on specific squares
- Arrows Extension ⇨ renders arrows on the chessboard
- Accessibility Extension ⇨ makes the chessboard more accessible
- PromotionDialog Extension ⇨ shows a dialog to select the piece to promote to
- Demo: http://shaack.com/projekte/cm-chessboard/
- Repository: https://github.com/shaack/cm-chessboard
- Option 1: Install the npm package with npm install cm-chessboard.
- Option 2: Download the code from GitHub.
- Option 3: Use it via CDN https://cdn.jsdelivr.net/npm/cm-chessboard@8/src/Chessboard.js
#### Step 2a: Include the CSS file
``html`
- Some extensions, like "Markers", "Promotion Dialog" or "Arrows" need additional CSS. See the examples.
#### Step 2b: Create a container for the chessboard
`html`
#### Step 2c: Create the chessboard in your JavaScript code.
`html
`
You need to configure the assetsUrl in your chessboard props (the second parameter). The assetsUrl must be the path to the assets folder of this project, where the pieces SVGs and other resources are located.
You can also copy the assets folder from cm-chessboard/assets to your project and modify the content.
#### See also
- Simple cm-chessboard example online
To enable the user to move the pieces, you have to enable the move input.
`javascript
const board = new Chessboard(document.getElementById("board"), {
position: FEN.start,
assetsUrl: "../assets/",
extensions: [{class: Markers}] // Looks better with markers. (Don't forget to also include the CSS for the markers)
})
board.enableMoveInput(inputHandler) // This enables the move input
function inputHandler(event) {
console.log(event)
if(event.type === INPUT_EVENT_TYPE.moveInputStarted ||
event.type === INPUT_EVENT_TYPE.validateMoveInput) {
return true // false cancels move
}
}
`
#### See also
- Simple example with move input enabled
- More complex example with move validation
Take a look at the /examples folder for more examples.
Below is the default configuration
`javascriptassets/pieces/
this.props = {
position: FEN.empty, // set position as fen, use FEN.start or FEN.empty as shortcuts
orientation: COLOR.white, // white on bottom
responsive: true, // resize the board automatically to the size of the context element
assetsUrl: "./assets/", // put all css and sprites in this folder, will be ignored for absolute urls of assets files
assetsCache: true, // cache the sprites, deactivate if you want to use multiple pieces sets in one page
style: {
cssClass: "default", // set the css theme of the board, try "green", "blue" or "chess-club"
showCoordinates: true, // show ranks and files
borderType: BORDER_TYPE.none, // "thin" thin border, "frame" wide border with coordinates in it, "none" no border
aspectRatio: 1, // height/width of the board
pieces: {
type: PIECES_FILE_TYPE.svgSprite, // pieces are in an SVG sprite, no other type supported for now
file: "pieces/standard.svg", // the filename of the sprite in or an absolute url like https://… or /…0
tileSize: 40 // the tile size in the sprite
},
animationDuration: 300 // pieces animation duration in milliseconds. Disable all animations with `
},
extensions: [ / {class: ExtensionClass, props: { ... }} /] // add extensions here
}
new Chessboard(context, props = {})
- context: the HTML DOM element being the container of the widget
- props: The board configuration (properties)
Sets a piece on a square. Example: board.setPiece("e4", PIECE.blackKnight, true) orboard.setPiece("e4", "bn"). Remove a Piece with board.setPiece("e4", null). Returns a Promise, which is
resolved,
after the animation finished.
Returns the piece on a square or null if the square is empty.
Move a piece from squareFrom to squareTo. Returns a Promise, which is resolved, after the animation finished.
Sets the position as fen or only the position part of a fen. Returns a Promise, which is resolved, after the animation finished.
Returns the board position in form of the position part of a fen.
Sets the board orientation (color at bottom). Allowed values are COLOR.white or COLOR.black.
Returns the board orientation.
Removes the board from the DOM.
Enables moves via user input (mouse or touch). Set optional color, if you want to enable the move input for a specificCOLOR.white
side, or COLOR.black.
eventHandler is called on specific events of the user interaction. Receives the parameter event.
`javascript`
board.enableMoveInput((event) => {
// handle user input here
}, COLOR.white)
The event has the following event.type:
- INPUT_EVENT_TYPE.moveInputStarted: User started the move input, event.squareFrom contains the coordinates.true
Return or false to validate the start square. false cancels the move.INPUT_EVENT_TYPE.validateMoveInput
- : To validate the users move input. event.squareFrom and event.squareTotrue
contain the coordinates. Return or false to validate the move. false cancels the move.INPUT_EVENT_TYPE.moveInputCanceled
- : The user canceled the move with clicking again on the start square, clickingINPUT_EVENT_TYPE.moveInputFinished
outside the board or right click.
- : Fired after the move was made, also when canceled.INPUT_EVENT_TYPE.movingOverSquare
- : Fired, when the user moves the piece over a square. event.squareTo contains
the coordinates.
`javascriptmoveInputStarted: ${event.squareFrom}
chessboard.enableMoveInput((event) => {
console.log("move input", event)
switch (event.type) {
case INPUT_EVENT_TYPE.moveInputStarted:
console.log()validateMoveInput: ${event.squareFrom}-${event.squareTo}
return true // false cancels move
case INPUT_EVENT_TYPE.validateMoveInput:
console.log()moveInputCanceled
return true // false cancels move
case INPUT_EVENT_TYPE.moveInputCanceled:
console.log()moveInputFinished
break
case INPUT_EVENT_TYPE.moveInputFinished:
console.log()movingOverSquare: ${event.squareTo}
break
case INPUT_EVENT_TYPE.movingOverSquare:
console.log()`
break
}
}, COLOR.white)
Disables moves via user input.
cm-chessboard supports alternative piece sets. A piece set is defined in an SVG sprite. cm-chessboard is shipped with
two sets, the default staunty (
chessboard-sprite-staunty.svg) and a sprite of the
Wikimedia standard pieces
(chessboard-sprite.svg).
Sprites must be 40x40px in size where the piece elements must have ids like
"bp" (black pawn) or "wq" (white queen). Just open the sprite in a text editor, SVG is readable like HTML.
cm-chessboard provides the ability to extend its functionality with extensions. Extensions extend the class Extension
and have access to the chessboard and can register extension points.
`js`
class MyCoolChessboardExtension extends Extension {
constructor(chessboard, props) {
super(chessboard, props)
this.registerExtensionPoint(EXTENSION_POINT.moveInput, (data) => {
// do something on move [start | cancel | done]
console.log(data)
})
}
}
Currently possible extension points are defined in Extension.js.
`js`
export const EXTENSION_POINT = {
positionChanged: "positionChanged", // the positions of the pieces was changed
boardChanged: "boardChanged", // the board (orientation) was changed
boardResized: "boardResized", // the board was resized
moveInputToggled: "moveInputToggled", // move input was enabled or disabled
moveInput: "moveInput", // move started, moving over a square, validating or canceled
beforeRedrawBoard: "beforeRedrawBoard", // called before redrawing the board
afterRedrawBoard: "afterRedrawBoard", // called after redrawing the board
animation: "animation", // called on animation start, end and on every animation frame
destroy: "destroy" // called, before the board is destroyed
}
Enable extensions via the chessboard props.
`js`
const chessboard = new Chessboard(document.getElementById("board"), {
position: FEN.start,
extensions: // list of used extensions
[{
class: MyCoolChessboardExtension, // the class of the extension
props: {
// configure the extension here
}
}]
})
Add methods to the chessboard in the constructor of your extension like shown below.
`js`
chessboard.addMarker = this.addMarker.bind(this)
Creates markers on the board. Example: Markers extension
See the README of the Markers. extension.
Draw arrows on the board. Example: Arrows extension
#### Methods
##### addArrow(type, fromSquare, toSquare)
Add an arrow.
##### removeArrows(type, from, to)
To remove all arrows, call chessboard.removeArrows() without parameters. To remove all arrows of a specificchessboard.removeArrows(ARROW_TYPE.danger)
type (type "danger"), call . To remove all arrows starting at "chessboard.removeArrows(undefined, "e2")
e2"
you can call and so on...
##### getArrows(type, from, to)
To get all arrows, call chessboard.getArrows() without parameters, as with removeArrows(type, from, to).
This extension ensures that visual impaired people can better use the chessboard. It displays the braille notation
of the current position in the alt tag of the board image and enables a form to move the pieces via text input. It
can also display the board as HTML table and the pieces as list.
See the example Accessibility extension
#### Usage
`js
const chessboard = new Chessboard(document.getElementById("board"), {
position: FEN.start,
sprite: {url: "../assets/images/chessboard-sprite.svg"},
// animationDuration: 0, // optional, set to 0 to disable animations
style: {
cssClass: "default-contrast" // make the coordinates better visible with the "default-contrast" theme
},
extensions:
[{
class: Accessibility,
props: {
brailleNotationInAlt: true, // show the braille notation of the position in the alt attribute of the SVG image
boardAsTable: true, // display the board additionally as HTML table
movePieceForm: true, // display a form to move a piece (from, to, move)
piecesAsList: true, // display the pieces additionally as List
visuallyHidden: false // hide all those extra outputs visually but keep them accessible for screen readers and braille displays
}
}]
})
`
#### Keyboard Shortcut
When movePieceForm` is enabled, press Shift+Option+E (Mac) or Shift+Alt+E (Windows/Linux) to focus the "Move from" input field.
- Works with Vue out of the box
- Works with Svelte out of the box
- I don't use React, but there exists a ticket from someone who is using cm-chessboard with
react: https://github.com/shaack/cm-chessboard/issues/20
- It should work also with all other JS frameworks, because cm-chessboard is written in standard ES6 and has **no
dependencies**.
- License for the code: MIT
- License for the Staunty SVG-pieces (
chessboard-sprite-staunty.svg): CC BY-NC-SA 4.0
- License for the Wikimedia SVG-pieces (
chessboard-sprite.svg): CC BY-SA 3.0
---
Find more high quality JavaScript modules from shaack.com
on our projects page.