-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-utils.ts
49 lines (40 loc) · 1.51 KB
/
image-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { CreateElement, CreateImage } from "./html-utils";
export function serializeImage(image: HTMLImageElement): string {
const canvas = CreateElement("canvas", {
width : image.width,
height: image.height,
});
(canvas.getContext("2d") as CanvasRenderingContext2D).drawImage(image, 0, 0);
return canvas.toDataURL("image/png");
}
export function deserializeImage(image: string): HTMLImageElement {
return CreateImage({
src: image,
});
}
export function createImage(callback: (context: CanvasRenderingContext2D) => void, width: number, height = width): HTMLCanvasElement {
const canvas = CreateElement("canvas", {
width,
height,
});
callback(canvas.getContext("2d") as CanvasRenderingContext2D);
return canvas;
}
export function imageAsPromise(src: string): Promise<HTMLImageElement> {
return new Promise<HTMLImageElement>((success, reject) => {
const image = document.createElement("img");
const callback = (e: any): void => {
image.dataset.hasError = e ? "true" : "false";
if (e.type === "error") {
reject(e);
return console.error("Error during loading image ", src);
}
image.removeEventListener("load", callback);
image.removeEventListener("error", callback);
success(image);
};
image.addEventListener("load", callback);
image.addEventListener("error", callback);
image.src = src;
});
}