mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-06 04:42:15 -05:00
Allow loading of Paper.js on Node.js without the need for compiling.
This commit is contained in:
parent
ebc3b3789b
commit
8ce7ed578e
2 changed files with 34 additions and 9 deletions
31
src/loadNode.js
Normal file
31
src/loadNode.js
Normal 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();
|
10
src/paper.js
10
src/paper.js
|
@ -107,10 +107,6 @@ var paper = new function() {
|
||||||
/*#*/ include('browser/DomEvent.js');
|
/*#*/ include('browser/DomEvent.js');
|
||||||
/*#*/ } // options.browser
|
/*#*/ } // options.browser
|
||||||
|
|
||||||
/*#*/ if (options.server) {
|
|
||||||
var Canvas = this.Canvas = require('canvas');
|
|
||||||
/*#*/ } // options.server
|
|
||||||
|
|
||||||
/*#*/ include('ui/View.js');
|
/*#*/ include('ui/View.js');
|
||||||
|
|
||||||
/*#*/ if (options.browser) {
|
/*#*/ if (options.browser) {
|
||||||
|
@ -128,7 +124,9 @@ var paper = new function() {
|
||||||
|
|
||||||
/*#*/ include('core/PaperScript.js');
|
/*#*/ include('core/PaperScript.js');
|
||||||
|
|
||||||
|
/*#*/ if (options.browser) {
|
||||||
/*#*/ include('core/initialize.js');
|
/*#*/ include('core/initialize.js');
|
||||||
|
/*#*/ } // options.browser
|
||||||
|
|
||||||
/*#*/ if (options.version != 'dev') {
|
/*#*/ if (options.version != 'dev') {
|
||||||
// Finally inject the classes set on 'this' into the PaperScope class and create
|
// Finally inject the classes set on 'this' into the PaperScope class and create
|
||||||
|
@ -141,7 +139,3 @@ this.enumerable = true;
|
||||||
return new (PaperScope.inject(this));
|
return new (PaperScope.inject(this));
|
||||||
/*#*/ } // options.version != 'dev'
|
/*#*/ } // options.version != 'dev'
|
||||||
};
|
};
|
||||||
|
|
||||||
/*#*/ if (options.server) {
|
|
||||||
module.exports = paper;
|
|
||||||
/*#*/ } // options.server
|
|
||||||
|
|
Loading…
Reference in a new issue