Allow loading of Paper.js on Node.js without the need for compiling.

This commit is contained in:
Jonathan Puckey 2011-08-10 21:02:32 +02:00
parent ebc3b3789b
commit 8ce7ed578e
2 changed files with 34 additions and 9 deletions

31
src/loadNode.js Normal file
View file

@ -0,0 +1,31 @@
var context = require('vm').createContext({
options: {
server: true,
version: 'dev'
},
Canvas: require('canvas'),
console: console,
require: require,
include: function(uri) {
var source = require('fs').readFileSync(__dirname + '/' + uri);
// For relative includes, we save the current directory and then add
// the uri directory to __dirname:
var oldDirname = __dirname;
__dirname = __dirname + '/' + uri.replace(/[^/]+$/, '');
require('vm').runInContext(source, context, uri);
__dirname = oldDirname;
}
});
context.include('paper.js');
context.Base.each(context, function(val, key) {
if (val && val.prototype instanceof context.Base) {
val._name = key;
// Export all classes through PaperScope:
context.PaperScope.prototype[key] = val;
}
});
context.PaperScope.prototype['Canvas'] = context.Canvas;
module.exports = new context.PaperScope();

View file

@ -107,10 +107,6 @@ var paper = new function() {
/*#*/ include('browser/DomEvent.js');
/*#*/ } // options.browser
/*#*/ if (options.server) {
var Canvas = this.Canvas = require('canvas');
/*#*/ } // options.server
/*#*/ include('ui/View.js');
/*#*/ if (options.browser) {
@ -128,7 +124,9 @@ var paper = new function() {
/*#*/ include('core/PaperScript.js');
/*#*/ if (options.browser) {
/*#*/ include('core/initialize.js');
/*#*/ } // options.browser
/*#*/ if (options.version != 'dev') {
// Finally inject the classes set on 'this' into the PaperScope class and create
@ -140,8 +138,4 @@ var paper = new function() {
this.enumerable = true;
return new (PaperScope.inject(this));
/*#*/ } // options.version != 'dev'
};
/*#*/ if (options.server) {
module.exports = paper;
/*#*/ } // options.server
};