From 38fd4fb165cde05ea1906c24cd51414f71c349b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arno=C5=A1t=20Pleskot?= Date: Wed, 23 Aug 2023 00:42:31 +0200 Subject: [PATCH] feat: center and scale solid background on top of fancyBackground --- src/scene/export.ts | 13 +++++++------ src/scene/fancyBackground.ts | 21 +++++++++++++++++---- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/scene/export.ts b/src/scene/export.ts index 0d218882d..d824babce 100644 --- a/src/scene/export.ts +++ b/src/scene/export.ts @@ -89,20 +89,21 @@ export const exportToCanvas = async ( exportWithFancyBackground && appState.fancyBackgroundImageKey !== "solid" ) { + const commonBounds = getCommonBounds(elements); + const contentSize: Dimensions = { + width: distance(commonBounds[0], commonBounds[2]), + height: distance(commonBounds[1], commonBounds[3]), + }; + await applyFancyBackgroundOnCanvas({ canvas, fancyBackgroundImageKey: appState.fancyBackgroundImageKey, backgroundColor: viewBackgroundColor, exportScale: scale, theme: appState.exportWithDarkMode ? THEME.DARK : THEME.LIGHT, + contentSize, }); - const commonBounds = getCommonBounds(elements); - const contentSize: Dimensions = { - width: distance(commonBounds[0], commonBounds[2]), - height: distance(commonBounds[1], commonBounds[3]), - }; - scrollXAdjustment = (width - contentSize.width - padding * 2) / 2; scrollYAdjustment = (height - contentSize.height - padding * 2) / 2; } diff --git a/src/scene/fancyBackground.ts b/src/scene/fancyBackground.ts index c12dc634f..34f226cd6 100644 --- a/src/scene/fancyBackground.ts +++ b/src/scene/fancyBackground.ts @@ -67,6 +67,7 @@ const addContentBackground = ( contentBackgroundColor: string, exportScale: AppState["exportScale"], theme: AppState["theme"], + contentSize: Dimensions, ) => { const shadows = [ { @@ -98,12 +99,21 @@ const addContentBackground = ( context.shadowOffsetX = shadow.offsetX * exportScale; context.shadowOffsetY = shadow.offsetY * exportScale; + const x = + (normalizedDimensions.width - contentSize.width) / 2 - FANCY_BG_PADDING; + const y = + (normalizedDimensions.height - contentSize.height) / 2 - FANCY_BG_PADDING; + if (context.roundRect) { context.roundRect( - FANCY_BG_PADDING * exportScale, - FANCY_BG_PADDING * exportScale, - normalizedDimensions.width - FANCY_BG_PADDING * 2 * exportScale, - normalizedDimensions.height - FANCY_BG_PADDING * 2 * exportScale, + x, + y, + (contentSize.width + + (DEFAULT_EXPORT_PADDING + FANCY_BG_BORDER_RADIUS) * 2) * + exportScale, + (contentSize.height * exportScale + + (DEFAULT_EXPORT_PADDING + FANCY_BG_BORDER_RADIUS) * 2) * + exportScale, FANCY_BG_BORDER_RADIUS * exportScale, ); } else { @@ -135,6 +145,7 @@ export const applyFancyBackgroundOnCanvas = async ({ backgroundColor, exportScale, theme, + contentSize, }: { canvas: HTMLCanvasElement; fancyBackgroundImageKey: Exclude< @@ -144,6 +155,7 @@ export const applyFancyBackgroundOnCanvas = async ({ backgroundColor: string; exportScale: AppState["exportScale"]; theme: AppState["theme"]; + contentSize: Dimensions; }) => { const context = canvas.getContext("2d")!; @@ -172,6 +184,7 @@ export const applyFancyBackgroundOnCanvas = async ({ backgroundColor, exportScale, theme, + contentSize, ); };