From 39f70843f633e769840b6949efd9ac0da55206e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Wed, 19 Jan 2022 22:01:04 +0100 Subject: [PATCH] base for quote pasting --- cli/collages.ts | 14 ++++++++++++++ cli/main.ts | 10 ++++++++++ src/common/collages.ts | 36 +++++++++++++++++++++++++++++++++++- src/common/utils.ts | 4 ++++ 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/cli/collages.ts b/cli/collages.ts index 5709be4..2030bff 100644 --- a/cli/collages.ts +++ b/cli/collages.ts @@ -91,6 +91,20 @@ export class DenoCollageModes extends CollageModes< ): void { 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(); diff --git a/cli/main.ts b/cli/main.ts index e801e34..4032f4a 100644 --- a/cli/main.ts +++ b/cli/main.ts @@ -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"]; console.log(`Saving to "${output}"...`); await Deno.writeFile(output, canvas.toBuffer()); diff --git a/src/common/collages.ts b/src/common/collages.ts index 88cc9fd..c00e96d 100644 --- a/src/common/collages.ts +++ b/src/common/collages.ts @@ -6,7 +6,7 @@ import { CollageMode, Segment, } from "@/common/types"; -import { choice, randint, range, shuffle } from "@/common/utils"; +import { choice, randint, randrange, range, shuffle } from "@/common/utils"; export const collageModeType = [ "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 canvasToImage(canvas: CS): PromiseLike; @@ -394,4 +419,13 @@ export abstract class CollageModes< dw: number, dh: number ): void; + + abstract fillText( + ctx: C, + text: string, + fontSize: number, + x: number, + y: number, + maxWidth: number + ): void; } diff --git a/src/common/utils.ts b/src/common/utils.ts index c90de72..55cccec 100644 --- a/src/common/utils.ts +++ b/src/common/utils.ts @@ -18,6 +18,10 @@ export function randint(n: number) { return Math.floor(Math.random() * n); } +export function randrange(min: number, max:number): number { + return Math.random() * (max - min) + min; +} + export function choice(arr: readonly T[]): T { return arr[randint(arr.length)]; }