-
Notifications
You must be signed in to change notification settings - Fork 20
/
chess-image-generator.js
176 lines (156 loc) · 4.19 KB
/
chess-image-generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const {
createCanvas,
loadImage,
} = require('canvas');
const Frame = require('canvas-to-buffer');
const { Chess } = require('chess.js');
const fs = require('fs');
const path = require('path');
const {
cols,
white,
black,
defaultSize,
defaultLight,
defaultDark,
deafultStyle,
filePaths,
} = require('./config/index');
/**
* Object constructor, initializes options.
* @param {object} options Optional options
* @param {number} size Pixel length of desired image
* @param {string} light Color of light squares
* @param {string} dark Color of dark squares
* @param {style} style Desired style of pieces
*/
function ChessImageGenerator(options = {}) {
this.chess = new Chess();
this.size = options.size || defaultSize;
this.light = options.light || defaultLight;
this.dark = options.dark || defaultDark;
this.style = options.style || deafultStyle;
this.ready = false;
}
ChessImageGenerator.prototype = {
/**
* Loads PGN into chess.js object.
* @param {string} pgn Chess game PGN
*/
async loadPGN(pgn) {
if (!this.chess.load_pgn(pgn)) {
throw new Error('PGN could not be read successfully');
} else {
this.ready = true;
}
},
/**
* Loads FEN into chess.js object
* @param {string} fen Chess position FEN
*/
async loadFEN(fen) {
if (!this.chess.load(fen)) {
throw new Error('FEN could not be read successfully');
} else {
this.ready = true;
}
},
/**
* Loads position array into chess.js object
* @param {Array<string>} array Chess position array
*/
loadArray(array) {
this.chess.clear();
for (let i = 0; i < array.length; i += 1) {
for (let j = 0; j < array[i].length; j += 1) {
if (array[i][j] !== '' && black.includes(array[i][j].toLowerCase())) {
this.chess.put(
{
type: array[i][j].toLowerCase(),
color: white.includes(array[i][j]) ? 'w' : 'b',
},
(cols[j] + (8 - i)),
);
}
}
}
this.ready = true;
},
/**
* Generates buffer image based on position
* @returns {Buffer} Image buffer
*/
async generateBuffer() {
if (!this.ready) {
throw new Error('Load a position first');
}
const canvas = createCanvas(this.size, this.size);
const ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.rect(
0,
0,
this.size,
this.size,
);
ctx.fillStyle = this.light;
ctx.fill();
for (let i = 0; i < 8; i += 1) {
for (let j = 0; j < 8; j += 1) {
if ((i + j) % 2 === 0) {
ctx.beginPath();
ctx.rect(
((this.size / 8) * ((7-j)+1)) - (this.size / 8),
((this.size / 8) * (i)) ,
(this.size / 8) ,
(this.size / 8)
);
ctx.fillStyle = this.dark;
ctx.fill();
}
const piece = this.chess.get(cols[7 - j] + ((7 - i) + 1));
if (piece && piece.type !== '' && black.includes(piece.type.toLowerCase())) {
const image = `resources/${this.style}/${filePaths[`${piece.color}${piece.type}`]}.png`;
const imageFile = await loadImage(path.join(__dirname, image));
await ctx.drawImage(
imageFile,
((this.size / 8) * ((7-j)+1)) - (this.size / 8),
((this.size / 8) * (i)) ,
(this.size / 8) ,
(this.size / 8)
);
}
}
}
const frame = new Frame(canvas, {
image: {
types: [
'png',
],
},
});
return frame.toBuffer();
},
/**
* Generates PNG image based on position
* @param {string} pngPath File name
*/
async generatePNG(pngPath) {
if (!this.ready) {
throw new Error('Load a position first');
}
const buffer = await this.generateBuffer();
fs.open(pngPath, 'w', (err, fd) => {
if (err) {
throw new Error(`could not open file: ${err}`);
}
fs.write(fd, buffer, 0, buffer.length, null, (writeError) => {
if (writeError) {
throw new Error(`error writing file: ${writeError}`);
}
fs.close(fd, () => pngPath);
});
});
},
};
module.exports = ChessImageGenerator;