Skip to content

Instantly share code, notes, and snippets.

@nanot1m
Created September 27, 2021 07:26
Show Gist options
  • Save nanot1m/80e50c087c28cbde05cdbb9a85d4d7b3 to your computer and use it in GitHub Desktop.
Save nanot1m/80e50c087c28cbde05cdbb9a85d4d7b3 to your computer and use it in GitHub Desktop.
Function to add opacity to the canvas image
function setCanvasImageOpacity(ctx: CanvasRenderingContext2D, opacity: number) {
const {width, height} = ctx.canvas
const image = ctx.getImageData(0, 0, width, height)
for (let i = 3; i < image.data.length; i += 4) {
image.data[i] *= opacity
}
ctx.putImageData(image, 0, 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment