2020-01-28 12:25:13 -08:00
|
|
|
import rough from "roughjs/bin/rough";
|
|
|
|
import { ExcalidrawElement } from "../element/types";
|
|
|
|
import { getCommonBounds } from "../element/bounds";
|
|
|
|
import { renderScene, renderSceneToSvg } from "../renderer/renderScene";
|
|
|
|
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-01-28 12:25:13 -08:00
|
|
|
|
2020-04-06 23:02:17 +02:00
|
|
|
export const SVG_EXPORT_TAG = `<!-- svg-source:excalidraw -->`;
|
|
|
|
|
2020-01-28 12:25:13 -08:00
|
|
|
export function exportToCanvas(
|
|
|
|
elements: readonly ExcalidrawElement[],
|
2020-03-08 10:20:55 -07:00
|
|
|
appState: AppState,
|
2020-01-28 12:25:13 -08:00
|
|
|
{
|
|
|
|
exportBackground,
|
|
|
|
exportPadding = 10,
|
|
|
|
viewBackgroundColor,
|
|
|
|
scale = 1,
|
|
|
|
}: {
|
|
|
|
exportBackground: boolean;
|
|
|
|
exportPadding?: number;
|
|
|
|
scale?: number;
|
|
|
|
viewBackgroundColor: string;
|
|
|
|
},
|
2020-03-23 13:05:07 +02:00
|
|
|
createCanvas: (width: number, height: number) => any = function (
|
2020-01-28 12:25:13 -08:00
|
|
|
width,
|
|
|
|
height,
|
|
|
|
) {
|
|
|
|
const tempCanvas = document.createElement("canvas");
|
|
|
|
tempCanvas.width = width * scale;
|
|
|
|
tempCanvas.height = height * scale;
|
|
|
|
return tempCanvas;
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
// calculate smallest area to fit the contents in
|
|
|
|
const [minX, minY, maxX, maxY] = getCommonBounds(elements);
|
|
|
|
const width = distance(minX, maxX) + exportPadding * 2;
|
|
|
|
const height = distance(minY, maxY) + exportPadding * 2;
|
|
|
|
|
|
|
|
const tempCanvas: any = createCanvas(width, height);
|
|
|
|
|
|
|
|
renderScene(
|
|
|
|
elements,
|
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-02-15 21:03:32 +01:00
|
|
|
zoom: 1,
|
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-01-28 12:25:13 -08:00
|
|
|
},
|
|
|
|
);
|
|
|
|
return tempCanvas;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function exportToSvg(
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
{
|
|
|
|
exportBackground,
|
|
|
|
exportPadding = 10,
|
|
|
|
viewBackgroundColor,
|
|
|
|
}: {
|
|
|
|
exportBackground: boolean;
|
|
|
|
exportPadding?: number;
|
|
|
|
viewBackgroundColor: string;
|
|
|
|
},
|
|
|
|
): SVGSVGElement {
|
|
|
|
// calculate canvas dimensions
|
|
|
|
const [minX, minY, maxX, maxY] = getCommonBounds(elements);
|
|
|
|
const width = distance(minX, maxX) + exportPadding * 2;
|
|
|
|
const height = distance(minY, maxY) + exportPadding * 2;
|
|
|
|
|
|
|
|
// 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}`);
|
|
|
|
|
|
|
|
svgRoot.innerHTML = `
|
2020-04-06 23:02:17 +02:00
|
|
|
${SVG_EXPORT_TAG}
|
2020-01-28 12:25:13 -08:00
|
|
|
<defs>
|
|
|
|
<style>
|
|
|
|
@font-face {
|
|
|
|
font-family: "Virgil";
|
|
|
|
src: url("https://excalidraw.com/FG_Virgil.ttf");
|
|
|
|
}
|
|
|
|
@font-face {
|
|
|
|
font-family: "Cascadia";
|
|
|
|
src: url("https://excalidraw.com/Cascadia.ttf");
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
</defs>
|
|
|
|
`;
|
|
|
|
|
|
|
|
// render backgroiund rect
|
|
|
|
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);
|
|
|
|
renderSceneToSvg(elements, rsvg, svgRoot, {
|
|
|
|
offsetX: -minX + exportPadding,
|
|
|
|
offsetY: -minY + exportPadding,
|
|
|
|
});
|
|
|
|
return svgRoot;
|
|
|
|
}
|