From c5f73edcd273f1cd33694243740e248bef0472c9 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 5 May 2017 18:12:26 +0200 Subject: [PATCH] Convert the `DOMCanvasFactory` to an ES6 class For consistency, also updates the `pdf2png.js` example to use the slightly less verbose `canvasAndContext` parameter name. --- examples/node/pdf2png/pdf2png.js | 26 ++++++++++++------------ src/display/dom_utils.js | 35 ++++++++++++++++---------------- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/examples/node/pdf2png/pdf2png.js b/examples/node/pdf2png/pdf2png.js index 468d1bab8..b595861d0 100644 --- a/examples/node/pdf2png/pdf2png.js +++ b/examples/node/pdf2png/pdf2png.js @@ -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); diff --git a/src/display/dom_utils.js b/src/display/dom_utils.js index cd359ee9a..06dfafd76 100644 --- a/src/display/dom_utils.js +++ b/src/display/dom_utils.js @@ -20,37 +20,36 @@ import { var DEFAULT_LINK_REL = 'noopener noreferrer nofollow'; -function DOMCanvasFactory() {} -DOMCanvasFactory.prototype = { - create: function DOMCanvasFactory_create(width, height) { +class DOMCanvasFactory { + create(width, height) { assert(width > 0 && height > 0, 'invalid canvas size'); - var canvas = document.createElement('canvas'); - var context = canvas.getContext('2d'); + let canvas = document.createElement('canvas'); + let context = canvas.getContext('2d'); canvas.width = width; canvas.height = height; return { canvas, context, }; - }, + } - reset: function DOMCanvasFactory_reset(canvasAndContextPair, width, height) { - assert(canvasAndContextPair.canvas, 'canvas is not specified'); + 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 DOMCanvasFactory_destroy(canvasAndContextPair) { - assert(canvasAndContextPair.canvas, 'canvas is not specified'); + 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; } -}; +} class DOMCMapReaderFactory { constructor({ baseUrl = null, isCompressed = false, }) {