Created
September 27, 2021 07:26
-
-
Save nanot1m/80e50c087c28cbde05cdbb9a85d4d7b3 to your computer and use it in GitHub Desktop.
Function to add opacity to the canvas image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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