Inital commit
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
export function urlToImage(base64Str: string) {
|
||||
return new Promise<HTMLImageElement>((resolve, reject) => {
|
||||
const imageSrc = new Image();
|
||||
imageSrc.addEventListener("load", () => {
|
||||
resolve(imageSrc);
|
||||
});
|
||||
imageSrc.addEventListener("error", () => {
|
||||
reject(new Error("Failed to load image"));
|
||||
});
|
||||
imageSrc.src = base64Str;
|
||||
});
|
||||
}
|
||||
|
||||
export function imageToCanvas(image: HTMLImageElement) {
|
||||
const canvas = document.createElement("canvas");
|
||||
canvas.width = image.naturalWidth;
|
||||
canvas.height = image.naturalHeight;
|
||||
|
||||
const ctx = canvas.getContext("2d");
|
||||
if (!ctx) throw new Error("No canvas context");
|
||||
|
||||
ctx.drawImage(image, 0, 0);
|
||||
return canvas;
|
||||
}
|
||||
|
||||
export function canvasToBlob(canvas: HTMLCanvasElement) {
|
||||
return new Promise<Blob>((resolve, reject) => {
|
||||
canvas.toBlob((b) => {
|
||||
if (!b) reject(new Error("Failed to create blob"));
|
||||
else resolve(b);
|
||||
}, "image/png");
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user