Compare commits
1 commit
Author | SHA1 | Date | |
---|---|---|---|
39f70843f6 |
4 changed files with 63 additions and 1 deletions
|
@ -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();
|
||||
|
|
10
cli/main.ts
10
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());
|
||||
|
|
|
@ -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<I>;
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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<T>(arr: readonly T[]): T {
|
||||
return arr[randint(arr.length)];
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue