Skip to content

Instantly share code, notes, and snippets.

@redblobgames
Created September 19, 2018 20:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save redblobgames/d5d1e61c68e29bed96c8cb728db54f93 to your computer and use it in GitHub Desktop.
Save redblobgames/d5d1e61c68e29bed96c8cb728db54f93 to your computer and use it in GitHub Desktop.
Remove alpha channel on a canvas, so it's always transparent or always opaque
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const pixels = imageData.data;
for (let i = 3, n = canvas.width * canvas.height * 4; i < n; i += 4) {
pixels[i] = pixels[i] < 127? 0 : 255
}
ctx.putImageData(imageData, 0, 0);
@pmallol
Copy link

pmallol commented Feb 18, 2022

Thank you! 🔥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment