Giter VIP home page Giter VIP logo

cm-chessboard's Introduction

cm-chessboard

A Lightweight, ES6 module based, responsive, mobile friendly SVG chessboard without dependencies. It works on desktop (current versions of Chrome, Firefox, Safari, Edge), and mobile (Android and iOS).

cm-chessboard is the main chessboard of chessmail.eu / chessmail.de, and is used every day by thousands of users.

Features

Demo and Repository

Example chessboards

Install

Option 1: Install the npm package with npm install cm-chessboard, or

Option 2: Download the code from GitHub and run npm install.

After installation, copy the cm-chessboard/assets/images/chessboard-sprite.svg to your projects assets/images folder. If you put the sprite somewhere else you have to configure the location in props.sprite.url.

Example Usage

Preconditions for using cm-chessboard in a web page:

  • include the css: styles/cm-chessboard.css
  • import the ES6 module: import {Chessboard} from "../src/cm-chessboard/Chessboard.js"

Example, showing a FEN:

<script type="module">
    import {Chessboard} from "./src/cm-chessboard/Chessboard.js"

    new Chessboard(document.getElementById("containerId"),
            { position: "rn2k1r1/ppp1pp1p/3p2p1/5bn1/P7/2N2B2/1PPPPP2/2BNK1RR" })
</script>

Take a look at the /examples folder for more simple examples.

Configuration

Below is the default configuration

props = {
    position: "empty", // set as fen, "start" or "empty"
    orientation: COLOR.white, // white on bottom
    style: {
        cssClass: "default",
        showCoordinates: true, // show ranks and files
        showBorder: false, // display a border around the board
        aspectRatio: 0.9 // height/width. Set to null, if you want to define it only in the css.
    },
    responsive: false, // resizes the board on window resize, if true
    animationDuration: 300, // pieces animation duration in milliseconds
    moveInputMode: MOVE_INPUT_MODE.viewOnly, // set to MOVE_INPUT_MODE.dragPiece or MOVE_INPUT_MODE.dragMarker for interactive movement
    sprite: {
        url: "./assets/images/chessboard-sprite.svg", // pieces and markers are stored as svg in the sprite
        grid: 40 // the sprite is tiled with one piece every 40px
    }
}

API

constructor

new Chessboard(containerElement, props = {})

  • containerElement - a HTML DOM element being the container of the widget
  • props - The board configuration (properties)

setPiece(square, piece)

Sets a piece on a square. Example: board.setPiece("e4", PIECE.blackKnight) or board.setPiece("e4", "bn").

Returns a Promise which will be resolved, after the piece is set.

getPiece(square)

Returns the piece on a square or null if the square is empty.

setPosition(fen, animated = true)

Sets the position as fen. Special values are "start", sets the chess start position and "empty", sets an empty board. When animated is set false, the new position will be shown instant.

Returns a Promise which will be resolved, after the Animation has finished.

Example for setPosition

getPosition()

Returns the board position as fen.

addMarker(square, type = MARKER_TYPE.emphasize)

Adds a marker on a square.

Default types are: MARKER_TYPE.move, MARKER_TYPE.emphasize, exportet by Chessboard.js. You can create your own marker types: Just create an object like const myMarker = {class: "my-marker", slice: "marker1"}, where class is the css class of the marker for styling and slice is the id in sprite.svg.

Example for addMarker, getMarkers and removeMarkers

getMarkers(square = null, type = null)

Returns the the board's markers as an array.

Set square to null, to get all markers of a type on the board. Set type to null, to get all types. Set both to null to get all markers on the board.

removeMarkers(square = null, type = null)

Removes markers from the board.

Set square to null to remove markers of type from all squares. Set type to null, to remove all types from a square. Set both to null to remove all markers from the board.

setOrientation(color)

Sets the board orientation (color at bottom). Allowed values are COLOR.white or COLOR.black.

Example for setOrientation

getOrientation()

Returns the the board orientation.

destroy()

Removes the board from the DOM. Returns a Promise which will be resolved, after destruction.

enableMoveInput(eventHandler, color = null)

Enables moves via user input (mouse or touch). Set optional color, if you want to enable the move input for a specific side, COLOR.white or COLOR.black.

eventHandler is called on specific events of the user interaction. Receives the parameter event.

board.enableMoveInput((event) => {
    // handle user input here
}, COLOR.white)

Example for enableMoveInput

The event has the following event.type:

  • INPUT_EVENT_TYPE.moveStart - User started the move input, event.square contains the coordinates
  • INPUT_EVENT_TYPE.moveDone - User finished the move input, event.squareFrom and event.squareTo contain the coordinates
  • INPUT_EVENT_TYPE.moveCanceled - User canceled the move with clicking again ob the start square or clicking outside of the board
chessboard.enableMoveInput((event) => {
    switch (event.type) {
        case INPUT_EVENT_TYPE.moveStart:
            console.log(`moveStart: ${event.square}`)
            // return `true`, if input is accepted/valid, `false` aborts the interaction, the piece will not move
            return true
        case INPUT_EVENT_TYPE.moveDone:
            console.log(`moveDone: ${event.squareFrom}-${event.squareTo}`)
            // return true, if input is accepted/valid, `false` takes the move back
            return true
        case INPUT_EVENT_TYPE.moveCanceled:
            console.log(`moveCanceled`)
    }
}, COLOR.white)

disableMoveInput()

Disables moves via user input.

enableContextInput(eventHandler)

Enables context input (right click on squares).

board.enableContextInput((event) => {
    // handle user context input here    
})

Example for enableContextInput

event.square contains the coordinates of the user input.

disableContextInput()

Disables the context input.

Usage with react

There exists a ticket from someone who is using cm-chessboard with react: shaack#20

Licenses

cm-chessboard's People

Contributors

biowaffeln avatar doomdesign avatar shaack avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.