-
-
Save huntercaron/6cd9b38229ea06255de7a79a257308d7 to your computer and use it in GitHub Desktop.
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
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