From 2e63f41ca87a054ff736ff2895ce6531199fea47 Mon Sep 17 00:00:00 2001 From: nulltask Date: Tue, 19 Jun 2012 02:34:13 +0900 Subject: [PATCH] move main script from index.js to lib/processing.js --- index.js | 67 +------------------------------- lib/processing.js | 98 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 66 deletions(-) create mode 100644 lib/processing.js diff --git a/index.js b/index.js index 083cfa5..f356154 100644 --- a/index.js +++ b/index.js @@ -1,67 +1,2 @@ -/** - * Module dependencies. - */ - -var fs = require('fs') - , util = require('util') - , jsdom = require('jsdom') - , Canvas = require('canvas') - , Image = Canvas.Image - , document = jsdom.jsdom('') - , window = document.createWindow() - , navigator = window.navigator - , HTMLImageElement = window.HTMLImageElement - , createElement = document.createElement - , noop = function() {} - , src = fs.readFileSync('./deps/processing-js/processing.js'); - -/** - * Dummy function for hack. - */ - -function HTMLCanvasElement() {} - -/** - * Make `Canvas` instace of `HTMLCanvasElement` - */ - -Canvas.prototype.__proto__ = HTMLCanvasElement.prototype; - -/** - * Creating Processing instance. - */ - -eval(src.toString('utf-8')); - -/** - * Save referece for `window` and `document`. - */ - -Processing.window = window; -Processing.document = document; - -/** - * Integrate with node-canvas. - */ - -Processing.createElement = document.createElement = function() { - if ('canvas' === arguments[0]) { - var canvas = new Canvas(); - - canvas.style = { setProperty: noop }; - canvas.attachEvent = noop; - canvas.setAttribute = noop; - canvas.getAttribute = noop; - canvas.hasAttribute = noop; - - return canvas; - } - return createElement.apply(this, arguments); -}; - -/** - * Expose `Processing`. - */ - -module.exports = Processing; +module.exports = require('./lib/processing'); diff --git a/lib/processing.js b/lib/processing.js new file mode 100644 index 0000000..f9a0351 --- /dev/null +++ b/lib/processing.js @@ -0,0 +1,98 @@ + +/** + * Module dependencies. + */ + +var fs = require('fs') + , util = require('util') + , jsdom = require('jsdom') + , pkg = require('../package') + , patch = require('./patch') + , XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest + , Canvas = require('./canvas') + , Image = Canvas.Image + , document = jsdom.jsdom('') + , window = document.createWindow() + , navigator = window.navigator + , HTMLImageElement = window.HTMLImageElement + , createElement = document.createElement + , noop = function() {} + , processing = fs.readFileSync('./deps/processing-js/processing.js'); + +/** + * Expose `version`. + */ + +exports.version = pkg.version; + +/** + * Expose `window`. + */ + +exports.window = window; + +/** + * Expose `document`. + */ + +exports.document = document; + +/** + * Make `Canvas` instance of `HTMLCanvasElement`. + */ + +function HTMLCanvasElement() {} +Canvas.prototype.__proto__ = HTMLCanvasElement.prototype; + +/** + * Evaluating Processing source code. + * + * FIXME: `Processing` leaks to global object. + */ + +eval(processing.toString('utf-8')); + +/** + * Expose `Processing`. + */ + +exports.Processing = Processing; + +/** + * Return processing instance. + * + * @param {Canvas} canvas + * @param {String} path + * @return {Processing} + */ + +exports.createInstance = function(canvas, path) { + var src; + + if (1 == arguments.length) { + path = canvas; + canvas = document.createElement('canvas'); + } else if ('string' === typeof canvas) { + src = canvas; + canvas = document.createElement('canvas'); + } + + if (!src) { + src = fs.readFileSync(path).toString(); + } + + return patch(new Processing(canvas, src), path); +}; + +/** + * Integrate with node-canvas. + */ + +document.createElement = function() { + if ('canvas' === arguments[0]) { + return new Canvas(); + } else if ('img' === arguments[0]) { + return new Image(); + } + return createElement.apply(this, arguments); +};