diff --git a/src/loadNode.js b/src/loadNode.js new file mode 100644 index 00000000..b4a62513 --- /dev/null +++ b/src/loadNode.js @@ -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(); \ No newline at end of file diff --git a/src/paper.js b/src/paper.js index d2f817ca..aa4669ba 100644 --- a/src/paper.js +++ b/src/paper.js @@ -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 +}; \ No newline at end of file