diff --git a/src/scene/export.ts b/src/scene/export.ts index a6e4297ad..b9924fc27 100644 --- a/src/scene/export.ts +++ b/src/scene/export.ts @@ -14,6 +14,18 @@ import { export const SVG_EXPORT_TAG = ``; +const createScene = ( + elements: readonly NonDeletedExcalidrawElement[], +): Scene | null => { + if (!elements || Scene.getScene(elements[0])) { + return null; + } + const scene = new Scene(); + scene.replaceAllElements(elements); + elements?.forEach((el) => Scene.mapElementToScene(el, scene)); + return scene; +}; + export const exportToCanvas = async ( elements: readonly NonDeletedExcalidrawElement[], appState: AppState, @@ -37,6 +49,7 @@ export const exportToCanvas = async ( return { canvas, scale: appState.exportScale }; }, ) => { + const scene = createScene(elements); const [minX, minY, width, height] = getCanvasSize(elements, exportPadding); const { canvas, scale = 1 } = createCanvas(width, height); @@ -76,6 +89,7 @@ export const exportToCanvas = async ( }, }); + scene?.destroy(); return canvas; }; @@ -97,6 +111,7 @@ export const exportToSvg = async ( exportScale = 1, exportEmbedScene, } = appState; + const scene = createScene(elements); let metadata = ""; if (exportEmbedScene) { try { @@ -169,6 +184,7 @@ export const exportToSvg = async ( exportWithDarkMode: appState.exportWithDarkMode, }); + scene?.destroy(); return svgRoot; };