This commit is contained in:
bach
2025-02-07 16:38:56 +01:00
parent 803e4407f4
commit b1c9f6170b
186 changed files with 178519 additions and 0 deletions
+66
View File
@@ -0,0 +1,66 @@
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* http://paperjs.org/
*
* Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey
* http://juerglehni.com/ & https://puckey.studio/
*
* 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
// - Various Node-Canvas methods, routed through from HTMLCanvasElement:
// toBuffer, pngStream, createPNGStream, jpegStream, createJPEGStream
module.exports = function(self, requireName) {
var Canvas;
try {
Canvas = require('canvas').Canvas;
} catch(error) {
// 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.
delete self.window;
// 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.');
}
return;
}
var HTMLCanvasElement = self.HTMLCanvasElement,
idlUtils = require('jsdom/lib/jsdom/living/generated/utils');
// Add fake HTMLCanvasElement#type property:
Object.defineProperty(HTMLCanvasElement.prototype, 'type', {
get: function() {
var canvas = idlUtils.implForWrapper(this)._canvas;
return canvas && canvas.type || 'image';
},
set: function(type) {
// Allow replacement of internal node-canvas, so we can switch to a
// PDF canvas.
var impl = idlUtils.implForWrapper(this),
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:
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);
};
});
};
+156
View File
@@ -0,0 +1,156 @@
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* http://paperjs.org/
*
* Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey
* http://juerglehni.com/ & https://puckey.studio/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
var fs = require('fs'),
path = require('path');
module.exports = function(paper) {
if (paper.PaperScript) {
var sourceMapSupport = 'require("source-map-support").install(paper.PaperScript.sourceMapSupport);\n',
sourceMaps = {};
paper.PaperScript.sourceMapSupport = {
retrieveSourceMap: function(source) {
var map = sourceMaps[source];
return map ? { url: source, map: map } : null;
}
};
// Register the .pjs extension for automatic compilation as PaperScript
require.extensions['.pjs'] = function(module, filename) {
// Requiring a PaperScript on Node.js returns an initialize method which
// needs to receive a Canvas object when called and returns the
// PaperScope.
module.exports = function(canvas) {
var source = fs.readFileSync(filename, 'utf8'),
code = sourceMapSupport + source,
compiled = paper.PaperScript.compile(code, {
url: filename,
source: source,
sourceMaps: true,
offset: -1 // remove sourceMapSupport...
}),
scope = new paper.PaperScope();
// Keep track of sourceMaps so retrieveSourceMap() can link them up
scope.setup(canvas);
scope.__filename = filename;
scope.__dirname = path.dirname(filename);
// Expose core methods and values
scope.require = require;
scope.console = console;
sourceMaps[filename] = compiled.map;
paper.PaperScript.execute(compiled, scope);
return scope;
};
};
}
paper.PaperScope.inject({
createCanvas: function(width, height, type) {
// Do not use CanvasProvider.getCanvas(), since we may be changing
// the underlying node-canvas when requesting PDF support, and don't
// want to release it after back into the pool.
var canvas = paper.document.createElement('canvas');
canvas.width = width;
canvas.height = height;
canvas.type = type;
return canvas;
},
/**
* @deprecated, use use {@link #createCanvas(width, height)} instead.
*/
Canvas: '#createCanvas'
});
// Override requestAnimationFrame() to avoid setInterval() timers.
// NOTE: In Node.js, we only support manual updating for now, but
// View#exportFrames() below offers a way to emulate animations by exporting
// them frame by frame at the given frame-rate.
paper.DomEvent.requestAnimationFrame = function(callback) {
};
// Node.js based image exporting code.
paper.CanvasView.inject({
// DOCS: CanvasView#exportFrames(options);
exportFrames: function(options) {
options = paper.Base.set({
fps: 30,
prefix: 'frame-',
amount: 1,
format: 'png' // Supported: 'png' or 'jpeg'
}, options);
if (!options.directory)
throw new Error('Missing options.directory');
if (options.format && !/^(jpeg|png)$/.test(options.format))
throw new Error('Unsupported format. Use "png" or "jpeg"');
var view = this,
count = 0,
frameDuration = 1 / options.fps,
startTime = Date.now(),
lastTime = startTime,
padding = options.padding || ((options.amount - 1) + '').length,
paddedStr = Array(padding + 1).join('0');
// Start exporting frames by exporting the first frame:
exportFrame(options);
function exportFrame() {
// Convert to a Base object, for #toString()
view.emit('frame', new paper.Base({
delta: frameDuration,
time: frameDuration * count,
count: count
}));
var file = path.join(options.directory,
options.prefix + (paddedStr + count).slice(-padding)
+ '.' + options.format);
var out = view.exportImage(file, function() {
// Once the file has been closed, export the next fame:
var then = Date.now();
if (options.onProgress) {
options.onProgress({
count: count,
amount: options.amount,
percentage: Math.round((count + 1) / options.amount
* 10000) / 100,
time: then - startTime,
delta: then - lastTime
});
}
lastTime = then;
if (++count < options.amount) {
exportFrame();
} else {
// Call onComplete handler when finished:
if (options.onComplete) {
options.onComplete();
}
}
});
}
},
// DOCS: CanvasView#exportImage(path, callback);
exportImage: function(path, callback) {
this.update();
var out = fs.createWriteStream(path),
format = /\.jp(e?)g$/.test(path) ? 'jpeg' : 'png',
stream = this._element[format + 'Stream']();
stream.pipe(out);
if (callback) {
out.on('close', callback);
}
return out;
}
});
};
+58
View File
@@ -0,0 +1,58 @@
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* http://paperjs.org/
*
* Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey
* http://juerglehni.com/ & https://puckey.studio/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
// Node.js emulation layer of browser environment, based on jsdom with node-
// canvas integration.
var path = require('path');
// Determine the name by which name the module was required (either 'paper',
// 'paper-jsdom' or 'paper-jsdom-canvas'), and use this to determine if error
// exceptions should be thrown or if loading should fail silently.
var parent = module.parent && module.parent.parent,
requireName = parent && path.basename(path.dirname(parent.filename));
requireName = /^paper/.test(requireName) ? requireName : 'paper';
var jsdom,
self;
try {
jsdom = require('jsdom');
} catch(e) {
// Check the required module's name to see if it contains jsdom, and only
// complain about its lack if the module requires it.
if (/\bjsdom\b/.test(requireName)) {
throw new Error('Unable to load jsdom module.');
}
}
if (jsdom) {
// Create our document and window objects through jsdom.
/* global document:true, window:true */
var document = new jsdom.JSDOM('<html><body></body></html>', {
// Use the current working directory as the document's origin, so
// requests to local files work correctly with CORS.
url: 'file://' + process.cwd() + '/',
resources: 'usable'
});
self = document.window;
require('./canvas.js')(self, requireName);
require('./xml.js')(self);
} else {
self = {
navigator: {
userAgent: 'Node.js (' + process.platform + '; U; rv:' +
process.version + ')'
}
};
}
module.exports = self;
+40
View File
@@ -0,0 +1,40 @@
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* http://paperjs.org/
*
* Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey
* http://juerglehni.com/ & https://puckey.studio/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
module.exports = function(self) {
// Define XMLSerializer shim, to emulate browser behavior.
// Effort to bring XMLSerializer to jsdom:
// https://github.com/tmpvar/jsdom/issues/1368
self.XMLSerializer = function XMLSerializer() {
};
self.XMLSerializer.prototype = {
serializeToString: function(node) {
if (!node)
return '';
// Fix a jsdom issue where all SVG tagNames are lowercased:
// https://github.com/tmpvar/jsdom/issues/620
var text = node.outerHTML,
tagNames = ['linearGradient', 'radialGradient', 'clipPath',
'textPath'];
for (var i = 0, l = tagNames.length; i < l; i++) {
var tagName = tagNames[i];
text = text.replace(
new RegExp('(<|</)' + tagName.toLowerCase() + '\\b', 'g'),
function(match, start) {
return start + tagName;
});
}
return text;
}
};
};
+1
View File
@@ -0,0 +1 @@
import './paper';
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff