Browse Source

Convert the `DOMCanvasFactory` to an ES6 class

For consistency, also updates the `pdf2png.js` example to use the slightly less verbose `canvasAndContext` parameter name.
Jonas Jenwald 8 years ago
parent
commit
c5f73edcd2
  1. 26
      examples/node/pdf2png/pdf2png.js
  2. 35
      src/display/dom_utils.js

26
examples/node/pdf2png/pdf2png.js

@ -29,22 +29,22 @@ NodeCanvasFactory.prototype = {
}; };
}, },
reset: function NodeCanvasFactory_reset(canvasAndContextPair, width, height) { reset: function NodeCanvasFactory_reset(canvasAndContext, width, height) {
assert(canvasAndContextPair.canvas, 'Canvas is not specified'); assert(canvasAndContext.canvas, 'Canvas is not specified');
assert(width > 0 && height > 0, 'Invalid canvas size'); assert(width > 0 && height > 0, 'Invalid canvas size');
canvasAndContextPair.canvas.width = width; canvasAndContext.canvas.width = width;
canvasAndContextPair.canvas.height = height; canvasAndContext.canvas.height = height;
}, },
destroy: function NodeCanvasFactory_destroy(canvasAndContextPair) { destroy: function NodeCanvasFactory_destroy(canvasAndContext) {
assert(canvasAndContextPair.canvas, 'Canvas is not specified'); assert(canvasAndContext.canvas, 'Canvas is not specified');
// Zeroing the width and height cause Firefox to release graphics // Zeroing the width and height cause Firefox to release graphics
// resources immediately, which can greatly reduce memory consumption. // resources immediately, which can greatly reduce memory consumption.
canvasAndContextPair.canvas.width = 0; canvasAndContext.canvas.width = 0;
canvasAndContextPair.canvas.height = 0; canvasAndContext.canvas.height = 0;
canvasAndContextPair.canvas = null; canvasAndContext.canvas = null;
canvasAndContextPair.context = null; canvasAndContext.context = null;
}, },
}; };
@ -65,16 +65,16 @@ pdfjsLib.getDocument(rawData).then(function (pdfDocument) {
// Render the page on a Node canvas with 100% scale. // Render the page on a Node canvas with 100% scale.
var viewport = page.getViewport(1.0); var viewport = page.getViewport(1.0);
var canvasFactory = new NodeCanvasFactory(); var canvasFactory = new NodeCanvasFactory();
var canvasAndContextPair = canvasFactory.create(viewport.width, viewport.height); var canvasAndContext = canvasFactory.create(viewport.width, viewport.height);
var renderContext = { var renderContext = {
canvasContext: canvasAndContextPair.context, canvasContext: canvasAndContext.context,
viewport: viewport, viewport: viewport,
canvasFactory: canvasFactory canvasFactory: canvasFactory
}; };
page.render(renderContext).then(function () { page.render(renderContext).then(function () {
// Convert the canvas to an image buffer. // Convert the canvas to an image buffer.
image = canvasAndContextPair.canvas.toBuffer(); var image = canvasAndContext.canvas.toBuffer();
fs.writeFile('output.png', image, function (error) { fs.writeFile('output.png', image, function (error) {
if (error) { if (error) {
console.error('Error: ' + error); console.error('Error: ' + error);

35
src/display/dom_utils.js

@ -20,37 +20,36 @@ import {
var DEFAULT_LINK_REL = 'noopener noreferrer nofollow'; var DEFAULT_LINK_REL = 'noopener noreferrer nofollow';
function DOMCanvasFactory() {} class DOMCanvasFactory {
DOMCanvasFactory.prototype = { create(width, height) {
create: function DOMCanvasFactory_create(width, height) {
assert(width > 0 && height > 0, 'invalid canvas size'); assert(width > 0 && height > 0, 'invalid canvas size');
var canvas = document.createElement('canvas'); let canvas = document.createElement('canvas');
var context = canvas.getContext('2d'); let context = canvas.getContext('2d');
canvas.width = width; canvas.width = width;
canvas.height = height; canvas.height = height;
return { return {
canvas, canvas,
context, context,
}; };
}, }
reset: function DOMCanvasFactory_reset(canvasAndContextPair, width, height) { reset(canvasAndContext, width, height) {
assert(canvasAndContextPair.canvas, 'canvas is not specified'); assert(canvasAndContext.canvas, 'canvas is not specified');
assert(width > 0 && height > 0, 'invalid canvas size'); assert(width > 0 && height > 0, 'invalid canvas size');
canvasAndContextPair.canvas.width = width; canvasAndContext.canvas.width = width;
canvasAndContextPair.canvas.height = height; canvasAndContext.canvas.height = height;
}, }
destroy: function DOMCanvasFactory_destroy(canvasAndContextPair) { destroy(canvasAndContext) {
assert(canvasAndContextPair.canvas, 'canvas is not specified'); assert(canvasAndContext.canvas, 'canvas is not specified');
// Zeroing the width and height cause Firefox to release graphics // Zeroing the width and height cause Firefox to release graphics
// resources immediately, which can greatly reduce memory consumption. // resources immediately, which can greatly reduce memory consumption.
canvasAndContextPair.canvas.width = 0; canvasAndContext.canvas.width = 0;
canvasAndContextPair.canvas.height = 0; canvasAndContext.canvas.height = 0;
canvasAndContextPair.canvas = null; canvasAndContext.canvas = null;
canvasAndContextPair.context = null; canvasAndContext.context = null;
}
} }
};
class DOMCMapReaderFactory { class DOMCMapReaderFactory {
constructor({ baseUrl = null, isCompressed = false, }) { constructor({ baseUrl = null, isCompressed = false, }) {

Loading…
Cancel
Save