From 7711f19810fd3f9e3c18822f08020f57d125d0c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Wed, 15 Jul 2020 23:05:15 +0200 Subject: [PATCH] split concentric into two modes --- src/collages.ts | 54 +++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/src/collages.ts b/src/collages.ts index e1cec2d..b06c4d0 100644 --- a/src/collages.ts +++ b/src/collages.ts @@ -1,7 +1,7 @@ import {CollageMode} from "@/types"; import {randint, shuffle} from "@/utils"; -const collageModeType = ["grid", "row", "irow", "col", "icol", "center"] as const; +const collageModeType = ["grid", "row", "irow", "col", "icol", "concentric_factor", "concentric_spaced"] as const; export type CollageModeType = typeof collageModeType[number]; function cleanDraw(ctx: CanvasRenderingContext2D, image: ImageBitmap, @@ -126,8 +126,8 @@ const modes: { [key in CollageModeType]: CollageMode } = { }); }, }, - "center": { - name: "Concentric", + "concentric_factor": { + name: "Constant factor concentric", minImages: 2, forceConfig: { cleanCrops: true @@ -137,27 +137,37 @@ const modes: { [key in CollageModeType]: CollageMode } = { const x = ctx.canvas.width / 2; const y = ctx.canvas.height / 2; - if (Math.random() < .66) { - selectedImages.forEach((image, idx) => { - cleanDraw( - ctx, image, x, y, - ctx.canvas.width - (ctx.canvas.width / selectedImages.length * idx), - ctx.canvas.height - (ctx.canvas.height / selectedImages.length * idx), - ); - }); + let factor: number; + if (Math.random() > .5) { + const factors = [1 / Math.sqrt(2), .5, .88]; + factor = factors[Math.floor(Math.random() * factors.length)]; } else { - let factor: number; - if (Math.random() > .5) { - const factors = [1 / Math.sqrt(2), .5, .88]; - factor = factors[Math.floor(Math.random() * factors.length)]; - } else { - factor = 1 - (1 / selectedImages.length); - } - selectedImages.forEach((image, idx) => { - const ratio = Math.pow(factor, idx); - cleanDraw(ctx, image, x, y, ctx.canvas.width * ratio, ctx.canvas.height * ratio); - }); + factor = 1 - (1 / selectedImages.length); } + selectedImages.forEach((image, idx) => { + const ratio = Math.pow(factor, idx); + cleanDraw(ctx, image, x, y, ctx.canvas.width * ratio, ctx.canvas.height * ratio); + }); + } + }, + "concentric_spaced": { + name: "Equally spaced concentric", + minImages: 2, + forceConfig: { + cleanCrops: true + }, + place: (ctx, images, config) => { + const selectedImages = shuffle(images).slice(0, config.numImages || randint(4) + 2); + const x = ctx.canvas.width / 2; + const y = ctx.canvas.height / 2; + + selectedImages.forEach((image, idx) => { + cleanDraw( + ctx, image, x, y, + ctx.canvas.width - (ctx.canvas.width / selectedImages.length * idx), + ctx.canvas.height - (ctx.canvas.height / selectedImages.length * idx), + ); + }); } } };