Skip to content

Instantly share code, notes, and snippets.

@ledunguit
Created November 5, 2023 06:56
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 ledunguit/25941684bf4ed949d2d760f439af6eb8 to your computer and use it in GitHub Desktop.
Save ledunguit/25941684bf4ed949d2d760f439af6eb8 to your computer and use it in GitHub Desktop.
Add alpha channel to Unit8ClampedArray
function addAlphaChannelToUnit8ClampedArray(unit8Array: Uint8ClampedArray, imageWidth: number, imageHeight: number) {
const newImageData = new Uint8ClampedArray(imageWidth * imageHeight * 4);
for (let j = 0, k = 0, jj = imageWidth * imageHeight * 4; j < jj; ) {
newImageData[j++] = unit8Array[k++];
newImageData[j++] = unit8Array[k++];
newImageData[j++] = unit8Array[k++];
newImageData[j++] = 255;
}
return newImageData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment