2021-09-15 21:19:03 +02:00
|
|
|
import { CollageModes } from "../src/common/collages.ts";
|
|
|
|
import {
|
2022-01-19 20:52:02 +01:00
|
|
|
init,
|
2021-09-15 21:19:03 +02:00
|
|
|
CanvasRenderingContext2D,
|
2021-09-20 22:38:04 +02:00
|
|
|
EmulatedCanvas2D,
|
2021-09-15 21:19:03 +02:00
|
|
|
Image,
|
2022-01-19 20:52:02 +01:00
|
|
|
} from "https://deno.land/x/canvas@v1.3.0/mod.ts";
|
2021-09-20 22:41:00 +02:00
|
|
|
import { CollageImage } from "../src/common/types.ts";
|
2021-09-19 11:46:49 +02:00
|
|
|
|
|
|
|
const canvasKit = await init();
|
2021-09-15 19:08:53 +02:00
|
|
|
|
|
|
|
export class ProxyImage implements CollageImage {
|
2021-09-20 22:38:04 +02:00
|
|
|
private filepath: string | null;
|
2021-09-19 11:46:49 +02:00
|
|
|
private _image: Image | undefined;
|
|
|
|
|
2021-09-20 22:38:04 +02:00
|
|
|
constructor(input: string | Image) {
|
|
|
|
if (typeof input === "string") {
|
|
|
|
this.filepath = input;
|
|
|
|
} else {
|
|
|
|
this.filepath = null;
|
|
|
|
this._image = input;
|
|
|
|
}
|
2021-09-19 11:46:49 +02:00
|
|
|
}
|
|
|
|
|
2021-09-22 23:55:48 +02:00
|
|
|
public get path(): string | null {
|
|
|
|
return this.filepath;
|
|
|
|
}
|
|
|
|
|
2021-09-19 11:46:49 +02:00
|
|
|
public get image(): Image {
|
|
|
|
if (!this._image) {
|
2021-09-20 22:38:04 +02:00
|
|
|
const image = canvasKit.MakeImageFromEncoded(
|
2022-01-19 20:52:02 +01:00
|
|
|
Deno.readFileSync(this.filepath!)
|
2021-09-20 22:38:04 +02:00
|
|
|
);
|
2021-09-19 11:46:49 +02:00
|
|
|
if (!image) {
|
|
|
|
throw Error(`Failed loading ${this.filepath}!`);
|
|
|
|
}
|
|
|
|
this._image = image;
|
|
|
|
}
|
|
|
|
return this._image;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get width(): number {
|
|
|
|
return this.image.width();
|
2021-09-15 21:19:03 +02:00
|
|
|
}
|
2021-09-15 19:08:53 +02:00
|
|
|
|
2021-09-19 11:46:49 +02:00
|
|
|
public get height(): number {
|
|
|
|
return this.image.height();
|
2021-09-15 21:19:03 +02:00
|
|
|
}
|
2021-09-15 19:08:53 +02:00
|
|
|
}
|
|
|
|
|
2022-01-19 20:52:02 +01:00
|
|
|
declare module "https://deno.land/x/canvas@v1.3.0/mod.ts" {
|
2021-09-20 22:38:04 +02:00
|
|
|
interface HTMLCanvasElement {
|
|
|
|
width: number;
|
|
|
|
height: number;
|
2021-09-20 22:41:00 +02:00
|
|
|
getContext(x: "2d"): CanvasRenderingContext2D;
|
2021-09-20 22:38:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 22:41:00 +02:00
|
|
|
export class DenoCollageModes extends CollageModes<
|
2021-09-20 22:38:04 +02:00
|
|
|
CanvasRenderingContext2D,
|
|
|
|
ProxyImage,
|
|
|
|
EmulatedCanvas2D
|
2021-09-20 22:41:00 +02:00
|
|
|
> {
|
2021-09-20 22:38:04 +02:00
|
|
|
createCanvas(w: number, h: number): EmulatedCanvas2D {
|
|
|
|
const canvas = canvasKit.MakeCanvas(Math.round(w), Math.round(h));
|
|
|
|
if (!canvas) {
|
|
|
|
throw Error("Error initializing canvas.");
|
|
|
|
}
|
|
|
|
return canvas;
|
|
|
|
}
|
|
|
|
|
|
|
|
canvasToImage(canvas: EmulatedCanvas2D): PromiseLike<ProxyImage> {
|
2022-01-19 20:52:02 +01:00
|
|
|
const image = canvasKit.MakeImageFromEncoded(canvas.toBuffer());
|
2021-09-20 22:38:04 +02:00
|
|
|
if (!image) {
|
|
|
|
throw Error("Something went wrong converting canvas to image.");
|
|
|
|
}
|
|
|
|
return Promise.resolve(new ProxyImage(image));
|
|
|
|
}
|
2021-09-15 19:08:53 +02:00
|
|
|
|
2021-09-15 21:19:03 +02:00
|
|
|
drawImage(
|
2021-09-20 22:38:04 +02:00
|
|
|
ctx: CanvasRenderingContext2D,
|
2021-09-15 21:19:03 +02:00
|
|
|
image: ProxyImage,
|
|
|
|
sx: number,
|
|
|
|
sy: number,
|
|
|
|
sw: number,
|
|
|
|
sh: number,
|
|
|
|
dx: number,
|
|
|
|
dy: number,
|
|
|
|
dw: number,
|
2022-01-19 20:52:02 +01:00
|
|
|
dh: number
|
2021-09-15 21:19:03 +02:00
|
|
|
): void {
|
2021-09-19 11:46:49 +02:00
|
|
|
ctx.drawImage(image.image, sx, sy, sw, sh, dx, dy, dw, dh);
|
2021-09-15 21:19:03 +02:00
|
|
|
}
|
2022-01-19 22:01:04 +01:00
|
|
|
|
|
|
|
fillText(
|
|
|
|
ctx: CanvasRenderingContext2D,
|
|
|
|
text: string,
|
|
|
|
fontSize: number,
|
|
|
|
x: number,
|
|
|
|
y: number,
|
|
|
|
maxWidth: number
|
|
|
|
): void {
|
|
|
|
ctx.globalCompositeOperation = "difference";
|
|
|
|
ctx.fillStyle = "white";
|
|
|
|
ctx.font = `bold ${fontSize}px sans-serif`;
|
|
|
|
ctx.fillText(text, x, y, maxWidth);
|
|
|
|
}
|
2021-09-15 21:19:03 +02:00
|
|
|
}
|
2021-09-20 22:38:04 +02:00
|
|
|
|
|
|
|
export const denoCollageModes = new DenoCollageModes();
|