mirror of
https://github.com/scratchfoundation/scratch-render.git
synced 2025-08-28 22:30:04 -04:00
Merge pull request #415 from mzgoddard/drop-silhouette-alpha-buffer
Replace Silhouette._data with Silhouette._colorData
This commit is contained in:
commit
19ee8e8eaa
1 changed files with 4 additions and 10 deletions
|
@ -19,12 +19,12 @@ let __SilhouetteUpdateCanvas;
|
||||||
* @param {number} y - y
|
* @param {number} y - y
|
||||||
* @return {number} Alpha value for x/y position
|
* @return {number} Alpha value for x/y position
|
||||||
*/
|
*/
|
||||||
const getPoint = ({_width: width, _height: height, _data: data}, x, y) => {
|
const getPoint = ({_width: width, _height: height, _colorData: data}, x, y) => {
|
||||||
// 0 if outside bouds, otherwise read from data.
|
// 0 if outside bouds, otherwise read from data.
|
||||||
if (x >= width || y >= height || x < 0 || y < 0) {
|
if (x >= width || y >= height || x < 0 || y < 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return data[(y * width) + x];
|
return data[(((y * width) + x) * 4) + 3];
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -76,7 +76,6 @@ class Silhouette {
|
||||||
* The data representing a skin's silhouette shape.
|
* The data representing a skin's silhouette shape.
|
||||||
* @type {Uint8ClampedArray}
|
* @type {Uint8ClampedArray}
|
||||||
*/
|
*/
|
||||||
this._data = null;
|
|
||||||
this._colorData = null;
|
this._colorData = null;
|
||||||
|
|
||||||
this.colorAtNearest = this.colorAtLinear = (_, dst) => dst.fill(0);
|
this.colorAtNearest = this.colorAtLinear = (_, dst) => dst.fill(0);
|
||||||
|
@ -100,16 +99,11 @@ class Silhouette {
|
||||||
ctx.drawImage(bitmapData, 0, 0, width, height);
|
ctx.drawImage(bitmapData, 0, 0, width, height);
|
||||||
const imageData = ctx.getImageData(0, 0, width, height);
|
const imageData = ctx.getImageData(0, 0, width, height);
|
||||||
|
|
||||||
this._data = new Uint8ClampedArray(imageData.data.length / 4);
|
|
||||||
this._colorData = imageData.data;
|
this._colorData = imageData.data;
|
||||||
// delete our custom overriden "uninitalized" color functions
|
// delete our custom overriden "uninitalized" color functions
|
||||||
// let the prototype work for itself
|
// let the prototype work for itself
|
||||||
delete this.colorAtNearest;
|
delete this.colorAtNearest;
|
||||||
delete this.colorAtLinear;
|
delete this.colorAtLinear;
|
||||||
|
|
||||||
for (let i = 0; i < imageData.data.length; i += 4) {
|
|
||||||
this._data[i / 4] = imageData.data[i + 3];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -166,7 +160,7 @@ class Silhouette {
|
||||||
* @return {boolean} If the nearest pixel has an alpha value.
|
* @return {boolean} If the nearest pixel has an alpha value.
|
||||||
*/
|
*/
|
||||||
isTouchingNearest (vec) {
|
isTouchingNearest (vec) {
|
||||||
if (!this._data) return;
|
if (!this._colorData) return;
|
||||||
return getPoint(
|
return getPoint(
|
||||||
this,
|
this,
|
||||||
Math.floor(vec[0] * (this._width - 1)),
|
Math.floor(vec[0] * (this._width - 1)),
|
||||||
|
@ -181,7 +175,7 @@ class Silhouette {
|
||||||
* @return {boolean} Any of the pixels have some alpha.
|
* @return {boolean} Any of the pixels have some alpha.
|
||||||
*/
|
*/
|
||||||
isTouchingLinear (vec) {
|
isTouchingLinear (vec) {
|
||||||
if (!this._data) return;
|
if (!this._colorData) return;
|
||||||
const x = Math.floor(vec[0] * (this._width - 1));
|
const x = Math.floor(vec[0] * (this._width - 1));
|
||||||
const y = Math.floor(vec[1] * (this._height - 1));
|
const y = Math.floor(vec[1] * (this._height - 1));
|
||||||
return getPoint(this, x, y) > 0 ||
|
return getPoint(this, x, y) > 0 ||
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue