This repository has been archived by the owner on Mar 12, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move main script from index.js to lib/processing.js
- Loading branch information
Showing
2 changed files
with
99 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |