forked from GameJs/gamejs
/
mask.js
245 lines (224 loc) · 5.82 KB
/
mask.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
var gamejs = require('../gamejs');
var objects = require('./utils/objects');
/**
* @fileoverview Image masks. Usefull for pixel perfect collision detection.
*/
/**
* Creates an image mask from the given Surface. The alpha of each pixel is checked
* to see if it is greater than the given threshold. If it is greater then
* that pixel is set as non-colliding.
*
* @param {gamejs.Surface} surface
* @param {Number} threshold 0 to 255. defaults to: 255, fully transparent
*/
exports.fromSurface = function(surface, threshold) {
threshold = threshold && (255 - threshold) || 255;
var imgData = surface.getImageData().data;
var dims = surface.getSize();
var mask = new Mask(dims);
for (var i=0;i<imgData.length;i += 4) {
// y: pixel # / width
var y = parseInt((i / 4) / dims[0], 10);
// x: pixel # % width
var x = parseInt((i / 4) % dims[0], 10);
var alpha = imgData[i+3];
if (alpha >= threshold) {
mask.setAt(x, y);
}
}
return mask;
};
/**
* Image Mask
* @param {Array} dimensions [width, height]
*
*/
var Mask = exports.Mask = function(dims) {
/**
* @ignore
*/
this.width = dims[0];
/**
* @ignore
*/
this.height = dims[1];
/**
* @ignore
*/
this._bits = [];
for (var i=0;i<this.width;i++) {
this._bits[i] = [];
for (var j=0;j<this.height;j++) {
this._bits[i][j] = false;
}
}
return;
};
/**
* @param {gamejs.mask.Mask} otherMask
* @param {Array} offset [x,y]
* @returns the overlapping rectangle or null if there is no overlap;
*/
Mask.prototype.overlapRect = function(otherMask, offset) {
var arect = this.rect;
var brect = otherMask.rect;
if (offset) {
brect.moveIp(offset);
}
// bounding box intersect
if (!brect.collideRect(arect)) {
return null;
}
var xStart = Math.max(arect.left, brect.left);
var xEnd = Math.min(arect.right, brect.right);
var yStart = Math.max(arect.top, brect.top);
var yEnd = Math.min(arect.bottom, brect.bottom);
return new gamejs.Rect([xStart, yStart], [xEnd - xStart, yEnd - yStart]);
};
/**
*
* @returns True if the otherMask overlaps with this map.
* @param {Mask} otherMask
* @param {Array} offset
*/
Mask.prototype.overlap = function(otherMask, offset) {
var overlapRect = this.overlapRect(otherMask, offset);
if (overlapRect === null) {
return false;
}
var arect = this.rect;
var brect = otherMask.rect;
if (offset) {
brect.moveIp(offset);
}
var count = 0;
for (var y=overlapRect.top; y<=overlapRect.bottom; y++) {
for (var x=overlapRect.left; x<=overlapRect.right; x++) {
if (this.getAt(x - arect.left, y - arect.top) &&
otherMask.getAt(x - brect.left, y - brect.top)) {
return true;
}
}
}
// NOTE this should not happen because either we bailed out
// long ago because the rects do not overlap or there is an
// overlap and we should not have gotten this far.
// throw new Error("Maks.overlap: overlap detected but could not create mask for it.");
return false;
};
/**
* @param {gamejs.mask.Mask} otherMask
* @param {Array} offset [x,y]
* @returns the number of overlapping pixels
*/
Mask.prototype.overlapArea = function(otherMask, offset) {
var overlapRect = this.overlapRect(otherMask, offset);
if (overlapRect === null) {
return 0;
}
var arect = this.rect;
var brect = otherMask.rect;
if (offset) {
brect.moveIp(offset);
}
var count = 0;
for (var y=overlapRect.top; y<=overlapRect.bottom; y++) {
for (var x=overlapRect.left; x<=overlapRect.right; x++) {
if (this.getAt(x - arect.left, y - arect.top) &&
otherMask.getAt(x - brect.left, y - brect.top)) {
count++;
}
}
}
return count;
};
/**
* @param {gamejs.mask.Mask} otherMask
* @param {Array} offset [x,y]
* @returns a mask of the overlapping pixels
*/
Mask.prototype.overlapMask = function(otherMask, offset) {
var overlapRect = this.overlapRect(otherMask, offset);
if (overlapRect === null) {
return 0;
}
var arect = this.rect;
var brect = otherMask.rect;
if (offset) {
brect.moveIp(offset);
}
var mask = new Mask([overlapRect.width, overlapRect.height]);
for (var y=overlapRect.top; y<=overlapRect.bottom; y++) {
for (var x=overlapRect.left; x<=overlapRect.right; x++) {
if (this.getAt(x - arect.left, y - arect.top) &&
otherMask.getAt(x - brect.left, y - brect.top)) {
mask.setAt(x, y);
}
}
}
return mask;
};
/**
* Set bit at position.
* @param {Number} x
* @param {Number} y
*/
Mask.prototype.setAt = function(x, y) {
this._bits[x][y] = true;
};
/**
* Get bit at position.
*
* @param {Number} x
* @param {Number} y
*/
Mask.prototype.getAt = function(x, y) {
x = parseInt(x, 10);
y = parseInt(y, 10);
if (x < 0 || y < 0 || x >= this.width || y >= this.height) {
return false;
}
return this._bits[x][y];
};
/**
* Flip the bits in this map.
*/
Mask.prototype.invert = function() {
this._bits = this._bits.map(function(row) {
return row.map(function(b) {
return !b;
});
});
};
/**
* @returns {Array} the dimensions of the map
*/
Mask.prototype.getSize = function() {
return [this.width, this.height];
};
objects.accessors(Mask.prototype, {
/**
* Rect of this Mask.
*/
'rect': {
get: function() {
return new gamejs.Rect([0, 0], [this.width, this.height]);
}
},
/**
* @returns {Number} number of set pixels in this mask.
*/
'length': {
get: function() {
var c = 0;
this._bits.forEach(function(row) {
row.forEach(function(b) {
if (b) {
c++;
}
});
});
return c;
}
}
});