Skip to content

Instantly share code, notes, and snippets.

@huntercaron
Created March 18, 2024 17:21
Show Gist options
  • Save huntercaron/6cd9b38229ea06255de7a79a257308d7 to your computer and use it in GitHub Desktop.
Save huntercaron/6cd9b38229ea06255de7a79a257308d7 to your computer and use it in GitHub Desktop.
export function bytesFromCanvas(
canvas: HTMLCanvasElement
): Promise<Uint8Array | null> {
return new Promise<Uint8Array>((resolve, reject) => {
canvas.toBlob((blob) => {
if (!blob) throw new Error("Blob does not exist");
const reader = new FileReader();
reader.onload = () => {
if (!reader.result) {
throw new Error("Reader result does not exist");
}
resolve(new Uint8Array(reader.result as ArrayBuffer));
};
reader.onerror = () => reject(new Error("Could not read from blob"));
reader.readAsArrayBuffer(blob);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
OSZAR »