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
|
function EventListener() {
this.listeners = {};
this.addEventListener = function(event, func) {
if (!this.listeners[event]) this.listeners[event] = [];
this.listeners[event].push(func);
};
};
var window = new EventListener();
window.close = function() {
postMessage({ target: 'window', method: 'close' });
};
var document = new EventListener();
document.createElement = function(what) {
switch(what) {
case 'canvas': {
var canvas = new EventListener();
canvas.ensureData = function() {
if (!canvas.data || canvas.data.width !== canvas.width || canvas.data.height !== canvas.height) {
canvas.data = {
width: canvas.width,
height: canvas.height,
data: new Uint8Array(canvas.width*canvas.height*4)
};
postMessage({ target: 'canvas', op: 'resize', width: canvas.width, height: canvas.height });
}
};
canvas.getContext = function(type) {
assert(type == '2d');
return {
getImageData: function(x, y, w, h) {
assert(x == 0 && y == 0 && w == canvas.width && h == canvas.height);
canvas.ensureData();
return {
width: canvas.data.width,
height: canvas.data.height,
data: new Uint8Array(canvas.data.data) // TODO: can we avoid this copy?
};
},
putImageData: function(image, x, y) {
canvas.ensureData();
assert(x == 0 && y == 0 && image.width == canvas.width && image.height == canvas.height);
canvas.data.data.set(image.data); // TODO: can we avoid this copy?
postMessage({ target: 'canvas', op: 'render', image: canvas.data });
}
};
};
return canvas;
}
default: throw 'document.createElement ' + what;
}
};
Module.canvas = document.createElement('canvas');
|