Skip to content

Commit 19ee8e8

Browse files
authored
Merge pull request scratchfoundation#415 from mzgoddard/drop-silhouette-alpha-buffer
Replace Silhouette._data with Silhouette._colorData
2 parents c9f86ef + e022222 commit 19ee8e8

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

src/Silhouette.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ let __SilhouetteUpdateCanvas;
1919
* @param {number} y - y
2020
* @return {number} Alpha value for x/y position
2121
*/
22-
const getPoint = ({_width: width, _height: height, _data: data}, x, y) => {
22+
const getPoint = ({_width: width, _height: height, _colorData: data}, x, y) => {
2323
// 0 if outside bouds, otherwise read from data.
2424
if (x >= width || y >= height || x < 0 || y < 0) {
2525
return 0;
2626
}
27-
return data[(y * width) + x];
27+
return data[(((y * width) + x) * 4) + 3];
2828
};
2929

3030
/**
@@ -76,7 +76,6 @@ class Silhouette {
7676
* The data representing a skin's silhouette shape.
7777
* @type {Uint8ClampedArray}
7878
*/
79-
this._data = null;
8079
this._colorData = null;
8180

8281
this.colorAtNearest = this.colorAtLinear = (_, dst) => dst.fill(0);
@@ -100,16 +99,11 @@ class Silhouette {
10099
ctx.drawImage(bitmapData, 0, 0, width, height);
101100
const imageData = ctx.getImageData(0, 0, width, height);
102101

103-
this._data = new Uint8ClampedArray(imageData.data.length / 4);
104102
this._colorData = imageData.data;
105103
// delete our custom overriden "uninitalized" color functions
106104
// let the prototype work for itself
107105
delete this.colorAtNearest;
108106
delete this.colorAtLinear;
109-
110-
for (let i = 0; i < imageData.data.length; i += 4) {
111-
this._data[i / 4] = imageData.data[i + 3];
112-
}
113107
}
114108

115109
/**
@@ -166,7 +160,7 @@ class Silhouette {
166160
* @return {boolean} If the nearest pixel has an alpha value.
167161
*/
168162
isTouchingNearest (vec) {
169-
if (!this._data) return;
163+
if (!this._colorData) return;
170164
return getPoint(
171165
this,
172166
Math.floor(vec[0] * (this._width - 1)),
@@ -181,7 +175,7 @@ class Silhouette {
181175
* @return {boolean} Any of the pixels have some alpha.
182176
*/
183177
isTouchingLinear (vec) {
184-
if (!this._data) return;
178+
if (!this._colorData) return;
185179
const x = Math.floor(vec[0] * (this._width - 1));
186180
const y = Math.floor(vec[1] * (this._height - 1));
187181
return getPoint(this, x, y) > 0 ||

0 commit comments

Comments
 (0)