-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvaswrapper.js
More file actions
100 lines (91 loc) · 2.29 KB
/
canvaswrapper.js
File metadata and controls
100 lines (91 loc) · 2.29 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
// We can't trust canvas to do bit-exact alpha values, since it has to translate
// int -> float -> int.
class CanvasWrapper {
constructor(data) {
this.data = data;
this.view = new Uint32Array(this.data.data.buffer);
}
getAddr32(i, j) {
return i + this.data.width * j
}
fillRectWith(color, op, x, y, w, h) {
assert(_.isNumber(color));
assert(_.isString(op));
if (x < 0) {
x = 0;
}
if (x >= this.data.width) {
x = this.data.width - 1;
}
if (y < 0) {
y = 0;
}
if (y >= this.data.height) {
y = this.data.height - 1;
}
if (x + w >= this.data.width) {
w = this.data.width - x;
}
if (y + h >= this.data.height) {
h = this.data.height - y;
}
for (let i = 0; i < w; ++i) {
for (let j = 0; j < h; ++j) {
if (op === 'or') {
this.view[this.getAddr32(i + x, j + y)] |= color;
} else {
assert(op === 'set');
this.view[this.getAddr32(i + x, j + y)] = color;
}
}
}
}
orRect(color, x, y, w, h) {
return this.fillRectWith(color, 'or', x, y, w, h);
}
fillRect(color, x, y, w, h) {
return this.fillRectWith(color, 'set', x, y, w, h);
}
strokeRect(color, x, y, w, h) {
if (x < 0) {
x = 0;
}
if (x >= this.data.width) {
x = this.data.width - 1;
}
if (y < 0) {
y = 0;
}
if (y >= this.data.height) {
y = this.data.height - 1;
}
if (x + w >= this.data.width) {
w = this.data.width - x;
}
if (y + h >= this.data.height) {
h = this.data.height - y;
}
--w; --h;
for (let i = 0; i < w; ++i) {
this.view[this.getAddr32(i + x, y)] = color;
this.view[this.getAddr32(i + x, y + h)] = color;
}
for (let j = 0; j <= h; ++j) {
this.view[this.getAddr32(x, j + y)] = color;
this.view[this.getAddr32(x + w, j + y)] = color;
}
}
fillBitmap(x, y, message, key) {
assert(x >= 0);
assert(y >= 0);
for (let j = 0; j < message.length; ++j) {
assert(y + j < this.data.height);
let line = message[j];
for (let i = 0; i < line.length; ++i) {
assert(x + i < this.data.width);
assert(message[j][i] in key)
this.view[this.getAddr32(i + x, j + y)] = key[message[j][i]];
}
}
}
}