chess2img 0.1.2 → 0.1.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/README.md CHANGED
@@ -1,26 +1,56 @@
1
1
  # chess2img
2
2
 
3
- [![npm version](https://img.shields.io/npm/v/chess2img)](https://www.npmjs.com/package/chess2img)
4
- [![License: MIT](https://img.shields.io/npm/l/chess2img)](https://www.npmjs.com/package/chess2img)
3
+ <div align="center">
4
+ <img src="./assets/themes/cburnett/wK.png" alt="chess2img king icon" width="88" />
5
+ <p>Generate PNG chessboard images from FEN, PGN, or board arrays in Node.js.</p>
6
+ <p>
7
+ <a href="https://www.npmjs.com/package/chess2img"><img src="https://img.shields.io/npm/v/chess2img" alt="npm version" /></a>
8
+ <a href="https://www.npmjs.com/package/chess2img"><img src="https://img.shields.io/npm/dm/chess2img" alt="npm downloads" /></a>
9
+ <a href="https://www.npmjs.com/package/chess2img"><img src="https://img.shields.io/npm/l/chess2img" alt="license" /></a>
10
+ <a href="https://github.com/ZiriloXXX/chess2img"><img src="https://img.shields.io/github/stars/ZiriloXXX/chess2img" alt="GitHub stars" /></a>
11
+ </p>
12
+ </div>
13
+
14
+ ## Overview
15
+
16
+ `chess2img` renders chessboard PNGs from FEN, PGN, or board-array inputs with a small Promise-based API for JavaScript and TypeScript users on Node.js.
17
+
18
+ ## Features
19
+
20
+ - Render PNG chessboard images from `fen`, `pgn`, or `board` input.
21
+ - Use five bundled built-in themes: `merida`, `alpha`, `cburnett`, `cheq`, and `leipzig`.
22
+ - Highlight squares such as the last move or key tactical ideas.
23
+ - Customize board colors, size, padding, and board orientation.
24
+ - Use either the functional `renderChess(...)` API or the `ChessImageGenerator` class API.
25
+ - Register custom themes globally or pass a theme inline for one-off rendering.
26
+ - Consume the package from both JavaScript and TypeScript projects.
5
27
 
6
- `chess2img` renders PNG chess board images from FEN, PGN, or board-array inputs for Node.js with a small Promise-based API for JavaScript and TypeScript users.
28
+ ## Example Output
7
29
 
8
- ## Quick Start
30
+ Opening `1.e4` with a Chess.com-like board palette, highlighted origin and destination squares, and the built-in `cburnett` piece set.
9
31
 
10
- Install:
32
+ <img src="./assets/readme/chesscom-opening-e4-cburnett.png" alt="Opening 1.e4 example board" width="480" />
33
+
34
+ ## Quick Start
11
35
 
12
36
  ```bash
13
37
  npm install chess2img
14
38
  ```
15
39
 
16
- Render a board image:
17
-
18
40
  ```ts
19
41
  import { writeFile } from "node:fs/promises";
20
42
  import { renderChess } from "chess2img";
21
43
 
22
44
  const png = await renderChess({
23
- fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
45
+ fen: "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1",
46
+ size: 480,
47
+ style: "cburnett",
48
+ colors: {
49
+ lightSquare: "#EEEED2",
50
+ darkSquare: "#769656",
51
+ highlight: "rgba(246, 246, 105, 0.6)",
52
+ },
53
+ highlightSquares: ["e2", "e4"],
24
54
  });
25
55
 
26
56
  await writeFile("board.png", png);
@@ -28,19 +58,13 @@ await writeFile("board.png", png);
28
58
 
29
59
  `renderChess(...)` returns a `Promise<Buffer>` containing PNG data.
30
60
 
31
- ## Example Output
32
-
33
- Example board rendered with a built-in theme:
34
-
35
- ![Example board output](https://raw.githubusercontent.com/ZiriloXXX/chess2img/main/tests/fixtures/golden/board-initial-cburnett.png)
36
-
37
61
  ## Installation
38
62
 
39
63
  ```bash
40
64
  npm install chess2img
41
65
  ```
42
66
 
43
- ## Node And Canvas Requirements
67
+ ### Node And Canvas Requirements
44
68
 
45
69
  - Node.js `18+`
46
70
  - native build support for `canvas`
@@ -48,116 +72,60 @@ npm install chess2img
48
72
 
49
73
  If `canvas` fails to install, verify your system packages first. The library ships `canvas` as a direct dependency, but native prerequisites still need to exist on the host.
50
74
 
51
- ## JavaScript Usage
75
+ ## Basic Usage
52
76
 
53
- ```js
54
- const { ChessImageGenerator } = require("chess2img");
55
-
56
- async function main() {
57
- const generator = new ChessImageGenerator({
58
- size: 800,
59
- style: "alpha",
60
- });
61
-
62
- await generator.loadPGN("1. e4 e5 2. Nf3 Nc6 3. Bb5 a6");
63
- generator.setHighlights(["e4", "e5"]);
64
-
65
- const buffer = await generator.toBuffer();
66
- await generator.toFile("board.png");
67
- }
68
-
69
- main().catch(console.error);
70
- ```
71
-
72
- ## TypeScript Usage
77
+ ### Functional API
73
78
 
74
79
  ```ts
75
- import { ChessImageGenerator, renderChess } from "chess2img";
76
-
77
- const buffer = await renderChess({
78
- board: [
79
- ["r", "n", "b", "q", "k", "b", "n", "r"],
80
- ["p", "p", "p", "p", "p", "p", "p", "p"],
81
- [null, null, null, null, null, null, null, null],
82
- [null, null, null, null, null, null, null, null],
83
- [null, null, null, null, null, null, null, null],
84
- [null, null, null, null, null, null, null, null],
85
- ["P", "P", "P", "P", "P", "P", "P", "P"],
86
- ["R", "N", "B", "Q", "K", "B", "N", "R"],
87
- ],
88
- size: 640,
89
- style: "cburnett",
80
+ import { writeFile } from "node:fs/promises";
81
+ import { renderChess } from "chess2img";
82
+
83
+ const png = await renderChess({
84
+ fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
85
+ size: 480,
86
+ style: "merida",
90
87
  });
91
88
 
92
- const generator = new ChessImageGenerator({ size: 640, theme: "leipzig" });
93
- await generator.loadFEN("4k3/8/8/3p4/4P3/8/8/4K3 w - - 0 1");
94
- await generator.toFile("board.png");
89
+ await writeFile("board.png", png);
95
90
  ```
96
91
 
97
- ## API
98
-
99
92
  ### Class API
100
93
 
101
94
  ```ts
95
+ import { ChessImageGenerator } from "chess2img";
96
+
102
97
  const generator = new ChessImageGenerator({
103
98
  size: 800,
104
- style: "merida",
105
- flipped: false,
99
+ style: "alpha",
106
100
  });
107
101
 
108
- await generator.loadFEN(fen);
109
- generator.setHighlights(["e4", "d5"]);
102
+ await generator.loadPGN("1. e4 e5 2. Nf3 Nc6 3. Bb5 a6");
103
+ generator.setHighlights(["e4", "e5"]);
110
104
 
111
- const buffer = await generator.toBuffer();
112
105
  await generator.toFile("board.png");
113
106
  ```
114
107
 
115
- Methods:
116
-
117
- - `loadFEN(fen: string): Promise<void>`
118
- - `loadPGN(pgn: string): Promise<void>`
119
- - `loadBoard(board: BoardArray): Promise<void>`
120
- - `setHighlights(squares: Square[]): void`
121
- - `clearHighlights(): void`
122
- - `toBuffer(): Promise<Buffer>`
123
- - `toFile(filePath: string): Promise<void>`
108
+ ### JavaScript Usage
124
109
 
125
- Semantics:
110
+ ```js
111
+ const { renderChess } = require("chess2img");
112
+ const { writeFile } = require("node:fs/promises");
126
113
 
127
- - `setHighlights` replaces the current highlight set
128
- - `clearHighlights` removes all highlights
129
- - loading a new position clears highlights
130
- - constructor defaults persist across position loads
114
+ async function main() {
115
+ const png = await renderChess({
116
+ fen: "4k3/8/8/8/8/8/8/4K3 w - - 0 1",
117
+ style: "merida",
118
+ });
131
119
 
132
- ### Functional API
120
+ await writeFile("board.png", png);
121
+ }
133
122
 
134
- ```ts
135
- const buffer = await renderChess({
136
- fen,
137
- size: 800,
138
- style: "merida",
139
- });
123
+ main().catch(console.error);
140
124
  ```
141
125
 
142
- `renderChess` accepts exactly one of:
126
+ ## Built-In Themes
143
127
 
144
- - `fen`
145
- - `pgn`
146
- - `board`
147
-
148
- ## Options Reference
149
-
150
- - `size`: board size in pixels
151
- - `padding`: `[top, right, bottom, left]`
152
- - `flipped`: render from black's perspective when `true`
153
- - `style`: built-in theme alias
154
- - `theme`: built-in theme name, registered custom theme name, or inline `ThemeDefinition`
155
- - `highlightSquares`: array of algebraic squares such as `["e4", "d5"]`
156
- - `colors.lightSquare`
157
- - `colors.darkSquare`
158
- - `colors.highlight`
159
-
160
- ## Built-In Styles
128
+ Bundled built-in themes:
161
129
 
162
130
  - `merida`
163
131
  - `alpha`
@@ -165,6 +133,8 @@ const buffer = await renderChess({
165
133
  - `cheq`
166
134
  - `leipzig`
167
135
 
136
+ Built-in themes are vendored in-package and render through the same theme pipeline as custom themes.
137
+
168
138
  ## Custom Themes
169
139
 
170
140
  Register a reusable theme globally:
@@ -176,7 +146,7 @@ registerTheme({
176
146
  name: "custom-theme",
177
147
  displayName: "Custom Theme",
178
148
  license: "MIT",
179
- attribution: "Project-authored",
149
+ attribution: "Theme author or package source",
180
150
  pieces: {
181
151
  // 12 canonical pieces required
182
152
  },
@@ -193,7 +163,54 @@ Custom themes may use either:
193
163
  - `svg` assets
194
164
  - `png` assets
195
165
 
196
- ## Error Model
166
+ ## API
167
+
168
+ ### Public Exports
169
+
170
+ - `new ChessImageGenerator(options?)`
171
+ - `renderChess(options)`
172
+ - `registerTheme(theme)`
173
+
174
+ ### Functional API
175
+
176
+ `renderChess(...)` accepts exactly one position source:
177
+
178
+ - `fen`
179
+ - `pgn`
180
+ - `board`
181
+
182
+ ### Class API
183
+
184
+ Methods:
185
+
186
+ - `loadFEN(fen: string): Promise<void>`
187
+ - `loadPGN(pgn: string): Promise<void>`
188
+ - `loadBoard(board: BoardArray): Promise<void>`
189
+ - `setHighlights(squares: Square[]): void`
190
+ - `clearHighlights(): void`
191
+ - `toBuffer(): Promise<Buffer>`
192
+ - `toFile(filePath: string): Promise<void>`
193
+
194
+ Semantics:
195
+
196
+ - `setHighlights` replaces the current highlight set
197
+ - `clearHighlights` removes all highlights
198
+ - loading a new position clears highlights
199
+ - constructor defaults persist across position loads
200
+
201
+ ### Options
202
+
203
+ - `size`: board size in pixels
204
+ - `padding`: `[top, right, bottom, left]`
205
+ - `flipped`: render from black's perspective when `true`
206
+ - `style`: built-in theme alias
207
+ - `theme`: built-in theme name, registered custom theme name, or inline `ThemeDefinition`
208
+ - `highlightSquares`: array of algebraic squares such as `["e4", "d5"]`
209
+ - `colors.lightSquare`
210
+ - `colors.darkSquare`
211
+ - `colors.highlight`
212
+
213
+ ### Errors
197
214
 
198
215
  The library exports:
199
216
 
@@ -203,30 +220,8 @@ The library exports:
203
220
  - `RenderError`
204
221
  - `IOError`
205
222
 
206
- ## Architecture Summary
207
-
208
- - `core` parses and validates chess position input into a canonical board model
209
- - `themes` validates, registers, and resolves built-in or custom themes
210
- - `render` rasterizes SVG or PNG theme assets and renders PNG output through `canvas`
211
- - `api` orchestrates parsing, theme resolution, rendering, and file output
212
-
213
- ## Migration From `chess-image-generator`
214
-
215
- | Old | New | Difference |
216
- | --- | --- | --- |
217
- | `loadArray` | `loadBoard` | renamed for clarity |
218
- | `highlightSquares([...])` | `setHighlights([...])` | replacement semantics are explicit |
219
- | `generateBuffer()` | `toBuffer()` | same role, new naming |
220
- | `generatePNG(path)` | `toFile(path)` | IO is explicit in the API name |
221
- | `style` only | `style` or `theme` | `theme` is the canonical path |
222
-
223
- ## Troubleshooting
224
-
225
- - `canvas` install failures usually indicate missing native prerequisites on the host
226
- - `ValidationError` from `renderChess` usually means multiple position inputs were provided or an option shape is invalid
227
- - `ThemeError` usually indicates an unknown theme name or an incomplete custom theme definition
228
- - `RenderError` usually indicates asset decoding/rasterization problems
223
+ ## License
229
224
 
230
- ## Asset Attribution
225
+ `chess2img` is distributed under the MIT license in package metadata.
231
226
 
232
227
  Bundled built-in themes are derived from the upstream `andyruwruw/chess-image-generator` resource packs and are vendored in-package for deterministic installs. Provenance and licensing notes live in [ATTRIBUTION.md](./ATTRIBUTION.md).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chess2img",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Modern chess board image rendering for Node.js",
5
5
  "license": "MIT",
6
6
  "repository": {