forked from DjDeveloperr/deno-canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
34 lines (26 loc) · 847 Bytes
/
mod.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
import init from "./src/canvaskit.ts";
import { decodeBase64 } from "./src/base64.ts";
const canvas = await init();
export function dataURLtoFile(dataurl: string) {
let arr: string[] = dataurl.split(",");
return decodeBase64(arr[1]);
}
export async function loadImage(url: string | Uint8Array) {
let data;
if (url instanceof Uint8Array) {
data = url;
} else if (url.startsWith("http")) {
data = await fetch(url).then((e) => e.arrayBuffer()).then((e) =>
new Uint8Array(e)
);
} else {
data = await Deno.readFile(url);
}
const img = canvas.MakeImageFromEncoded(data);
if (!img) throw new Error("Invalid image data");
return img;
}
export const createCanvas = canvas.MakeCanvas;
export * from "./src/types.ts";
export default canvas;
export * from "./src/base64.ts";