2020-01-28 12:25:13 -08:00
|
|
|
import rough from "roughjs/bin/rough";
|
2020-04-08 09:49:52 -07:00
|
|
|
import { NonDeletedExcalidrawElement } from "../element/types";
|
2020-01-28 12:25:13 -08:00
|
|
|
import { getCommonBounds } from "../element/bounds";
|
|
|
|
import { renderScene, renderSceneToSvg } from "../renderer/renderScene";
|
2021-10-21 22:05:48 +02:00
|
|
|
import { distance } from "../utils";
|
|
|
|
import { AppState, BinaryFiles } from "../types";
|
|
|
|
import { DEFAULT_EXPORT_PADDING, SVG_NS, THEME_FILTER } from "../constants";
|
2020-11-04 17:49:15 +00:00
|
|
|
import { getDefaultAppState } from "../appState";
|
2021-07-03 02:07:01 +05:30
|
|
|
import { serializeAsJSON } from "../data/json";
|
2021-10-21 22:05:48 +02:00
|
|
|
import {
|
|
|
|
getInitializedImageElements,
|
|
|
|
updateImageCache,
|
|
|
|
} from "../element/image";
|
2020-01-28 12:25:13 -08:00
|
|
|
|
2020-04-06 23:02:17 +02:00
|
|
|
export const SVG_EXPORT_TAG = `<!-- svg-source:excalidraw -->`;
|
|
|
|
|
2021-10-21 22:05:48 +02:00
|
|
|
export const exportToCanvas = async (
|
2020-04-08 09:49:52 -07:00
|
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
2020-03-08 10:20:55 -07:00
|
|
|
appState: AppState,
|
2021-10-21 22:05:48 +02:00
|
|
|
files: BinaryFiles,
|
2020-01-28 12:25:13 -08:00
|
|
|
{
|
|
|
|
exportBackground,
|
2021-05-30 15:31:12 +01:00
|
|
|
exportPadding = DEFAULT_EXPORT_PADDING,
|
2020-01-28 12:25:13 -08:00
|
|
|
viewBackgroundColor,
|
|
|
|
}: {
|
|
|
|
exportBackground: boolean;
|
|
|
|
exportPadding?: number;
|
|
|
|
viewBackgroundColor: string;
|
|
|
|
},
|
2021-02-04 14:54:08 +01:00
|
|
|
createCanvas: (
|
|
|
|
width: number,
|
|
|
|
height: number,
|
|
|
|
) => { canvas: HTMLCanvasElement; scale: number } = (width, height) => {
|
2021-05-30 15:31:12 +01:00
|
|
|
const canvas = document.createElement("canvas");
|
|
|
|
canvas.width = width * appState.exportScale;
|
|
|
|
canvas.height = height * appState.exportScale;
|
|
|
|
return { canvas, scale: appState.exportScale };
|
2020-01-28 12:25:13 -08:00
|
|
|
},
|
2020-05-20 16:21:37 +03:00
|
|
|
) => {
|
2021-05-26 21:44:54 +02:00
|
|
|
const [minX, minY, width, height] = getCanvasSize(elements, exportPadding);
|
2020-01-28 12:25:13 -08:00
|
|
|
|
2021-05-30 15:31:12 +01:00
|
|
|
const { canvas, scale = 1 } = createCanvas(width, height);
|
2020-01-28 12:25:13 -08:00
|
|
|
|
2021-10-21 22:05:48 +02:00
|
|
|
const defaultAppState = getDefaultAppState();
|
|
|
|
|
|
|
|
const { imageCache } = await updateImageCache({
|
|
|
|
imageCache: new Map(),
|
|
|
|
fileIds: getInitializedImageElements(elements).map(
|
|
|
|
(element) => element.fileId,
|
|
|
|
),
|
|
|
|
files,
|
|
|
|
});
|
|
|
|
|
2021-11-25 14:05:22 +01:00
|
|
|
renderScene(elements, appState, null, scale, rough.canvas(canvas), canvas, {
|
|
|
|
viewBackgroundColor: exportBackground ? viewBackgroundColor : null,
|
|
|
|
scrollX: -minX + exportPadding,
|
|
|
|
scrollY: -minY + exportPadding,
|
|
|
|
zoom: defaultAppState.zoom,
|
|
|
|
remotePointerViewportCoords: {},
|
|
|
|
remoteSelectedElementIds: {},
|
|
|
|
shouldCacheIgnoreZoom: false,
|
|
|
|
remotePointerUsernames: {},
|
|
|
|
remotePointerUserStates: {},
|
|
|
|
theme: appState.exportWithDarkMode ? "dark" : "light",
|
|
|
|
imageCache,
|
|
|
|
renderScrollbars: false,
|
|
|
|
renderSelection: false,
|
|
|
|
renderGrid: false,
|
|
|
|
isExporting: true,
|
|
|
|
});
|
2020-04-19 20:50:23 +01:00
|
|
|
|
2021-05-30 15:31:12 +01:00
|
|
|
return canvas;
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-01-28 12:25:13 -08:00
|
|
|
|
2021-07-03 02:07:01 +05:30
|
|
|
export const exportToSvg = async (
|
2020-04-08 09:49:52 -07:00
|
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
2021-07-03 02:07:01 +05:30
|
|
|
appState: {
|
2020-01-28 12:25:13 -08:00
|
|
|
exportBackground: boolean;
|
|
|
|
exportPadding?: number;
|
2021-05-30 15:31:12 +01:00
|
|
|
exportScale?: number;
|
2020-01-28 12:25:13 -08:00
|
|
|
viewBackgroundColor: string;
|
2021-02-24 19:52:17 +05:30
|
|
|
exportWithDarkMode?: boolean;
|
2021-07-03 02:07:01 +05:30
|
|
|
exportEmbedScene?: boolean;
|
2020-01-28 12:25:13 -08:00
|
|
|
},
|
2021-10-21 22:05:48 +02:00
|
|
|
files: BinaryFiles | null,
|
2021-07-03 02:07:01 +05:30
|
|
|
): Promise<SVGSVGElement> => {
|
|
|
|
const {
|
|
|
|
exportPadding = DEFAULT_EXPORT_PADDING,
|
|
|
|
viewBackgroundColor,
|
|
|
|
exportScale = 1,
|
|
|
|
exportEmbedScene,
|
|
|
|
} = appState;
|
|
|
|
let metadata = "";
|
|
|
|
if (exportEmbedScene) {
|
|
|
|
try {
|
|
|
|
metadata = await (
|
|
|
|
await import(/* webpackChunkName: "image" */ "../../src/data/image")
|
|
|
|
).encodeSvgMetadata({
|
2021-10-21 22:05:48 +02:00
|
|
|
text: serializeAsJSON(elements, appState, files || {}, "local"),
|
2021-07-03 02:07:01 +05:30
|
|
|
});
|
2021-11-02 14:24:16 +02:00
|
|
|
} catch (error: any) {
|
|
|
|
console.error(error);
|
2021-07-03 02:07:01 +05:30
|
|
|
}
|
|
|
|
}
|
2021-05-26 21:44:54 +02:00
|
|
|
const [minX, minY, width, height] = getCanvasSize(elements, exportPadding);
|
2020-01-28 12:25:13 -08:00
|
|
|
|
2022-03-02 01:07:12 -05:00
|
|
|
// initialize SVG root
|
2020-01-28 12:25:13 -08:00
|
|
|
const svgRoot = document.createElementNS(SVG_NS, "svg");
|
|
|
|
svgRoot.setAttribute("version", "1.1");
|
|
|
|
svgRoot.setAttribute("xmlns", SVG_NS);
|
|
|
|
svgRoot.setAttribute("viewBox", `0 0 ${width} ${height}`);
|
2021-05-30 15:31:12 +01:00
|
|
|
svgRoot.setAttribute("width", `${width * exportScale}`);
|
|
|
|
svgRoot.setAttribute("height", `${height * exportScale}`);
|
2021-07-03 02:07:01 +05:30
|
|
|
if (appState.exportWithDarkMode) {
|
2021-03-13 18:58:06 +05:30
|
|
|
svgRoot.setAttribute("filter", THEME_FILTER);
|
2021-02-24 19:52:17 +05:30
|
|
|
}
|
2020-01-28 12:25:13 -08:00
|
|
|
|
2022-04-28 20:19:41 +05:30
|
|
|
let assetPath = "https://excalidraw.com/";
|
|
|
|
|
|
|
|
// Asset path needs to be determined only when using package
|
|
|
|
if (process.env.IS_EXCALIDRAW_NPM_PACKAGE) {
|
|
|
|
assetPath =
|
|
|
|
window.EXCALIDRAW_ASSET_PATH ||
|
|
|
|
`https://unpkg.com/${process.env.PKG_NAME}@${process.env.PKG_VERSION}`;
|
|
|
|
|
|
|
|
if (assetPath?.startsWith("/")) {
|
|
|
|
assetPath = assetPath.replace("/", `${window.location.origin}/`);
|
|
|
|
}
|
|
|
|
assetPath = `${assetPath}/dist/excalidraw-assets/`;
|
|
|
|
}
|
2020-01-28 12:25:13 -08:00
|
|
|
svgRoot.innerHTML = `
|
2020-04-06 23:02:17 +02:00
|
|
|
${SVG_EXPORT_TAG}
|
2020-10-13 14:47:07 +02:00
|
|
|
${metadata}
|
2020-01-28 12:25:13 -08:00
|
|
|
<defs>
|
|
|
|
<style>
|
|
|
|
@font-face {
|
|
|
|
font-family: "Virgil";
|
2022-04-28 20:19:41 +05:30
|
|
|
src: url("${assetPath}Virgil.woff2");
|
2020-01-28 12:25:13 -08:00
|
|
|
}
|
|
|
|
@font-face {
|
|
|
|
font-family: "Cascadia";
|
2022-04-28 20:19:41 +05:30
|
|
|
src: url("${assetPath}Cascadia.woff2");
|
2020-01-28 12:25:13 -08:00
|
|
|
}
|
|
|
|
</style>
|
|
|
|
</defs>
|
|
|
|
`;
|
2020-11-02 20:14:20 +01:00
|
|
|
// render background rect
|
2021-07-03 02:07:01 +05:30
|
|
|
if (appState.exportBackground && viewBackgroundColor) {
|
2020-01-28 12:25:13 -08:00
|
|
|
const rect = svgRoot.ownerDocument!.createElementNS(SVG_NS, "rect");
|
|
|
|
rect.setAttribute("x", "0");
|
|
|
|
rect.setAttribute("y", "0");
|
|
|
|
rect.setAttribute("width", `${width}`);
|
|
|
|
rect.setAttribute("height", `${height}`);
|
|
|
|
rect.setAttribute("fill", viewBackgroundColor);
|
|
|
|
svgRoot.appendChild(rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
const rsvg = rough.svg(svgRoot);
|
2021-10-21 22:05:48 +02:00
|
|
|
renderSceneToSvg(elements, rsvg, svgRoot, files || {}, {
|
2020-01-28 12:25:13 -08:00
|
|
|
offsetX: -minX + exportPadding,
|
|
|
|
offsetY: -minY + exportPadding,
|
2021-11-20 15:31:51 +01:00
|
|
|
exportWithDarkMode: appState.exportWithDarkMode,
|
2020-01-28 12:25:13 -08:00
|
|
|
});
|
2020-04-19 20:50:23 +01:00
|
|
|
|
2020-01-28 12:25:13 -08:00
|
|
|
return svgRoot;
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-04-19 20:50:23 +01:00
|
|
|
|
2020-10-07 06:37:38 -07:00
|
|
|
// calculate smallest area to fit the contents in
|
|
|
|
const getCanvasSize = (
|
|
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
|
|
|
exportPadding: number,
|
|
|
|
): [number, number, number, number] => {
|
|
|
|
const [minX, minY, maxX, maxY] = getCommonBounds(elements);
|
|
|
|
const width = distance(minX, maxX) + exportPadding * 2;
|
2021-05-26 21:44:54 +02:00
|
|
|
const height = distance(minY, maxY) + exportPadding + exportPadding;
|
2020-10-07 06:37:38 -07:00
|
|
|
|
|
|
|
return [minX, minY, width, height];
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getExportSize = (
|
|
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
|
|
|
exportPadding: number,
|
|
|
|
scale: number,
|
|
|
|
): [number, number] => {
|
2021-11-01 15:24:05 +02:00
|
|
|
const [, , width, height] = getCanvasSize(elements, exportPadding).map(
|
|
|
|
(dimension) => Math.trunc(dimension * scale),
|
|
|
|
);
|
2020-10-07 06:37:38 -07:00
|
|
|
|
|
|
|
return [width, height];
|
|
|
|
};
|