-
Notifications
You must be signed in to change notification settings - Fork 4
/
render-image.js
48 lines (36 loc) · 1.28 KB
/
render-image.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
const fs = require('fs');
const path = require("path");
//const { createCanvas } = require('canvas');
const PImage = require('pureimage');
const cellSize = 7;
const marginSize = 1;
async function renderImage(engine, index, dir) {
const {colSize, rowSize} = engine.config;
const w = cellSize*colSize;
const h = cellSize*rowSize;
const canvas = PImage.make(w, h);
//const canvas = createCanvas(w, h, "png");
const context = canvas.getContext('2d');
context.fillStyle = "#fff";
context.fillRect(0, 0, w, h);
context.fillStyle = "#000";
for (let i = 0; i < rowSize; i++) {
for (let j = 0; j < colSize; j++) {
let y = j * cellSize + marginSize;
let x = i * cellSize + marginSize;
let sw = cellSize - 2*marginSize;
let sh = cellSize - 2*marginSize;
if (engine.getState(i, j) > 0) {
context.fillRect(x, y, sw, sh);
}
}
}
const fileName = path.join(dir, `${index.toString().padStart(4, "0")}.png`);
console.log(fileName);
/*const buffer = canvas.toBuffer('image/png', { compressionLevel: 0, filters: 'PNG_FILTER_NONE'});
fs.writeFileSync(fileName, buffer);*/
await PImage.encodePNGToStream(canvas, fs.createWriteStream(fileName));
return [w, h];
}
exports.cellSize = cellSize;
exports.renderImage = renderImage;