2020-01-28 12:25:13 -08:00
|
|
|
import rough from "roughjs/bin/rough";
|
2020-04-19 20:50:23 +01:00
|
|
|
import oc from "open-color";
|
|
|
|
import { newTextElement } from "../element";
|
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";
|
2020-06-25 21:21:27 +02:00
|
|
|
import { distance, SVG_NS } from "../utils";
|
2020-03-07 10:20:38 -05:00
|
|
|
import { normalizeScroll } from "./scroll";
|
2020-03-08 10:20:55 -07:00
|
|
|
import { AppState } from "../types";
|
2020-04-19 20:50:23 +01:00
|
|
|
import { t } from "../i18n";
|
2020-06-25 21:21:27 +02:00
|
|
|
import { DEFAULT_FONT_FAMILY, DEFAULT_VERTICAL_ALIGN } from "../constants";
|
2020-11-04 17:49:15 +00:00
|
|
|
import { getDefaultAppState } from "../appState";
|
2020-01-28 12:25:13 -08:00
|
|
|
|
2020-04-06 23:02:17 +02:00
|
|
|
export const SVG_EXPORT_TAG = `<!-- svg-source:excalidraw -->`;
|
2020-10-07 06:37:38 -07:00
|
|
|
const WATERMARK_HEIGHT = 16;
|
2020-04-06 23:02:17 +02:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const exportToCanvas = (
|
2020-04-08 09:49:52 -07:00
|
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
2020-03-08 10:20:55 -07:00
|
|
|
appState: AppState,
|
2020-01-28 12:25:13 -08:00
|
|
|
{
|
|
|
|
exportBackground,
|
|
|
|
exportPadding = 10,
|
|
|
|
viewBackgroundColor,
|
|
|
|
scale = 1,
|
2020-04-19 20:50:23 +01:00
|
|
|
shouldAddWatermark,
|
2020-01-28 12:25:13 -08:00
|
|
|
}: {
|
|
|
|
exportBackground: boolean;
|
|
|
|
exportPadding?: number;
|
|
|
|
scale?: number;
|
|
|
|
viewBackgroundColor: string;
|
2020-04-19 20:50:23 +01:00
|
|
|
shouldAddWatermark: boolean;
|
2020-01-28 12:25:13 -08:00
|
|
|
},
|
2020-10-13 14:47:07 +02:00
|
|
|
createCanvas: (width: number, height: number) => HTMLCanvasElement = (
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
) => {
|
2020-01-28 12:25:13 -08:00
|
|
|
const tempCanvas = document.createElement("canvas");
|
|
|
|
tempCanvas.width = width * scale;
|
|
|
|
tempCanvas.height = height * scale;
|
|
|
|
return tempCanvas;
|
|
|
|
},
|
2020-05-20 16:21:37 +03:00
|
|
|
) => {
|
2020-10-07 06:37:38 -07:00
|
|
|
const sceneElements = getElementsAndWatermark(elements, shouldAddWatermark);
|
2020-04-19 20:50:23 +01:00
|
|
|
|
2020-10-07 06:37:38 -07:00
|
|
|
const [minX, minY, width, height] = getCanvasSize(
|
|
|
|
sceneElements,
|
|
|
|
exportPadding,
|
|
|
|
shouldAddWatermark,
|
|
|
|
);
|
2020-01-28 12:25:13 -08:00
|
|
|
|
2020-10-13 14:47:07 +02:00
|
|
|
const tempCanvas = createCanvas(width, height);
|
2020-01-28 12:25:13 -08:00
|
|
|
|
|
|
|
renderScene(
|
2020-04-19 20:50:23 +01:00
|
|
|
sceneElements,
|
2020-03-08 10:20:55 -07:00
|
|
|
appState,
|
2020-02-05 22:47:10 +04:00
|
|
|
null,
|
2020-03-14 17:24:28 -07:00
|
|
|
scale,
|
2020-01-28 12:25:13 -08:00
|
|
|
rough.canvas(tempCanvas),
|
|
|
|
tempCanvas,
|
|
|
|
{
|
|
|
|
viewBackgroundColor: exportBackground ? viewBackgroundColor : null,
|
2020-02-19 08:25:01 -08:00
|
|
|
scrollX: normalizeScroll(-minX + exportPadding),
|
|
|
|
scrollY: normalizeScroll(-minY + exportPadding),
|
2020-11-04 17:49:15 +00:00
|
|
|
zoom: getDefaultAppState().zoom,
|
basic Socket.io implementation of collaborative editing (#879)
* Enable collaborative syncing for elements
* Don't fall back to local storage if using a room, as that is confusing
* Use remote socket server
* Send updates to new users when they join
* ~
* add mouse tracking
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* Add Live button and app state to support tracking collaborator counts
* Enable collaborative syncing for elements
* add mouse tracking
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* fix syncing bugs and add a button to start syncing mid session
* Add Live button and app state to support tracking collaborator counts
* prettier
* Fix bug with remote pointers not changing on scroll
* Enable collaborative syncing for elements
* add mouse tracking
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* Add Live button and app state to support tracking collaborator counts
* enable collaboration, rooms, and mouse tracking
* fix syncing bugs and add a button to start syncing mid session
* fix syncing bugs and add a button to start syncing mid session
* Fix bug with remote pointers not changing on scroll
* remove UI for collaboration
* remove link
* clean up lingering unused UI
* set random IV passed per encrypted message, reduce room id length, refactored socket broadcasting API, rename room_id to room, removed throttling of pointer movement
* fix package.json conflict
2020-03-09 08:48:25 -07:00
|
|
|
remotePointerViewportCoords: {},
|
2020-04-04 16:02:16 +02:00
|
|
|
remoteSelectedElementIds: {},
|
2020-03-28 16:59:36 -07:00
|
|
|
shouldCacheIgnoreZoom: false,
|
2020-04-07 14:02:42 +01:00
|
|
|
remotePointerUsernames: {},
|
2020-01-28 12:25:13 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
renderScrollbars: false,
|
|
|
|
renderSelection: false,
|
2020-02-19 08:25:01 -08:00
|
|
|
renderOptimizations: false,
|
2020-06-24 17:16:03 +09:00
|
|
|
renderGrid: false,
|
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 tempCanvas;
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-01-28 12:25:13 -08:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const exportToSvg = (
|
2020-04-08 09:49:52 -07:00
|
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
2020-01-28 12:25:13 -08:00
|
|
|
{
|
|
|
|
exportBackground,
|
|
|
|
exportPadding = 10,
|
|
|
|
viewBackgroundColor,
|
2020-10-28 17:10:22 +00:00
|
|
|
scale = 1,
|
2020-04-19 20:50:23 +01:00
|
|
|
shouldAddWatermark,
|
2020-10-13 14:47:07 +02:00
|
|
|
metadata = "",
|
2020-01-28 12:25:13 -08:00
|
|
|
}: {
|
|
|
|
exportBackground: boolean;
|
|
|
|
exportPadding?: number;
|
2020-10-28 17:10:22 +00:00
|
|
|
scale?: number;
|
2020-01-28 12:25:13 -08:00
|
|
|
viewBackgroundColor: string;
|
2020-04-19 20:50:23 +01:00
|
|
|
shouldAddWatermark: boolean;
|
2020-10-13 14:47:07 +02:00
|
|
|
metadata?: string;
|
2020-01-28 12:25:13 -08:00
|
|
|
},
|
2020-05-20 16:21:37 +03:00
|
|
|
): SVGSVGElement => {
|
2020-10-07 06:37:38 -07:00
|
|
|
const sceneElements = getElementsAndWatermark(elements, shouldAddWatermark);
|
2020-04-19 20:50:23 +01:00
|
|
|
|
2020-10-07 06:37:38 -07:00
|
|
|
const [minX, minY, width, height] = getCanvasSize(
|
|
|
|
sceneElements,
|
|
|
|
exportPadding,
|
|
|
|
shouldAddWatermark,
|
|
|
|
);
|
2020-01-28 12:25:13 -08:00
|
|
|
|
|
|
|
// initialze SVG root
|
|
|
|
const svgRoot = document.createElementNS(SVG_NS, "svg");
|
|
|
|
svgRoot.setAttribute("version", "1.1");
|
|
|
|
svgRoot.setAttribute("xmlns", SVG_NS);
|
|
|
|
svgRoot.setAttribute("viewBox", `0 0 ${width} ${height}`);
|
2020-10-28 17:10:22 +00:00
|
|
|
svgRoot.setAttribute("width", `${width * scale}`);
|
|
|
|
svgRoot.setAttribute("height", `${height * scale}`);
|
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";
|
2020-04-13 21:53:07 +02:00
|
|
|
src: url("https://excalidraw.com/FG_Virgil.woff2");
|
2020-01-28 12:25:13 -08:00
|
|
|
}
|
|
|
|
@font-face {
|
|
|
|
font-family: "Cascadia";
|
2020-04-13 21:53:07 +02:00
|
|
|
src: url("https://excalidraw.com/Cascadia.woff2");
|
2020-01-28 12:25:13 -08:00
|
|
|
}
|
|
|
|
</style>
|
|
|
|
</defs>
|
|
|
|
`;
|
|
|
|
|
2020-11-02 20:14:20 +01:00
|
|
|
// render background rect
|
2020-01-28 12:25:13 -08:00
|
|
|
if (exportBackground && viewBackgroundColor) {
|
|
|
|
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);
|
2020-04-19 20:50:23 +01:00
|
|
|
renderSceneToSvg(sceneElements, rsvg, svgRoot, {
|
2020-01-28 12:25:13 -08:00
|
|
|
offsetX: -minX + exportPadding,
|
|
|
|
offsetY: -minY + exportPadding,
|
|
|
|
});
|
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
|
|
|
const getElementsAndWatermark = (
|
|
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
|
|
|
shouldAddWatermark: boolean,
|
|
|
|
): readonly NonDeletedExcalidrawElement[] => {
|
|
|
|
let _elements = [...elements];
|
|
|
|
|
|
|
|
if (shouldAddWatermark) {
|
|
|
|
const [, , maxX, maxY] = getCommonBounds(elements);
|
|
|
|
_elements = [..._elements, getWatermarkElement(maxX, maxY)];
|
|
|
|
}
|
|
|
|
|
|
|
|
return _elements;
|
|
|
|
};
|
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
const getWatermarkElement = (maxX: number, maxY: number) => {
|
2020-04-19 20:50:23 +01:00
|
|
|
return newTextElement({
|
2020-06-25 21:21:27 +02:00
|
|
|
text: t("labels.madeWithExcalidraw"),
|
2020-10-07 06:37:38 -07:00
|
|
|
fontSize: WATERMARK_HEIGHT,
|
2020-06-25 21:21:27 +02:00
|
|
|
fontFamily: DEFAULT_FONT_FAMILY,
|
|
|
|
textAlign: "right",
|
|
|
|
verticalAlign: DEFAULT_VERTICAL_ALIGN,
|
|
|
|
x: maxX,
|
2020-10-07 06:37:38 -07:00
|
|
|
y: maxY + WATERMARK_HEIGHT,
|
2020-04-19 20:50:23 +01:00
|
|
|
strokeColor: oc.gray[5],
|
|
|
|
backgroundColor: "transparent",
|
|
|
|
fillStyle: "hachure",
|
|
|
|
strokeWidth: 1,
|
2020-05-14 17:04:33 +02:00
|
|
|
strokeStyle: "solid",
|
2020-04-19 20:50:23 +01:00
|
|
|
roughness: 1,
|
|
|
|
opacity: 100,
|
2020-08-15 00:59:43 +09:00
|
|
|
strokeSharpness: "sharp",
|
2020-04-19 20:50:23 +01:00
|
|
|
});
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-10-07 06:37:38 -07:00
|
|
|
|
|
|
|
// calculate smallest area to fit the contents in
|
|
|
|
const getCanvasSize = (
|
|
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
|
|
|
exportPadding: number,
|
|
|
|
shouldAddWatermark: boolean,
|
|
|
|
): [number, number, number, number] => {
|
|
|
|
const [minX, minY, maxX, maxY] = getCommonBounds(elements);
|
|
|
|
const width = distance(minX, maxX) + exportPadding * 2;
|
|
|
|
const height =
|
|
|
|
distance(minY, maxY) +
|
|
|
|
exportPadding +
|
|
|
|
(shouldAddWatermark ? 0 : exportPadding);
|
|
|
|
|
|
|
|
return [minX, minY, width, height];
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getExportSize = (
|
|
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
|
|
|
exportPadding: number,
|
|
|
|
shouldAddWatermark: boolean,
|
|
|
|
scale: number,
|
|
|
|
): [number, number] => {
|
|
|
|
const sceneElements = getElementsAndWatermark(elements, shouldAddWatermark);
|
|
|
|
|
|
|
|
const [, , width, height] = getCanvasSize(
|
|
|
|
sceneElements,
|
|
|
|
exportPadding,
|
|
|
|
shouldAddWatermark,
|
|
|
|
).map((dimension) => Math.trunc(dimension * scale));
|
|
|
|
|
|
|
|
return [width, height];
|
|
|
|
};
|