-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageGenerator.js
62 lines (59 loc) · 1.7 KB
/
imageGenerator.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
// Require library
const PIECE_SIZE_X = 110,
PIECE_SIZE_Y = 110;
var Jimp = require('jimp');
var baseImage;
var pieces;
var coords = {
"wK": {x: 0, y: 0}, "bK":{x: 0, y: 110},
"wQ": {x: 110, y: 0}, "bQ":{x: 110, y: 110},
"wB": {x: 220, y: 0}, "bB":{x: 220, y: 110},
"wN": {x: 330, y: 0}, "bN":{x: 330, y: 110},
"wR": {x: 440, y: 0}, "bR":{x: 440, y: 110},
"wP": {x: 550, y: 0}, "bP":{x: 550, y: 110}
}
var baseImageP = Jimp.read("./assets/base_board.png");
var chessPieceP = Jimp.read("./assets/chess_pieces.png");
Promise.all([baseImageP, chessPieceP]).then(function(values){
baseImage = values[0];
pieces = values[1];
}).catch(function(err){
console.log("ERROR LOADING PICTURES: " + err);
});
module.exports.createTestImage = function(){
return new Promise(function (resolve, reject){
console.log("Entering create Test Image");
var image = new Jimp(1024, 1024, function (err, image){
//idk if i need this call back;
image.getBuffer(Jimp.MIME_PNG, function(err, thing){
console.log("err: "+err);
console.log(thing);
resolve(thing);
})
});
})
}
module.exports.createImage = function(board){
return new Promise(function (resolve, reject){
console.log("Creating Image");
console.log(baseImage);
var image = baseImage.clone();
for (var i = 0; i < 8; i++){
for (var k = 0; k < 8; k++){
let el = board[i][k];
if(el != 0){
image.composite(pieces.clone().crop(coords[el].x, coords[el].y, PIECE_SIZE_X, PIECE_SIZE_Y), 72+k*110, 72+i*110);
}
}
}
image.getBuffer(Jimp.MIME_PNG, function(err, thing){
if (err){
console.log("err: " + err);
reject(err);
}
//Do things to image to make it board
console.log(thing);
resolve(thing);
});
})
}