mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-08 05:42:07 -05:00
Fix issues with running paper.js in node.
We need to export the Object and Array definition into new context, to make Base.isPlainObject() work. See http://nodejs.org/api/vm.html#vm_globals
This commit is contained in:
parent
219ccd2fdd
commit
8dbe1f4927
2 changed files with 36 additions and 29 deletions
|
@ -13,10 +13,17 @@
|
||||||
var fs = require('fs'),
|
var fs = require('fs'),
|
||||||
vm = require('vm'),
|
vm = require('vm'),
|
||||||
path = require('path'),
|
path = require('path'),
|
||||||
|
// Node Canvas library: https://github.com/learnboost/node-canvas
|
||||||
Canvas = require('canvas'),
|
Canvas = require('canvas'),
|
||||||
jsdom = require('jsdom');
|
jsdom = require('jsdom'),
|
||||||
|
dirname = path.resolve(__dirname, '..');
|
||||||
|
|
||||||
__dirname = path.resolve(__dirname, '..');
|
var options = {
|
||||||
|
server: true,
|
||||||
|
svg: true,
|
||||||
|
parser: 'acorn',
|
||||||
|
version: 'dev'
|
||||||
|
};
|
||||||
|
|
||||||
// Create a window and document using jsdom, e.g. for exportSVG()
|
// Create a window and document using jsdom, e.g. for exportSVG()
|
||||||
var win = jsdom.createWindow(),
|
var win = jsdom.createWindow(),
|
||||||
|
@ -24,14 +31,12 @@ var win = jsdom.createWindow(),
|
||||||
|
|
||||||
// Create the context within which we will run the source files:
|
// Create the context within which we will run the source files:
|
||||||
var context = vm.createContext({
|
var context = vm.createContext({
|
||||||
options: {
|
options: options,
|
||||||
server: true,
|
|
||||||
svg: true,
|
|
||||||
parser: 'acorn',
|
|
||||||
version: 'dev'
|
|
||||||
},
|
|
||||||
fs: fs,
|
fs: fs,
|
||||||
// Node Canvas library: https://github.com/learnboost/node-canvas
|
// We need to export the local Object definition, so Base.isPlainObject()
|
||||||
|
// works. See http://nodejs.org/api/vm.html#vm_globals
|
||||||
|
Object: Object,
|
||||||
|
Array: Array,
|
||||||
Canvas: Canvas,
|
Canvas: Canvas,
|
||||||
HTMLCanvasElement: Canvas,
|
HTMLCanvasElement: Canvas,
|
||||||
Image: Canvas.Image,
|
Image: Canvas.Image,
|
||||||
|
@ -41,17 +46,16 @@ var context = vm.createContext({
|
||||||
navigator: win.navigator,
|
navigator: win.navigator,
|
||||||
console: console,
|
console: console,
|
||||||
require: require,
|
require: require,
|
||||||
__dirname: __dirname,
|
__dirname: dirname,
|
||||||
__filename: __filename,
|
|
||||||
// Used to load and run source files within the same context:
|
// Used to load and run source files within the same context:
|
||||||
include: function(uri) {
|
include: function(uri) {
|
||||||
var source = fs.readFileSync(path.resolve(__dirname, uri), 'utf8');
|
var source = fs.readFileSync(path.resolve(dirname, uri), 'utf8'),
|
||||||
// For relative includes, we save the current directory and then
|
// For relative includes, we save the current directory and then
|
||||||
// add the uri directory to __dirname:
|
// add the uri directory to dirname:
|
||||||
var oldDirname = __dirname;
|
prevDirname = dirname;
|
||||||
__dirname = path.resolve(__dirname, path.dirname(uri));
|
dirname = path.resolve(dirname, path.dirname(uri));
|
||||||
vm.runInContext(source, context, uri);
|
vm.runInContext(source, context, uri);
|
||||||
__dirname = oldDirname;
|
dirname = prevDirname;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -63,15 +67,18 @@ context.Base.each(context, function(val, key) {
|
||||||
if (val && val.prototype instanceof context.Base)
|
if (val && val.prototype instanceof context.Base)
|
||||||
context.PaperScope.prototype[key] = val;
|
context.PaperScope.prototype[key] = val;
|
||||||
});
|
});
|
||||||
context.PaperScope.prototype['Canvas'] = context.Canvas;
|
context.PaperScope.prototype.Canvas = Canvas;
|
||||||
|
|
||||||
require.extensions['.pjs'] = function(module, uri) {
|
require.extensions['.pjs'] = function(module, uri) {
|
||||||
var source = context.PaperScript.compile(fs.readFileSync(uri, 'utf8'));
|
var source = context.PaperScript.compile(fs.readFileSync(uri, 'utf8'));
|
||||||
var envVars = 'var __dirname = \'' + path.dirname(uri) + '\';' +
|
var prevDirname = context.__dirname,
|
||||||
'var __filename = \'' + uri + '\';';
|
prevFilename = context.__filename;
|
||||||
vm.runInContext(envVars, context);
|
context.__dirname = path.dirname(uri);
|
||||||
|
context.__filename = uri;
|
||||||
var scope = new context.PaperScope();
|
var scope = new context.PaperScope();
|
||||||
context.PaperScript.evaluate(source, scope);
|
context.PaperScript.evaluate(source, scope);
|
||||||
|
context.__dirname = prevDirname;
|
||||||
|
context.__filename = prevFilename;
|
||||||
module.exports = scope;
|
module.exports = scope;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -172,7 +172,6 @@ var CanvasView = View.extend(/** @lends CanvasView# */{
|
||||||
/*#*/ if (options.server) {
|
/*#*/ if (options.server) {
|
||||||
// Node.js server based image exporting code.
|
// Node.js server based image exporting code.
|
||||||
CanvasView.inject(new function() {
|
CanvasView.inject(new function() {
|
||||||
var path = require('path');
|
|
||||||
// Utility function that converts a number to a string with
|
// Utility function that converts a number to a string with
|
||||||
// x amount of padded 0 digits:
|
// x amount of padded 0 digits:
|
||||||
function toPaddedString(number, length) {
|
function toPaddedString(number, length) {
|
||||||
|
@ -196,7 +195,8 @@ CanvasView.inject(new function() {
|
||||||
var view = this,
|
var view = this,
|
||||||
count = 0,
|
count = 0,
|
||||||
frameDuration = 1 / param.fps,
|
frameDuration = 1 / param.fps,
|
||||||
lastTime = startTime = Date.now();
|
startTime = Date.now(),
|
||||||
|
lastTime = startTime;
|
||||||
|
|
||||||
// Start exporting frames by exporting the first frame:
|
// Start exporting frames by exporting the first frame:
|
||||||
exportFrame(param);
|
exportFrame(param);
|
||||||
|
@ -204,8 +204,8 @@ CanvasView.inject(new function() {
|
||||||
function exportFrame(param) {
|
function exportFrame(param) {
|
||||||
count++;
|
count++;
|
||||||
var filename = param.prefix + toPaddedString(count, 6) + '.png',
|
var filename = param.prefix + toPaddedString(count, 6) + '.png',
|
||||||
uri = param.directory + '/' + filename;
|
path = param.directory + '/' + filename;
|
||||||
var out = view.exportImage(uri, function() {
|
var out = view.exportImage(path, function() {
|
||||||
// When the file has been closed, export the next fame:
|
// When the file has been closed, export the next fame:
|
||||||
var then = Date.now();
|
var then = Date.now();
|
||||||
if (param.onProgress) {
|
if (param.onProgress) {
|
||||||
|
@ -237,11 +237,11 @@ CanvasView.inject(new function() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
// DOCS: View#exportImage(uri, callback);
|
|
||||||
exportImage: function(uri, callback) {
|
// DOCS: View#exportImage(path, callback);
|
||||||
|
exportImage: function(path, callback) {
|
||||||
this.draw();
|
this.draw();
|
||||||
// TODO: is it necessary to resolve the path?
|
var out = fs.createWriteStream(path),
|
||||||
var out = fs.createWriteStream(path.resolve(__dirname, uri)),
|
|
||||||
stream = this._element.createPNGStream();
|
stream = this._element.createPNGStream();
|
||||||
// Pipe the png stream to the write stream:
|
// Pipe the png stream to the write stream:
|
||||||
stream.pipe(out);
|
stream.pipe(out);
|
||||||
|
|
Loading…
Reference in a new issue