fix: respect clip frame during exports

This commit is contained in:
Arnošt Pleskot 2023-09-18 14:10:09 +02:00
parent 0c4d6fbe95
commit c8180308c2
No known key found for this signature in database
3 changed files with 25 additions and 3 deletions

View File

@ -48,6 +48,7 @@ export const exportCanvas = async (
exportScale: appState.exportScale, exportScale: appState.exportScale,
exportEmbedScene: appState.exportEmbedScene && type === "svg", exportEmbedScene: appState.exportEmbedScene && type === "svg",
fancyBackgroundImageKey: appState.fancyBackgroundImageKey, fancyBackgroundImageKey: appState.fancyBackgroundImageKey,
clipFrame: appState.frameRendering.clip,
}, },
files, files,
); );

View File

@ -366,6 +366,10 @@ const frameClip = (
renderConfig: StaticCanvasRenderConfig, renderConfig: StaticCanvasRenderConfig,
appState: StaticCanvasAppState, appState: StaticCanvasAppState,
) => { ) => {
if (!appState.frameRendering.clip) {
return;
}
context.translate(frame.x + appState.scrollX, frame.y + appState.scrollY); context.translate(frame.x + appState.scrollX, frame.y + appState.scrollY);
context.beginPath(); context.beginPath();
if (context.roundRect && !renderConfig.isExporting) { if (context.roundRect && !renderConfig.isExporting) {

View File

@ -79,6 +79,9 @@ export const exportToCanvas = async (
padding, padding,
onlyExportingSingleFrame, onlyExportingSingleFrame,
exportingWithFancyBackground, exportingWithFancyBackground,
opts: {
clipFrame: appState.frameRendering.clip,
},
}) })
: getCanvasSize({ : getCanvasSize({
elements, elements,
@ -87,6 +90,7 @@ export const exportToCanvas = async (
exportingWithFancyBackground, exportingWithFancyBackground,
opts: { opts: {
aspectRatio: { width: 16, height: 9 }, aspectRatio: { width: 16, height: 9 },
clipFrame: appState.frameRendering.clip,
}, },
}); });
@ -112,6 +116,7 @@ export const exportToCanvas = async (
const commonBounds = getElementsSize({ const commonBounds = getElementsSize({
elements, elements,
onlyExportingSingleFrame, onlyExportingSingleFrame,
clipFrame: appState.frameRendering.clip,
}); });
const contentSize: Dimensions = { const contentSize: Dimensions = {
width: distance(commonBounds[0], commonBounds[2]), width: distance(commonBounds[0], commonBounds[2]),
@ -183,6 +188,7 @@ export const exportToSvg = async (
exportEmbedScene?: boolean; exportEmbedScene?: boolean;
renderFrame?: boolean; renderFrame?: boolean;
fancyBackgroundImageKey?: keyof typeof FANCY_BACKGROUND_IMAGES; fancyBackgroundImageKey?: keyof typeof FANCY_BACKGROUND_IMAGES;
clipFrame?: boolean;
}, },
files: BinaryFiles | null, files: BinaryFiles | null,
opts?: { opts?: {
@ -235,6 +241,9 @@ export const exportToSvg = async (
padding, padding,
onlyExportingSingleFrame, onlyExportingSingleFrame,
exportingWithFancyBackground, exportingWithFancyBackground,
opts: {
clipFrame: appState.clipFrame ?? false,
},
}) })
: getCanvasSize({ : getCanvasSize({
elements, elements,
@ -243,6 +252,7 @@ export const exportToSvg = async (
exportingWithFancyBackground, exportingWithFancyBackground,
opts: { opts: {
aspectRatio: { width: 16, height: 9 }, aspectRatio: { width: 16, height: 9 },
clipFrame: appState.clipFrame ?? false,
}, },
}); });
@ -294,7 +304,7 @@ export const exportToSvg = async (
: elements.find((element) => element.type === "frame"); : elements.find((element) => element.type === "frame");
let exportingFrameClipPath = ""; let exportingFrameClipPath = "";
if (exportingFrame) { if (exportingFrame && appState.clipFrame) {
const [x1, y1, x2, y2] = getElementAbsoluteCoords(exportingFrame); const [x1, y1, x2, y2] = getElementAbsoluteCoords(exportingFrame);
const cx = (x2 - x1) / 2 - (exportingFrame.x - x1); const cx = (x2 - x1) / 2 - (exportingFrame.x - x1);
const cy = (y2 - y1) / 2 - (exportingFrame.y - y1); const cy = (y2 - y1) / 2 - (exportingFrame.y - y1);
@ -341,6 +351,7 @@ export const exportToSvg = async (
const commonBounds = getElementsSize({ const commonBounds = getElementsSize({
elements, elements,
onlyExportingSingleFrame, onlyExportingSingleFrame,
clipFrame: appState.clipFrame ?? false,
}); });
const contentSize: Dimensions = { const contentSize: Dimensions = {
width: distance(commonBounds[0], commonBounds[2]), width: distance(commonBounds[0], commonBounds[2]),
@ -389,9 +400,11 @@ export const exportToSvg = async (
const getElementsSize = ({ const getElementsSize = ({
elements, elements,
onlyExportingSingleFrame, onlyExportingSingleFrame,
clipFrame,
}: { }: {
elements: readonly NonDeletedExcalidrawElement[]; elements: readonly NonDeletedExcalidrawElement[];
onlyExportingSingleFrame: boolean; onlyExportingSingleFrame: boolean;
clipFrame: boolean;
}): Bounds => { }): Bounds => {
// we should decide if we are exporting the whole canvas // we should decide if we are exporting the whole canvas
// if so, we are not clipping elements in the frame // if so, we are not clipping elements in the frame
@ -400,7 +413,7 @@ const getElementsSize = ({
Scene.getScene(elements[0])?.getNonDeletedElements()?.length === Scene.getScene(elements[0])?.getNonDeletedElements()?.length ===
elements.length; elements.length;
if (!isExportingWholeCanvas || onlyExportingSingleFrame) { if ((!isExportingWholeCanvas || onlyExportingSingleFrame) && clipFrame) {
const frames = elements.filter((element) => element.type === "frame"); const frames = elements.filter((element) => element.type === "frame");
const exportedFrameIds = frames.reduce((acc, frame) => { const exportedFrameIds = frames.reduce((acc, frame) => {
@ -430,11 +443,12 @@ const getCanvasSize = ({
padding: ExportPadding; padding: ExportPadding;
onlyExportingSingleFrame: boolean; onlyExportingSingleFrame: boolean;
exportingWithFancyBackground: boolean; exportingWithFancyBackground: boolean;
opts?: { aspectRatio: Dimensions }; opts?: { aspectRatio?: Dimensions; clipFrame?: boolean };
}): [number, number, number, number] => { }): [number, number, number, number] => {
const [minX, minY, maxX, maxY] = getElementsSize({ const [minX, minY, maxX, maxY] = getElementsSize({
elements, elements,
onlyExportingSingleFrame, onlyExportingSingleFrame,
clipFrame: opts?.clipFrame ?? false,
}); });
let width = 0; let width = 0;
@ -474,6 +488,9 @@ export const getExportSize = (
appState, appState,
elements, elements,
), ),
opts: {
clipFrame: appState.frameRendering.clip,
},
}).map((dimension) => Math.trunc(dimension * scale)); }).map((dimension) => Math.trunc(dimension * scale));
return [width, height]; return [width, height];