2021-09-15 21:19:03 +02:00
|
|
|
import { CollageModes } from "../src/common/collages.ts";
|
|
|
|
import {
|
|
|
|
CanvasRenderingContext2D,
|
|
|
|
Image,
|
|
|
|
} from "https://deno.land/x/canvas/mod.ts";
|
2021-09-15 19:08:53 +02:00
|
|
|
import { CollageContext, CollageImage } from "../src/common/types.ts";
|
2021-09-19 11:46:49 +02:00
|
|
|
import { init } from "https://deno.land/x/canvas@v1.3.0/mod.ts";
|
|
|
|
|
|
|
|
const canvasKit = await init();
|
2021-09-15 19:08:53 +02:00
|
|
|
|
|
|
|
export class ProxyImage implements CollageImage {
|
2021-09-19 11:46:49 +02:00
|
|
|
private filepath: string;
|
|
|
|
private _image: Image | undefined;
|
|
|
|
|
|
|
|
constructor(filepath: string) {
|
|
|
|
this.filepath = filepath;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get image(): Image {
|
|
|
|
if (!this._image) {
|
|
|
|
const image = canvasKit.MakeImageFromEncoded(Deno.readFileSync(this.filepath))
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-09-15 21:19:03 +02:00
|
|
|
export type CastCanvasRenderingContext =
|
|
|
|
& CanvasRenderingContext2D
|
|
|
|
& CollageContext;
|
2021-09-15 19:08:53 +02:00
|
|
|
|
2021-09-15 21:19:03 +02:00
|
|
|
export default class BrowserCollageModes
|
|
|
|
extends CollageModes<CastCanvasRenderingContext, ProxyImage> {
|
|
|
|
drawImage(
|
|
|
|
ctx: CastCanvasRenderingContext,
|
|
|
|
image: ProxyImage,
|
|
|
|
sx: number,
|
|
|
|
sy: number,
|
|
|
|
sw: number,
|
|
|
|
sh: number,
|
|
|
|
dx: number,
|
|
|
|
dy: number,
|
|
|
|
dw: number,
|
|
|
|
dh: number,
|
|
|
|
): 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
|
|
|
}
|
|
|
|
}
|