forked from processing-js/processing-js.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessing-helper.js
More file actions
99 lines (85 loc) · 3.04 KB
/
Copy pathprocessing-helper.js
File metadata and controls
99 lines (85 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
(function(global) {
var canvas = document.getElementById('sketch'),
code = document.getElementById('code'),
output = document.getElementById('output'),
instance = null;
function createCanvas() {
// Make a new canvas, in case we're switching from 2D to 3D contexts.
var container = document.getElementById('sketch-container');
var sketch = document.getElementById('sketch');
container.removeChild(sketch);
sketch = document.createElement('canvas');
sketch.id = 'sketch';
container.appendChild(sketch);
return sketch;
}
function waitForExit() {
var checkbox = document.getElementById('expect-exit-callback');
if (!checkbox) {
return false;
}
return checkbox.checked || checkbox.value;
}
global.runSketch = function(callback) {
try {
output.value = '';
canvas = createCanvas();
var sketch = Processing.compile(code.value);
if (callback) {
if (!/exit\(\);/.test(code.value)) {
throw "exit() not found in sketch. Add the exit() command, and re-run the sketch.";
}
sketch.onExit = callback;
instance = new Processing(canvas, sketch);
} else {
instance = new Processing(canvas, sketch);
}
} catch (e) {
output.value = "Error! Error was:\n" + e.toString();
}
};
global.convertToJS = function() {
try {
output.value = js_beautify(
Processing.compile(code.value).sourceCode).replace(/\n\n\n+/g, '\n\n');
output.select();
} catch (e) {
output.value = "Parser Error! Error was:\n" + e.toString();
}
};
global.generateDataURI = function() {
// Run the sketch first, in case the user hasn't
runSketch();
output.value = canvas.toDataURL();
output.select();
};
function buildRefTest() {
try {
// if the test was 2d, we can just call getImageData
if (!instance.use3DContext) {
var context = canvas.getContext('2d');
var imgData = context.getImageData(0, 0, canvas.width, canvas.height).data;
// else, we'll need to call WebGL's readPixels.
} else {
// The order of the pixels go from bottom to top, left to right.
var context = canvas.getContext("experimental-webgl");
var imgData = new Uint8Array(canvas.width * canvas.height * 4);
context.readPixels(0, 0, canvas.width, canvas.height, context.RGBA, context.UNSIGNED_BYTE, imgData);
}
var pixels = [];
for(var i = 0, idl = imgData.length; i < idl; i++) {
pixels[i] = imgData[i];
}
var dimensions = "[" + canvas.width + "," + canvas.height + "]";
// Opera doesn't have btoa() so this won't work there.
document.location.href= "data:text/plain;charset=utf-8;base64," +
btoa('//' + dimensions + pixels + '\n' + code.value);
} catch (e) {
output.value = "Error creating ref test! Error was: " + e.toString();
}
};
global.generateRefTest = function() {
// Run the sketch first, in case the user hasn't
runSketch(buildRefTest);
};
}(window));