Skip to content

Instantly share code, notes, and snippets.

@AlainBarrios
Created March 27, 2020 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlainBarrios/c4cd5ff37cec47f4b90763158384af22 to your computer and use it in GitHub Desktop.
Save AlainBarrios/c4cd5ff37cec47f4b90763158384af22 to your computer and use it in GitHub Desktop.
Know if the alpha is transparent
const isEmpty = (texture) => {
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
ctx.drawImage(texture.image, 0, 0);
const data = ctx.getImageData(0, 0, texture.image.width, texture.image.height);
const lengthData = data.data.length
let numVisible = 0
for(let i = 0; i < lengthData; i += 4){
if(data.data[i + 3] < 10) continue
numVisible++
}
return [data.data, numVisible]
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment