Home

Awesome

Svelte-chess: Playable chess component

Fully playable chess component for Svelte. Powered by Chess.js logic, Chessground chessboard and optionally Stockfish chess AI.

Svelte-chess screenshots

Features

Usage

Installation:

npm install svelte-chess

Basic playable chessboard (REPL):

<script>
    import {Chess} from 'svelte-chess';
</script>    
<Chess />

Interact with the game via props, methods or events.

Props

Game state can be observed by binding to props.

PropBindable and readableWritableValue
turnCurrent color to move: w or b
moveNumberCurrent move number (whole moves)
historyArray of all moves as SAN strings, e.g. ['d4','Nf6']
inCheckTrue if the player to move is in check.
isGameOverTrue if the game is over. See also the gameOver event.
fenCurrent position in FEN
orientationOrientation of the board: w or b.
engineOptions for the Stockfish chess AI. See Engine.
classCSS class applied to children instead of default (see Styling).

All readable props are bindable and updated whenever the game state changes. Writable props are only used when the component is created.

Example using bindable props to monitor state (REPL):

<script>
    import {Chess} from 'svelte-chess';
    let moveNumber, turn, history;
</script>    
<Chess bind:moveNumber bind:turn bind:history/>
<p>
    It's move {moveNumber}, with {turn} to move.
    Moves played: {history?.join(' ')}.
</p>

Starting from a specific FEN (REPL):

<Chess fen="rnbqkb1r/1p2pppp/p2p1n2/8/3NP3/2N5/PPP2PPP/R1BQKB1R w KQkq - 0 6" />

Methods

The board state can be read and manipulated via method calls to the Chess component itself.

Methods for reading game/board state:

Methods for manipulating game/board state:

Example implementing undo/reset buttons (REPL):

<script>
    import {Chess} from 'svelte-chess';
    let chess;
</script>    
<Chess bind:this={chess}/>
<button on:click={()=>chess?.reset()}>Reset</button>
<button on:click={()=>chess?.undo()}>Undo</button>

Events

A ready event is dispatched when the Chess component is ready for interaction, which is generally immediately on mount. If an engine was specified, the event is dispatched after engine initialisation, which might take a second.

A move event is dispatched after every move, containing the corresponding Move object.

A gameOver event is emitted after a move that ends the game. The GameOver object has two keys:

A uci event is emitted when Stockfish, if enabled, sends a UCI message.

Example listening for move and gameOver events (REPL):

<script>
    import {Chess} from 'svelte-chess';
    function moveListener(event) {
        const move = event.detail;
        console.log( `${move.color} played ${move.san}` );
    }
    function gameOverListener(event) {
        console.log( `The game ended due to ${event.detail.reason}` );
    }
</script>
<Chess on:move={moveListener} on:gameOver={gameOverListener} />

Svelte-chess exports the MoveEvent, GameOverEvent, ReadyEvent and UciEvent types.

Engine / Stockfish

Svelte-chess can be used to play against the chess AI Stockfish 14. You need to download the Stockfish web worker script separately: stockfish.js web worker (1.6MB) and serve it at /stockfish.js. If you're using SvelteKit, do this by putting it in the static folder.

Example playing Black versus Stockfish (live):

<script>
    import Chess, { Engine } from 'svelte-chess';
    // Note: stockfish.js must be manually downloaded (see Readme)
</script>
<Chess engine={new Engine({depth: 20, moveTime: 1500, color: 'w'})} />

The engine prop is an object with the following keys, all optional:

KeyDefaultDescription
colorbColor the engine plays: w or b, or both for an engine-vs-engine game, or none if the engine should only make a move when makeEngineMove() is called.
moveTime2000Max time in milliseconds for the engine to spend on a move.
depth40Max depth in ply for the engine to search.

To inspect Stockfish's current evaluation and other engine details, you can listen to uci events from the Chess component to read all UCI messages sent by Stockfish.

Styling

The stylesheet shipped with Chessground is used by default. To restyle the board, pass the class prop and import a stylesheet.

Example with custom stylesheet:

<script>
    import { Chess } from 'svelte-chess';
</script>
<link rel="stylesheet" href="/my-style.css" />
<Chess class="my-class" />

A sample stylesheet can be found in /static/style-paper.css.

Types

Move

A Move describes a chess move. Properties:

Future