1.0.6 • Published 11 months ago

node-chessboard v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

Overview

Generate a Buffer with a provided PGN or FEN with full customisability. Intended to work alongside chess.js.

Documentation

Installation

Install via node:

npm install node-chessboard

Import the package and instantiate a new Chessboard.

import { Chessboard } from "node-chessboard";

const chessboard = new Chessboard();

You may also pass in options for customisability.

import { Chessboard, Theme } from "node-chessboard";

const chessboard = new Chessboard({
	size: 720,
	light: "rgb(240, 217, 181)"
	dark: "rgb(181, 136, 99)",
	highlight: "rgba(235, 97, 80, 0.8)"
});

Load in your chess positions with any of the provided methods and export as a Buffer.

Supported Formats

Loading by FEN

.loadFEN(fen)
ParameterTypeDescription
fenstringIf you are using chess.js, you can retrieve the FEN value with Chess.fen()

FEN appears in the following format:

// 8/8/8/8/8/8/8/8 w - - 0 1

Loading by PGN

.loadPGN(pgn)
ParameterTypeDescription
pgnstringIf you are using chess.js, you can retrieve the PGN value with Chess.pgn()

PGN appears in the following format:

1. e4 e5 2. Nf3 Nc6 3. Bb5 a6 {This opening is called the Ruy Lopez.}
4. Ba4 Nf6 5. O-O Be7 6. Re1 b5 7. Bb3 d6 8. c3 O-O 9. h3 Nb8 10. d4 Nbd7
11. c4 c6 12. cxb5 axb5 13. Nc3 Bb7 14. Bg5 b4 15. Nb1 h6 16. Bh4 c5 17. dxe5
Nxe4 18. Bxe7 Qxe7 19. exd6 Qf6 20. Nbd2 Nxd6 21. Nc4 Nxc4 22. Bxc4 Nb6
23. Ne5 Rae8 24. Bxf7+ Rxf7 25. Nxf7 Rxe1+ 26. Qxe1 Kxf7 27. Qe3 Qg5 28. Qxg5
hxg5 29. b3 Ke6 30. a3 Kd6 31. axb4 cxb4 32. Ra5 Nd5 33. f3 Bc8 34. Kf2 Bf5
35. Ra7 g6 36. Ra6+ Kc5 37. Ke1 Nf4 38. g3 Nxh3 39. Kd2 Kb5 40. Rd6 Kc5 41. Ra6
Nf2 42. g4 Bd3 43. Re6 1/2-1/2

Generating a Buffer

After you've loaded a chess position, you can use .buffer() to generate a buffer.

ParameterTypeDescription
mimestringA string that specifies the MIME type of the image. Only accepts image/png, image/jpeg, image/webp, image/avif and image/gif
optionsBufferOptions?The buffer options. See the BufferOptions interface below for detailed information.

BufferOptions

PropertyTypeDescription
delaynumber?Sets the delay between each frame, 500 by default. Only applicable if mime is image/gif
movenumber?Specifies the move number in a PGN string to which the game should be played up to. For example:
- If move is 2, and the PGN string loaded is "1. Nc3 Nf6 2. Ne4", the result should be "1. Nc3 Nf6"
- Alternatively, if move is 1, the result should be "1. Nc3"

Example

This is an example of loading a gif and png Buffer for a game that had Fool's Mate.

import { Chessboard } from "node-chessboard";

const chessboard = new Chessboard();
// Loading Fool's Mate
chessboard.loadPGN("1. e4 g5 2. Nc3 f5 3. Qh5#");
chessboard.buffer("image/gif").then(buffer => fs.writeFile("assets/game.gif", buffer));

// Getting the 4th move
chessboard.buffer("image/png", { move: 4, highlight: true }).then(buffer => fs.writeFile("assets/blunder.png", buffer));

These are the gif and png output respectively:

Dependencies

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago