fix: frame export with fancy background

This commit is contained in:
Arnošt Pleskot 2023-09-08 00:13:57 +02:00
parent 75cd5c20b6
commit 47de794c44
No known key found for this signature in database

View File

@ -55,22 +55,24 @@ export const exportToCanvas = async (
return { canvas, scale: appState.exportScale }; return { canvas, scale: appState.exportScale };
}, },
) => { ) => {
const exportWithFancyBackground = const exportingWithFancyBackground = isExportingWithFacnyBackground(
exportBackground && appState,
appState.fancyBackgroundImageKey && elements,
appState.fancyBackgroundImageKey !== "solid" && );
elements.length > 0;
const padding = !exportWithFancyBackground const padding = !exportingWithFancyBackground
? convertToExportPadding(exportPadding) ? convertToExportPadding(exportPadding)
: getFancyBackgroundPadding( : getFancyBackgroundPadding(
convertToExportPadding(exportPadding), convertToExportPadding(exportPadding),
FANCY_BG_INCLUDE_LOGO, FANCY_BG_INCLUDE_LOGO,
); );
const [minX, minY, width, height] = !exportWithFancyBackground const onlyExportingSingleFrame =
? getCanvasSize(elements, padding) isOnlyExportingSingleFrame(elements) && !exportingWithFancyBackground;
: getCanvasSize(elements, padding, {
const [minX, minY, width, height] = !exportingWithFancyBackground
? getCanvasSize(elements, padding, onlyExportingSingleFrame)
: getCanvasSize(elements, padding, onlyExportingSingleFrame, {
aspectRatio: { width: 16, height: 9 }, aspectRatio: { width: 16, height: 9 },
}); });
@ -86,13 +88,11 @@ export const exportToCanvas = async (
files, files,
}); });
const onlyExportingSingleFrame = isOnlyExportingSingleFrame(elements);
let scrollXAdjustment = 0; let scrollXAdjustment = 0;
let scrollYAdjustment = 0; let scrollYAdjustment = 0;
if ( if (
exportWithFancyBackground && exportingWithFancyBackground &&
appState.fancyBackgroundImageKey !== "solid" appState.fancyBackgroundImageKey !== "solid"
) { ) {
const commonBounds = getCommonBounds(elements); const commonBounds = getCommonBounds(elements);
@ -127,7 +127,7 @@ export const exportToCanvas = async (
appState: { appState: {
...appState, ...appState,
viewBackgroundColor: viewBackgroundColor:
exportBackground && !exportWithFancyBackground exportBackground && !exportingWithFancyBackground
? viewBackgroundColor ? viewBackgroundColor
: null, : null,
scrollX: scrollX:
@ -142,7 +142,7 @@ export const exportToCanvas = async (
imageCache, imageCache,
renderGrid: false, renderGrid: false,
isExporting: true, isExporting: true,
exportWithFancyBackground, exportWithFancyBackground: exportingWithFancyBackground,
}, },
}); });
@ -172,16 +172,17 @@ export const exportToSvg = async (
viewBackgroundColor, viewBackgroundColor,
exportScale = 1, exportScale = 1,
exportEmbedScene, exportEmbedScene,
exportBackground,
} = appState; } = appState;
const exportWithFancyBackground = const exportingWithFancyBackground = isExportingWithFacnyBackground(
exportBackground && {
elements.length > 0 && exportBackground: appState.exportBackground,
appState.fancyBackgroundImageKey && fancyBackgroundImageKey: appState.fancyBackgroundImageKey,
appState.fancyBackgroundImageKey !== "solid"; },
elements,
);
const padding = !exportWithFancyBackground const padding = !exportingWithFancyBackground
? convertToExportPadding(exportPadding) ? convertToExportPadding(exportPadding)
: getFancyBackgroundPadding( : getFancyBackgroundPadding(
convertToExportPadding(exportPadding), convertToExportPadding(exportPadding),
@ -202,9 +203,13 @@ export const exportToSvg = async (
console.error(error); console.error(error);
} }
} }
const [minX, minY, width, height] = !exportWithFancyBackground
? getCanvasSize(elements, padding) const onlyExportingSingleFrame =
: getCanvasSize(elements, padding, { isOnlyExportingSingleFrame(elements) && !exportingWithFancyBackground;
const [minX, minY, width, height] = !exportingWithFancyBackground
? getCanvasSize(elements, padding, onlyExportingSingleFrame)
: getCanvasSize(elements, padding, onlyExportingSingleFrame, {
aspectRatio: { width: 16, height: 9 }, aspectRatio: { width: 16, height: 9 },
}); });
@ -239,8 +244,6 @@ export const exportToSvg = async (
Scene.getScene(elements[0])?.getNonDeletedElements()?.length === Scene.getScene(elements[0])?.getNonDeletedElements()?.length ===
elements.length; elements.length;
const onlyExportingSingleFrame = isOnlyExportingSingleFrame(elements);
const offsetX = -minX + (onlyExportingSingleFrame ? 0 : padding[3]); const offsetX = -minX + (onlyExportingSingleFrame ? 0 : padding[3]);
const offsetY = -minY + (onlyExportingSingleFrame ? 0 : padding[0]); const offsetY = -minY + (onlyExportingSingleFrame ? 0 : padding[0]);
@ -290,6 +293,7 @@ export const exportToSvg = async (
// render background rect // render background rect
if (appState.exportBackground && viewBackgroundColor) { if (appState.exportBackground && viewBackgroundColor) {
if ( if (
exportingWithFancyBackground &&
appState.fancyBackgroundImageKey && appState.fancyBackgroundImageKey &&
appState.fancyBackgroundImageKey !== "solid" appState.fancyBackgroundImageKey !== "solid"
) { ) {
@ -342,6 +346,7 @@ export const exportToSvg = async (
const getCanvasSize = ( const getCanvasSize = (
elements: readonly NonDeletedExcalidrawElement[], elements: readonly NonDeletedExcalidrawElement[],
exportPadding: ExportPadding, exportPadding: ExportPadding,
onlyExportingSingleFrame: boolean,
opts?: { aspectRatio: Dimensions }, opts?: { aspectRatio: Dimensions },
): [number, number, number, number] => { ): [number, number, number, number] => {
// we should decide if we are exporting the whole canvas // we should decide if we are exporting the whole canvas
@ -352,8 +357,6 @@ const getCanvasSize = (
Scene.getScene(elements[0])?.getNonDeletedElements()?.length === Scene.getScene(elements[0])?.getNonDeletedElements()?.length ===
elements.length; elements.length;
const onlyExportingSingleFrame = isOnlyExportingSingleFrame(elements);
if (!isExportingWholeCanvas || onlyExportingSingleFrame) { if (!isExportingWholeCanvas || onlyExportingSingleFrame) {
const frames = elements.filter((element) => element.type === "frame"); const frames = elements.filter((element) => element.type === "frame");
@ -398,11 +401,27 @@ export const getExportSize = (
elements: readonly NonDeletedExcalidrawElement[], elements: readonly NonDeletedExcalidrawElement[],
exportPadding: number, exportPadding: number,
scale: number, scale: number,
appState: AppState,
): [number, number] => { ): [number, number] => {
const [, , width, height] = getCanvasSize( const [, , width, height] = getCanvasSize(
elements, elements,
convertToExportPadding(exportPadding), convertToExportPadding(exportPadding),
isExportingWithFacnyBackground(appState, elements),
).map((dimension) => Math.trunc(dimension * scale)); ).map((dimension) => Math.trunc(dimension * scale));
return [width, height]; return [width, height];
}; };
const isExportingWithFacnyBackground = (
appState: Partial<
Pick<AppState, "exportBackground" | "fancyBackgroundImageKey">
>,
elements: readonly NonDeletedExcalidrawElement[],
): boolean => {
return Boolean(
appState.exportBackground &&
appState.fancyBackgroundImageKey &&
appState.fancyBackgroundImageKey !== "solid" &&
elements.length > 0,
);
};