base for quote pasting

This commit is contained in:
Tomáš Mládek 2022-01-19 22:01:04 +01:00
parent c89080ff00
commit 39f70843f6
No known key found for this signature in database
GPG key ID: ED21612889E75EC5
4 changed files with 63 additions and 1 deletions

View file

@ -91,6 +91,20 @@ export class DenoCollageModes extends CollageModes<
): void { ): void {
ctx.drawImage(image.image, sx, sy, sw, sh, dx, dy, dw, dh); ctx.drawImage(image.image, sx, sy, sw, sh, dx, dy, dw, dh);
} }
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);
}
} }
export const denoCollageModes = new DenoCollageModes(); export const denoCollageModes = new DenoCollageModes();

View file

@ -100,6 +100,16 @@ if (modeKey === "recursive") {
); );
} }
console.log(args);
if (args["quotes"]) {
const text = await Deno.readTextFile(args["quotes"]);
const lines = text.split("\n");
const quote = choice(lines).trim();
console.log(quote);
denoCollageModes.placeText(context, quote);
}
const output = args["output"]; const output = args["output"];
console.log(`Saving to "${output}"...`); console.log(`Saving to "${output}"...`);
await Deno.writeFile(output, canvas.toBuffer()); await Deno.writeFile(output, canvas.toBuffer());

View file

@ -6,7 +6,7 @@ import {
CollageMode, CollageMode,
Segment, Segment,
} from "@/common/types"; } from "@/common/types";
import { choice, randint, range, shuffle } from "@/common/utils"; import { choice, randint, randrange, range, shuffle } from "@/common/utils";
export const collageModeType = [ export const collageModeType = [
"clean_grid", "clean_grid",
@ -378,6 +378,31 @@ export abstract class CollageModes<
}); });
} }
public placeText(ctx: C, text: string) {
const fontSize = Math.round(
((ctx.canvas.width + ctx.canvas.height) / 2) * randrange(0.02, 0.1)
);
console.log(fontSize);
const margin = ((ctx.canvas.width + ctx.canvas.height) / 2) * 0.1;
const positions = {
center: {
x: margin,
y: ctx.canvas.height / 2 + fontSize / 2,
maxWidth: ctx.canvas.width - margin * 2,
},
};
const position = choice(Object.values(positions));
this.fillText(
ctx,
text,
fontSize,
position.x,
position.y,
position.maxWidth
);
}
abstract createCanvas(w: number, h: number): CS; abstract createCanvas(w: number, h: number): CS;
abstract canvasToImage(canvas: CS): PromiseLike<I>; abstract canvasToImage(canvas: CS): PromiseLike<I>;
@ -394,4 +419,13 @@ export abstract class CollageModes<
dw: number, dw: number,
dh: number dh: number
): void; ): void;
abstract fillText(
ctx: C,
text: string,
fontSize: number,
x: number,
y: number,
maxWidth: number
): void;
} }

View file

@ -18,6 +18,10 @@ export function randint(n: number) {
return Math.floor(Math.random() * n); return Math.floor(Math.random() * n);
} }
export function randrange(min: number, max:number): number {
return Math.random() * (max - min) + min;
}
export function choice<T>(arr: readonly T[]): T { export function choice<T>(arr: readonly T[]): T {
return arr[randint(arr.length)]; return arr[randint(arr.length)];
} }