-
Notifications
You must be signed in to change notification settings - Fork 6
/
SoftwareCanvas.js
134 lines (111 loc) · 3.73 KB
/
SoftwareCanvas.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class SoftwareCanvas {
constructor() {
this.style = {};
}
set width(w) {
this._width = w;
this._resize()
}
set height(h) {
this._height = h;
this._resize()
}
_resize() {
this._data = new Uint8ClampedArray(this._width * this._height * 4);
}
getContext() {
return this;
// Use proxy to trap unhandled messages for development purposes
const proxy = new Proxy(this, {
get: function (object, property, proxy) {
if (property in object) {
return object[property];
}
console.error('Trapped get', property);
return null;
},
set: function(object, property, value, proxy) {
if (!(property in object)) {
console.error('Trapped set', property, value);
}
object[property] = value;
return object[property];
}
});
return proxy;
}
fillRect(sx, sy, w, h) {
const [r, g, b, a] = this._parseStyle(this.fillStyle);
const a1 = a / 255;
for (let j = 0; j < h; j++) {
for (let i = 0; i < w; i++) {
const x = i + sx;
const y = j + sy;
const si = (y * this._width + x) * 4;
// TODO Need to blend!!!
this._data[si + 0] = r * a1 | 0;
this._data[si + 1] = g * a1 | 0;
this._data[si + 2] = b * a1 | 0;
this._data[si + 3] = a;
}
}
}
_parseStyle(s) {
const RGB = /rgb\s*\(\s*(\d+)[ ,]+(\d+)[ ,]+(\d+)\s*\)/
let match = RGB.exec(s);
if (match) {
return [...match.slice(1), 255];
}
const RGBA = /rgba\s*\(\s*(\d+)[ ,]+(\d+)[ ,]+(\d+)[ ,]+(\d+)\s*\)/
match = RGBA.exec(s);
if (match)
return match.slice(1);
elsef
console.error('Cannot parse', s);
return [0, 0, 0, 0];
}
/*
set fillStyle(x) {
}*/
getImageData(sx, sy, sw, sh) {
const data = new Uint8ClampedArray(sw * sh * 4);
// TODO this doesn't check boundary conditions!
for (let j = 0; j < sh; j++) {
for (let i = 0; i < sw; i++) {
const x = sx + i;
const y = sy + j;
const source = (y * this._width + x) * 4;
const dest = (j * sw + i) * 4;
data[dest + 0] = this._data[source + 0];
data[dest + 1] = this._data[source + 1];
data[dest + 2] = this._data[source + 2];
data[dest + 3] = this._data[source + 3];
}
}
const result = {
data: data,
width: sw,
height: sh
}
return result;
}
putImageData(imageData, dx, dy, dirtyX, dirtyY, dirtyWidth, dirtyHeight) {
const data = imageData.data;
for (let y = 0; y < dirtyHeight; y++) {
for (let x = 0; x < dirtyWidth; x++) {
const tx = dirtyX + x;
const ty = dirtyY + y;
const source = (ty * imageData.width + tx) * 4;
const sx = dx + x + dirtyX;
const sy = dy + y + dirtyY;
const dest = (sy * this._width + sx) * 4;
const a = this._data[source + 3] / 255;
this._data[dest + 0] = data[source + 0] * a | 0;
this._data[dest + 1] = data[source + 1] * a | 0;
this._data[dest + 2] = data[source + 2] * a | 0;
this._data[dest + 3] *= data[source + 3] | 0;
}
}
}
}
module.exports = SoftwareCanvas;