1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-29 07:37:57 +02:00

Convert the DOMCanvasFactory to an ES6 class

For consistency, also updates the `pdf2png.js` example to use the slightly less verbose `canvasAndContext` parameter name.
This commit is contained in:
Jonas Jenwald 2017-05-05 18:12:26 +02:00
parent 32baa6af7a
commit c5f73edcd2
2 changed files with 30 additions and 31 deletions

View file

@ -29,22 +29,22 @@ NodeCanvasFactory.prototype = {
};
},
reset: function NodeCanvasFactory_reset(canvasAndContextPair, width, height) {
assert(canvasAndContextPair.canvas, 'Canvas is not specified');
reset: function NodeCanvasFactory_reset(canvasAndContext, width, height) {
assert(canvasAndContext.canvas, 'Canvas is not specified');
assert(width > 0 && height > 0, 'Invalid canvas size');
canvasAndContextPair.canvas.width = width;
canvasAndContextPair.canvas.height = height;
canvasAndContext.canvas.width = width;
canvasAndContext.canvas.height = height;
},
destroy: function NodeCanvasFactory_destroy(canvasAndContextPair) {
assert(canvasAndContextPair.canvas, 'Canvas is not specified');
destroy: function NodeCanvasFactory_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.
canvasAndContextPair.canvas.width = 0;
canvasAndContextPair.canvas.height = 0;
canvasAndContextPair.canvas = null;
canvasAndContextPair.context = null;
canvasAndContext.canvas.width = 0;
canvasAndContext.canvas.height = 0;
canvasAndContext.canvas = null;
canvasAndContext.context = null;
},
};
@ -65,16 +65,16 @@ pdfjsLib.getDocument(rawData).then(function (pdfDocument) {
// Render the page on a Node canvas with 100% scale.
var viewport = page.getViewport(1.0);
var canvasFactory = new NodeCanvasFactory();
var canvasAndContextPair = canvasFactory.create(viewport.width, viewport.height);
var canvasAndContext = canvasFactory.create(viewport.width, viewport.height);
var renderContext = {
canvasContext: canvasAndContextPair.context,
canvasContext: canvasAndContext.context,
viewport: viewport,
canvasFactory: canvasFactory
};
page.render(renderContext).then(function () {
// Convert the canvas to an image buffer.
image = canvasAndContextPair.canvas.toBuffer();
var image = canvasAndContext.canvas.toBuffer();
fs.writeFile('output.png', image, function (error) {
if (error) {
console.error('Error: ' + error);