2016-01-30 07:48:16 -05:00
|
|
|
/*
|
|
|
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
|
|
|
* http://paperjs.org/
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011 - 2016, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://scratchdisk.com/ & http://jonathanpuckey.com/
|
|
|
|
*
|
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Add some useful extensions to HTMLCanvasElement:
|
|
|
|
// - HTMLCanvasElement#type, so we can switch to a PDF canvas
|
2017-04-19 14:32:21 -04:00
|
|
|
// - Various Node-Canvas methods, routed through from HTMLCanvasElement:
|
2017-10-04 16:44:14 -04:00
|
|
|
// toBuffer, pngStream, createPNGStream, jpegStream, createJPEGStream
|
2016-01-30 07:48:16 -05:00
|
|
|
|
2017-03-20 07:44:32 -04:00
|
|
|
module.exports = function(self, requireName) {
|
2016-07-12 13:22:49 -04:00
|
|
|
var Canvas;
|
|
|
|
try {
|
|
|
|
Canvas = require('canvas');
|
|
|
|
} catch(e) {
|
|
|
|
// Remove `self.window`, so we still have the global `self` reference,
|
|
|
|
// but no `window` object:
|
|
|
|
// - On the browser, this corresponds to a worker context.
|
|
|
|
// - On Node.js, it basically means the canvas is missing or not working
|
|
|
|
// which can be treated the same way.
|
2017-03-19 11:07:41 -04:00
|
|
|
delete self.window;
|
2017-03-20 07:44:32 -04:00
|
|
|
// Check the required module's name to see if it contains canvas, and
|
|
|
|
// only complain about its lack if the module requires it.
|
|
|
|
if (/\bcanvas\b/.test(requireName)) {
|
|
|
|
throw new Error('Unable to load canvas module.');
|
|
|
|
}
|
2016-07-12 13:22:49 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-20 07:44:32 -04:00
|
|
|
var HTMLCanvasElement = self.HTMLCanvasElement,
|
|
|
|
idlUtils = require('jsdom/lib/jsdom/living/generated/utils');
|
2016-01-30 07:48:16 -05:00
|
|
|
|
|
|
|
// Add fake HTMLCanvasElement#type property:
|
|
|
|
Object.defineProperty(HTMLCanvasElement.prototype, 'type', {
|
|
|
|
get: function() {
|
2016-07-03 08:20:10 -04:00
|
|
|
var canvas = idlUtils.implForWrapper(this)._canvas;
|
2016-01-30 07:48:16 -05:00
|
|
|
return canvas && canvas.type || 'image';
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(type) {
|
|
|
|
// Allow replacement of internal node-canvas, so we can switch to a
|
|
|
|
// PDF canvas.
|
2016-07-03 08:20:10 -04:00
|
|
|
var impl = idlUtils.implForWrapper(this),
|
2016-01-30 07:48:16 -05:00
|
|
|
size = impl._canvas || impl;
|
|
|
|
impl._canvas = new Canvas(size.width, size.height, type);
|
|
|
|
impl._context = null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Extend HTMLCanvasElement with useful methods from the underlying Canvas:
|
2017-10-04 16:44:14 -04:00
|
|
|
var methods = ['toBuffer', 'pngStream', 'createPNGStream', 'jpegStream',
|
|
|
|
'createJPEGStream'];
|
|
|
|
methods.forEach(function(key) {
|
|
|
|
HTMLCanvasElement.prototype[key] = function() {
|
|
|
|
var canvas = idlUtils.implForWrapper(this)._canvas;
|
|
|
|
return canvas[key].apply(canvas, arguments);
|
|
|
|
};
|
|
|
|
});
|
2016-01-30 07:48:16 -05:00
|
|
|
};
|