-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.js
37 lines (34 loc) · 1005 Bytes
/
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
const fs = require("fs")
const { createCanvas } = require("canvas")
let i = 0
const OFFSET = 1_000
module.exports.Image = class Image {
constructor(w, h) {
this.width = w
this.height = h
this.canvas = createCanvas(this.width, this.height);
this.context = this.canvas.getContext("2d");
this.context.fillStyle = "#000000ff";
this.context.fillRect(0, 0, this.width, this.height);
}
setPixelColor(x, y, c) {
this.context.fillStyle = "#" +
(c.r).x(2) +
(c.g).x(2) +
(c.b).x(2) +
(c.a).x(2);
this.context.fillRect(x, y, 1, 1);
}
toBuffer() {
return this.canvas.toBuffer("image/png")
}
saveAsPng(pth) {
if (!fs.existsSync(pth)) {
fs.mkdirSync(pth)
}
if ((i % OFFSET) == 0) {
fs.writeFileSync(pth + "/" + (i).toString().padStart(16, "0") + ".png", this.canvas.toBuffer("image/png"));
}
++i
}
}