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

Commit 2e63f41

Browse files
committed
move main script from index.js to lib/processing.js
1 parent f1927e3 commit 2e63f41

File tree

2 files changed

+99
-66
lines changed

2 files changed

+99
-66
lines changed

index.js

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,2 @@
11

2-
/**
3-
* Module dependencies.
4-
*/
5-
6-
var fs = require('fs')
7-
, util = require('util')
8-
, jsdom = require('jsdom')
9-
, Canvas = require('canvas')
10-
, Image = Canvas.Image
11-
, document = jsdom.jsdom('<!doctype html><html><head></head><body></body></html>')
12-
, window = document.createWindow()
13-
, navigator = window.navigator
14-
, HTMLImageElement = window.HTMLImageElement
15-
, createElement = document.createElement
16-
, noop = function() {}
17-
, src = fs.readFileSync('./deps/processing-js/processing.js');
18-
19-
/**
20-
* Dummy function for hack.
21-
*/
22-
23-
function HTMLCanvasElement() {}
24-
25-
/**
26-
* Make `Canvas` instace of `HTMLCanvasElement`
27-
*/
28-
29-
Canvas.prototype.__proto__ = HTMLCanvasElement.prototype;
30-
31-
/**
32-
* Creating Processing instance.
33-
*/
34-
35-
eval(src.toString('utf-8'));
36-
37-
/**
38-
* Save referece for `window` and `document`.
39-
*/
40-
41-
Processing.window = window;
42-
Processing.document = document;
43-
44-
/**
45-
* Integrate with node-canvas.
46-
*/
47-
48-
Processing.createElement = document.createElement = function() {
49-
if ('canvas' === arguments[0]) {
50-
var canvas = new Canvas();
51-
52-
canvas.style = { setProperty: noop };
53-
canvas.attachEvent = noop;
54-
canvas.setAttribute = noop;
55-
canvas.getAttribute = noop;
56-
canvas.hasAttribute = noop;
57-
58-
return canvas;
59-
}
60-
return createElement.apply(this, arguments);
61-
};
62-
63-
/**
64-
* Expose `Processing`.
65-
*/
66-
67-
module.exports = Processing;
2+
module.exports = require('./lib/processing');

lib/processing.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
2+
/**
3+
* Module dependencies.
4+
*/
5+
6+
var fs = require('fs')
7+
, util = require('util')
8+
, jsdom = require('jsdom')
9+
, pkg = require('../package')
10+
, patch = require('./patch')
11+
, XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest
12+
, Canvas = require('./canvas')
13+
, Image = Canvas.Image
14+
, document = jsdom.jsdom('<!doctype html><html><head></head><body></body></html>')
15+
, window = document.createWindow()
16+
, navigator = window.navigator
17+
, HTMLImageElement = window.HTMLImageElement
18+
, createElement = document.createElement
19+
, noop = function() {}
20+
, processing = fs.readFileSync('./deps/processing-js/processing.js');
21+
22+
/**
23+
* Expose `version`.
24+
*/
25+
26+
exports.version = pkg.version;
27+
28+
/**
29+
* Expose `window`.
30+
*/
31+
32+
exports.window = window;
33+
34+
/**
35+
* Expose `document`.
36+
*/
37+
38+
exports.document = document;
39+
40+
/**
41+
* Make `Canvas` instance of `HTMLCanvasElement`.
42+
*/
43+
44+
function HTMLCanvasElement() {}
45+
Canvas.prototype.__proto__ = HTMLCanvasElement.prototype;
46+
47+
/**
48+
* Evaluating Processing source code.
49+
*
50+
* FIXME: `Processing` leaks to global object.
51+
*/
52+
53+
eval(processing.toString('utf-8'));
54+
55+
/**
56+
* Expose `Processing`.
57+
*/
58+
59+
exports.Processing = Processing;
60+
61+
/**
62+
* Return processing instance.
63+
*
64+
* @param {Canvas} canvas
65+
* @param {String} path
66+
* @return {Processing}
67+
*/
68+
69+
exports.createInstance = function(canvas, path) {
70+
var src;
71+
72+
if (1 == arguments.length) {
73+
path = canvas;
74+
canvas = document.createElement('canvas');
75+
} else if ('string' === typeof canvas) {
76+
src = canvas;
77+
canvas = document.createElement('canvas');
78+
}
79+
80+
if (!src) {
81+
src = fs.readFileSync(path).toString();
82+
}
83+
84+
return patch(new Processing(canvas, src), path);
85+
};
86+
87+
/**
88+
* Integrate with node-canvas.
89+
*/
90+
91+
document.createElement = function() {
92+
if ('canvas' === arguments[0]) {
93+
return new Canvas();
94+
} else if ('img' === arguments[0]) {
95+
return new Image();
96+
}
97+
return createElement.apply(this, arguments);
98+
};

0 commit comments

Comments
 (0)