-
Notifications
You must be signed in to change notification settings - Fork 0
/
NodeCanvasFactory.js
36 lines (32 loc) · 1.2 KB
/
NodeCanvasFactory.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
// https://github.com/mozilla/pdf.js/tree/master/examples/node/pdf2png
const assert = require('assert').strict;
const Canvas = require('canvas');
class NodeCanvasFactory {
constructor() {
this.create = (width, height) => {
assert(width > 0 && height > 0, 'Invalid canvas size');
var canvas = Canvas.createCanvas(width, height);
var context = canvas.getContext('2d');
return {
canvas: canvas,
context: context,
};
};
this.reset = (canvasAndContext, width, height) => {
assert(canvasAndContext.canvas, 'Canvas is not specified');
assert(width > 0 && height > 0, 'Invalid canvas size');
canvasAndContext.canvas.width = width;
canvasAndContext.canvas.height = height;
};
this.destroy = (canvasAndContext) => {
assert(canvasAndContext.canvas, 'Canvas is not specified');
// Zeroing the width and height cause Firefox to release graphics
// resources immediately, which can greatly reduce memory consumption.
canvasAndContext.canvas.width = 0;
canvasAndContext.canvas.height = 0;
canvasAndContext.canvas = null;
canvasAndContext.context = null;
};
}
}
module.exports = { NodeCanvasFactory };