-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeCanvasFactory.js
More file actions
42 lines (33 loc) · 1.19 KB
/
NodeCanvasFactory.js
File metadata and controls
42 lines (33 loc) · 1.19 KB
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
/* eslint-disable no-param-reassign */
/* eslint-disable class-methods-use-this */
/*
* This code was taken from https://github.com/mozilla/pdf.js/blob/master/examples/node/pdf2png/pdf2png.js
*/
import Canvas from 'canvas';
import { strict as assert } from 'assert';
export default class NodeCanvasFactory {
create(width, height) {
assert(width > 0 && height > 0, 'Invalid canvas size');
const canvas = Canvas.createCanvas(width, height);
const context = canvas.getContext('2d');
return {
canvas,
context,
};
}
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;
}
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;
}
}