Skip to content

Commit

Permalink
feat: constrain export dialog preview size (excalidraw#6475)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwelle authored Apr 18, 2023
1 parent 801412b commit 4d0d844
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
24 changes: 11 additions & 13 deletions src/components/ImageExportDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { canvasToBlob } from "../data/blob";
import { NonDeletedExcalidrawElement } from "../element/types";
import { t } from "../i18n";
import { getSelectedElements, isSomeElementSelected } from "../scene";
import { exportToCanvas } from "../scene/export";
import { AppState, BinaryFiles } from "../types";
import { Dialog } from "./Dialog";
import { clipboard } from "./icons";
Expand All @@ -15,6 +14,7 @@ import { CheckboxItem } from "./CheckboxItem";
import { DEFAULT_EXPORT_PADDING, isFirefox } from "../constants";
import { nativeFileSystemSupported } from "../data/filesystem";
import { ActionManager } from "../actions/manager";
import { exportToCanvas } from "../packages/utils";

const supportsContextFilters =
"filter" in document.createElement("canvas").getContext("2d")!;
Expand Down Expand Up @@ -83,7 +83,6 @@ const ImageExportModal = ({
const someElementIsSelected = isSomeElementSelected(elements, appState);
const [exportSelected, setExportSelected] = useState(someElementIsSelected);
const previewRef = useRef<HTMLDivElement>(null);
const { exportBackground, viewBackgroundColor } = appState;
const [renderError, setRenderError] = useState<Error | null>(null);

const exportedElements = exportSelected
Expand All @@ -99,10 +98,16 @@ const ImageExportModal = ({
if (!previewNode) {
return;
}
exportToCanvas(exportedElements, appState, files, {
exportBackground,
viewBackgroundColor,
const maxWidth = previewNode.offsetWidth;
if (!maxWidth) {
return;
}
exportToCanvas({
elements: exportedElements,
appState,
files,
exportPadding,
maxWidthOrHeight: maxWidth,
})
.then((canvas) => {
setRenderError(null);
Expand All @@ -116,14 +121,7 @@ const ImageExportModal = ({
console.error(error);
setRenderError(error);
});
}, [
appState,
files,
exportedElements,
exportBackground,
exportPadding,
viewBackgroundColor,
]);
}, [appState, files, exportedElements, exportPadding]);

return (
<div className="ExportDialog">
Expand Down
6 changes: 5 additions & 1 deletion src/packages/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ export const exportToCanvas = ({

const max = Math.max(width, height);

const scale = maxWidthOrHeight / max;
// if content is less then maxWidthOrHeight, fallback on supplied scale
const scale =
maxWidthOrHeight < max
? maxWidthOrHeight / max
: appState?.exportScale ?? 1;

canvas.width = width * scale;
canvas.height = height * scale;
Expand Down

0 comments on commit 4d0d844

Please sign in to comment.