Skip to content
This repository has been archived by the owner on Mar 12, 2018. It is now read-only.

Commit

Permalink
move main script from index.js to lib/processing.js
Browse files Browse the repository at this point in the history
  • Loading branch information
nulltask committed Jun 18, 2012
1 parent f1927e3 commit 2e63f41
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 66 deletions.
67 changes: 1 addition & 66 deletions 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('<!doctype html><html><head></head><body></body></html>')
, 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');
98 changes: 98 additions & 0 deletions 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('<!doctype html><html><head></head><body></body></html>')
, 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);
};

0 comments on commit 2e63f41

Please sign in to comment.