2020-04-08 09:49:52 -07:00
|
|
|
import {
|
|
|
|
ExcalidrawElement,
|
|
|
|
ExcalidrawTextElement,
|
|
|
|
NonDeletedExcalidrawElement,
|
|
|
|
} from "../element/types";
|
2020-01-07 19:04:52 +04:00
|
|
|
import { isTextElement } from "../element/typeChecks";
|
2020-02-19 08:25:01 -08:00
|
|
|
import {
|
|
|
|
getDiamondPoints,
|
|
|
|
getArrowPoints,
|
|
|
|
getElementAbsoluteCoords,
|
|
|
|
} from "../element/bounds";
|
2020-01-07 19:04:52 +04:00
|
|
|
import { RoughCanvas } from "roughjs/bin/canvas";
|
2020-04-09 01:46:47 -07:00
|
|
|
import { Drawable, Options } from "roughjs/bin/core";
|
2020-01-28 12:25:13 -08:00
|
|
|
import { RoughSVG } from "roughjs/bin/svg";
|
|
|
|
import { RoughGenerator } from "roughjs/bin/generator";
|
2020-02-19 08:25:01 -08:00
|
|
|
import { SceneState } from "../scene/types";
|
|
|
|
import { SVG_NS, distance } from "../utils";
|
2020-04-09 01:46:47 -07:00
|
|
|
import { isPathALoop } from "../math";
|
2020-02-19 08:25:01 -08:00
|
|
|
import rough from "roughjs/bin/rough";
|
|
|
|
|
|
|
|
const CANVAS_PADDING = 20;
|
|
|
|
|
2020-03-08 10:20:55 -07:00
|
|
|
export interface ExcalidrawElementWithCanvas {
|
|
|
|
element: ExcalidrawElement | ExcalidrawTextElement;
|
|
|
|
canvas: HTMLCanvasElement;
|
|
|
|
canvasZoom: number;
|
|
|
|
canvasOffsetX: number;
|
|
|
|
canvasOffsetY: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateElementCanvas(
|
2020-04-08 09:49:52 -07:00
|
|
|
element: NonDeletedExcalidrawElement,
|
2020-03-08 10:20:55 -07:00
|
|
|
zoom: number,
|
|
|
|
): ExcalidrawElementWithCanvas {
|
2020-02-19 08:25:01 -08:00
|
|
|
const canvas = document.createElement("canvas");
|
2020-03-08 10:20:55 -07:00
|
|
|
const context = canvas.getContext("2d")!;
|
2020-02-19 08:25:01 -08:00
|
|
|
|
|
|
|
const isLinear = /\b(arrow|line)\b/.test(element.type);
|
|
|
|
|
2020-03-08 10:20:55 -07:00
|
|
|
let canvasOffsetX = 0;
|
|
|
|
let canvasOffsetY = 0;
|
|
|
|
|
2020-02-19 08:25:01 -08:00
|
|
|
if (isLinear) {
|
|
|
|
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element);
|
|
|
|
canvas.width =
|
|
|
|
distance(x1, x2) * window.devicePixelRatio * zoom + CANVAS_PADDING * 2;
|
|
|
|
canvas.height =
|
|
|
|
distance(y1, y2) * window.devicePixelRatio * zoom + CANVAS_PADDING * 2;
|
|
|
|
|
2020-03-08 10:20:55 -07:00
|
|
|
canvasOffsetX =
|
2020-02-19 08:25:01 -08:00
|
|
|
element.x > x1
|
|
|
|
? Math.floor(distance(element.x, x1)) * window.devicePixelRatio
|
|
|
|
: 0;
|
2020-03-08 10:20:55 -07:00
|
|
|
canvasOffsetY =
|
2020-02-19 08:25:01 -08:00
|
|
|
element.y > y1
|
|
|
|
? Math.floor(distance(element.y, y1)) * window.devicePixelRatio
|
|
|
|
: 0;
|
2020-03-08 10:20:55 -07:00
|
|
|
context.translate(canvasOffsetX * zoom, canvasOffsetY * zoom);
|
2020-02-19 08:25:01 -08:00
|
|
|
} else {
|
|
|
|
canvas.width =
|
|
|
|
element.width * window.devicePixelRatio * zoom + CANVAS_PADDING * 2;
|
|
|
|
canvas.height =
|
|
|
|
element.height * window.devicePixelRatio * zoom + CANVAS_PADDING * 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
context.translate(CANVAS_PADDING, CANVAS_PADDING);
|
|
|
|
context.scale(window.devicePixelRatio * zoom, window.devicePixelRatio * zoom);
|
|
|
|
|
|
|
|
const rc = rough.canvas(canvas);
|
|
|
|
drawElementOnCanvas(element, rc, context);
|
|
|
|
context.translate(-CANVAS_PADDING, -CANVAS_PADDING);
|
2020-03-15 12:25:18 -07:00
|
|
|
context.scale(
|
|
|
|
1 / (window.devicePixelRatio * zoom),
|
|
|
|
1 / (window.devicePixelRatio * zoom),
|
|
|
|
);
|
2020-03-08 10:20:55 -07:00
|
|
|
return { element, canvas, canvasZoom: zoom, canvasOffsetX, canvasOffsetY };
|
2020-02-19 08:25:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
function drawElementOnCanvas(
|
2020-04-08 09:49:52 -07:00
|
|
|
element: NonDeletedExcalidrawElement,
|
2020-02-19 08:25:01 -08:00
|
|
|
rc: RoughCanvas,
|
|
|
|
context: CanvasRenderingContext2D,
|
|
|
|
) {
|
|
|
|
context.globalAlpha = element.opacity / 100;
|
|
|
|
switch (element.type) {
|
|
|
|
case "rectangle":
|
|
|
|
case "diamond":
|
|
|
|
case "ellipse": {
|
2020-03-08 10:20:55 -07:00
|
|
|
rc.draw(getShapeForElement(element) as Drawable);
|
2020-02-19 08:25:01 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "arrow":
|
|
|
|
case "line": {
|
2020-03-23 13:05:07 +02:00
|
|
|
(getShapeForElement(element) as Drawable[]).forEach((shape) =>
|
2020-03-08 10:20:55 -07:00
|
|
|
rc.draw(shape),
|
|
|
|
);
|
2020-02-19 08:25:01 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
if (isTextElement(element)) {
|
|
|
|
const font = context.font;
|
|
|
|
context.font = element.font;
|
|
|
|
const fillStyle = context.fillStyle;
|
|
|
|
context.fillStyle = element.strokeColor;
|
2020-04-08 21:00:27 +01:00
|
|
|
const textAlign = context.textAlign;
|
|
|
|
context.textAlign = element.textAlign as CanvasTextAlign;
|
2020-02-19 08:25:01 -08:00
|
|
|
// Canvas does not support multiline text by default
|
|
|
|
const lines = element.text.replace(/\r\n?/g, "\n").split("\n");
|
|
|
|
const lineHeight = element.height / lines.length;
|
2020-04-08 21:00:27 +01:00
|
|
|
const verticalOffset = element.height - element.baseline;
|
|
|
|
const horizontalOffset =
|
|
|
|
element.textAlign === "center"
|
|
|
|
? element.width / 2
|
|
|
|
: element.textAlign === "right"
|
|
|
|
? element.width
|
|
|
|
: 0;
|
2020-02-19 08:25:01 -08:00
|
|
|
for (let i = 0; i < lines.length; i++) {
|
2020-04-08 21:00:27 +01:00
|
|
|
context.fillText(
|
|
|
|
lines[i],
|
|
|
|
0 + horizontalOffset,
|
|
|
|
(i + 1) * lineHeight - verticalOffset,
|
|
|
|
);
|
2020-02-19 08:25:01 -08:00
|
|
|
}
|
|
|
|
context.fillStyle = fillStyle;
|
|
|
|
context.font = font;
|
2020-04-08 21:00:27 +01:00
|
|
|
context.textAlign = textAlign;
|
2020-02-19 08:25:01 -08:00
|
|
|
} else {
|
|
|
|
throw new Error(`Unimplemented type ${element.type}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
context.globalAlpha = 1;
|
|
|
|
}
|
2020-01-06 19:34:22 +04:00
|
|
|
|
2020-03-08 10:20:55 -07:00
|
|
|
const elementWithCanvasCache = new WeakMap<
|
|
|
|
ExcalidrawElement,
|
|
|
|
ExcalidrawElementWithCanvas
|
|
|
|
>();
|
|
|
|
|
|
|
|
const shapeCache = new WeakMap<
|
|
|
|
ExcalidrawElement,
|
|
|
|
Drawable | Drawable[] | null
|
|
|
|
>();
|
|
|
|
|
|
|
|
export function getShapeForElement(element: ExcalidrawElement) {
|
|
|
|
return shapeCache.get(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function invalidateShapeForElement(element: ExcalidrawElement) {
|
|
|
|
shapeCache.delete(element);
|
|
|
|
}
|
|
|
|
|
2020-01-28 12:25:13 -08:00
|
|
|
function generateElement(
|
2020-04-08 09:49:52 -07:00
|
|
|
element: NonDeletedExcalidrawElement,
|
2020-01-28 12:25:13 -08:00
|
|
|
generator: RoughGenerator,
|
2020-02-19 08:25:01 -08:00
|
|
|
sceneState?: SceneState,
|
2020-01-07 19:04:52 +04:00
|
|
|
) {
|
2020-03-08 10:20:55 -07:00
|
|
|
let shape = shapeCache.get(element) || null;
|
|
|
|
if (!shape) {
|
2020-03-08 23:35:30 -07:00
|
|
|
elementWithCanvasCache.delete(element);
|
2020-01-28 12:25:13 -08:00
|
|
|
switch (element.type) {
|
|
|
|
case "rectangle":
|
2020-03-08 10:20:55 -07:00
|
|
|
shape = generator.rectangle(0, 0, element.width, element.height, {
|
|
|
|
stroke: element.strokeColor,
|
|
|
|
fill:
|
|
|
|
element.backgroundColor === "transparent"
|
|
|
|
? undefined
|
|
|
|
: element.backgroundColor,
|
|
|
|
fillStyle: element.fillStyle,
|
|
|
|
strokeWidth: element.strokeWidth,
|
|
|
|
roughness: element.roughness,
|
|
|
|
seed: element.seed,
|
|
|
|
});
|
2020-02-19 08:25:01 -08:00
|
|
|
|
2020-01-28 12:25:13 -08:00
|
|
|
break;
|
|
|
|
case "diamond": {
|
|
|
|
const [
|
|
|
|
topX,
|
|
|
|
topY,
|
|
|
|
rightX,
|
|
|
|
rightY,
|
|
|
|
bottomX,
|
|
|
|
bottomY,
|
|
|
|
leftX,
|
|
|
|
leftY,
|
|
|
|
] = getDiamondPoints(element);
|
2020-03-08 10:20:55 -07:00
|
|
|
shape = generator.polygon(
|
2020-01-28 12:25:13 -08:00
|
|
|
[
|
|
|
|
[topX, topY],
|
|
|
|
[rightX, rightY],
|
|
|
|
[bottomX, bottomY],
|
|
|
|
[leftX, leftY],
|
|
|
|
],
|
|
|
|
{
|
|
|
|
stroke: element.strokeColor,
|
|
|
|
fill:
|
|
|
|
element.backgroundColor === "transparent"
|
|
|
|
? undefined
|
|
|
|
: element.backgroundColor,
|
|
|
|
fillStyle: element.fillStyle,
|
|
|
|
strokeWidth: element.strokeWidth,
|
|
|
|
roughness: element.roughness,
|
|
|
|
seed: element.seed,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "ellipse":
|
2020-03-08 10:20:55 -07:00
|
|
|
shape = generator.ellipse(
|
2020-01-28 12:25:13 -08:00
|
|
|
element.width / 2,
|
|
|
|
element.height / 2,
|
|
|
|
element.width,
|
|
|
|
element.height,
|
|
|
|
{
|
|
|
|
stroke: element.strokeColor,
|
|
|
|
fill:
|
|
|
|
element.backgroundColor === "transparent"
|
|
|
|
? undefined
|
|
|
|
: element.backgroundColor,
|
|
|
|
fillStyle: element.fillStyle,
|
|
|
|
strokeWidth: element.strokeWidth,
|
|
|
|
roughness: element.roughness,
|
|
|
|
seed: element.seed,
|
|
|
|
curveFitting: 1,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
break;
|
2020-02-04 13:45:22 +04:00
|
|
|
case "line":
|
2020-01-28 12:25:13 -08:00
|
|
|
case "arrow": {
|
2020-04-09 01:46:47 -07:00
|
|
|
const options: Options = {
|
2020-01-13 11:04:28 -08:00
|
|
|
stroke: element.strokeColor,
|
|
|
|
strokeWidth: element.strokeWidth,
|
|
|
|
roughness: element.roughness,
|
2020-01-24 12:04:54 +02:00
|
|
|
seed: element.seed,
|
2020-01-28 12:25:13 -08:00
|
|
|
};
|
2020-04-09 01:46:47 -07:00
|
|
|
|
2020-02-01 15:49:18 +04:00
|
|
|
// points array can be empty in the beginning, so it is important to add
|
|
|
|
// initial position to it
|
2020-03-14 21:48:51 -07:00
|
|
|
const points = element.points.length ? element.points : [[0, 0]];
|
2020-02-04 13:45:22 +04:00
|
|
|
|
2020-04-09 01:46:47 -07:00
|
|
|
// If shape is a line and is a closed shape,
|
|
|
|
// fill the shape if a color is set.
|
|
|
|
if (element.type === "line") {
|
|
|
|
if (isPathALoop(element.points)) {
|
|
|
|
options.fillStyle = element.fillStyle;
|
|
|
|
options.fill =
|
|
|
|
element.backgroundColor === "transparent"
|
|
|
|
? undefined
|
|
|
|
: element.backgroundColor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 13:45:22 +04:00
|
|
|
// curve is always the first element
|
|
|
|
// this simplifies finding the curve for an element
|
2020-03-14 21:48:51 -07:00
|
|
|
shape = [generator.curve(points as [number, number][], options)];
|
2020-02-04 13:45:22 +04:00
|
|
|
|
|
|
|
// add lines only in arrow
|
|
|
|
if (element.type === "arrow") {
|
2020-03-08 10:20:55 -07:00
|
|
|
const [x2, y2, x3, y3, x4, y4] = getArrowPoints(element, shape);
|
|
|
|
shape.push(
|
2020-02-04 13:45:22 +04:00
|
|
|
...[
|
|
|
|
generator.line(x3, y3, x2, y2, options),
|
|
|
|
generator.line(x4, y4, x2, y2, options),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
2020-01-28 12:25:13 -08:00
|
|
|
break;
|
|
|
|
}
|
2020-02-19 08:25:01 -08:00
|
|
|
case "text": {
|
|
|
|
// just to ensure we don't regenerate element.canvas on rerenders
|
2020-03-08 10:20:55 -07:00
|
|
|
shape = [];
|
2020-02-19 08:25:01 -08:00
|
|
|
break;
|
|
|
|
}
|
2020-01-12 04:00:00 +04:00
|
|
|
}
|
2020-03-08 10:20:55 -07:00
|
|
|
shapeCache.set(element, shape);
|
2020-01-28 12:25:13 -08:00
|
|
|
}
|
2020-02-19 08:25:01 -08:00
|
|
|
const zoom = sceneState ? sceneState.zoom : 1;
|
2020-03-08 10:20:55 -07:00
|
|
|
const prevElementWithCanvas = elementWithCanvasCache.get(element);
|
2020-03-28 16:59:36 -07:00
|
|
|
const shouldRegenerateBecauseZoom =
|
|
|
|
prevElementWithCanvas &&
|
|
|
|
prevElementWithCanvas.canvasZoom !== zoom &&
|
|
|
|
!sceneState?.shouldCacheIgnoreZoom;
|
|
|
|
if (!prevElementWithCanvas || shouldRegenerateBecauseZoom) {
|
2020-03-08 23:08:26 -07:00
|
|
|
const elementWithCanvas = generateElementCanvas(element, zoom);
|
|
|
|
elementWithCanvasCache.set(element, elementWithCanvas);
|
|
|
|
return elementWithCanvas;
|
2020-02-19 08:25:01 -08:00
|
|
|
}
|
2020-03-08 10:20:55 -07:00
|
|
|
return prevElementWithCanvas;
|
2020-02-19 08:25:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
function drawElementFromCanvas(
|
2020-03-08 10:20:55 -07:00
|
|
|
elementWithCanvas: ExcalidrawElementWithCanvas,
|
2020-02-19 08:25:01 -08:00
|
|
|
rc: RoughCanvas,
|
|
|
|
context: CanvasRenderingContext2D,
|
|
|
|
sceneState: SceneState,
|
|
|
|
) {
|
2020-04-02 17:40:26 +09:00
|
|
|
const element = elementWithCanvas.element;
|
|
|
|
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element);
|
|
|
|
const cx = ((x1 + x2) / 2 + sceneState.scrollX) * window.devicePixelRatio;
|
|
|
|
const cy = ((y1 + y2) / 2 + sceneState.scrollY) * window.devicePixelRatio;
|
2020-02-19 08:25:01 -08:00
|
|
|
context.scale(1 / window.devicePixelRatio, 1 / window.devicePixelRatio);
|
2020-04-02 17:40:26 +09:00
|
|
|
context.translate(cx, cy);
|
|
|
|
context.rotate(element.angle);
|
2020-02-19 08:25:01 -08:00
|
|
|
context.drawImage(
|
2020-03-08 10:20:55 -07:00
|
|
|
elementWithCanvas.canvas!,
|
2020-04-02 17:40:26 +09:00
|
|
|
(-(x2 - x1) / 2) * window.devicePixelRatio -
|
|
|
|
CANVAS_PADDING / elementWithCanvas.canvasZoom,
|
|
|
|
(-(y2 - y1) / 2) * window.devicePixelRatio -
|
|
|
|
CANVAS_PADDING / elementWithCanvas.canvasZoom,
|
2020-03-28 16:59:36 -07:00
|
|
|
elementWithCanvas.canvas!.width / elementWithCanvas.canvasZoom,
|
|
|
|
elementWithCanvas.canvas!.height / elementWithCanvas.canvasZoom,
|
2020-02-19 08:25:01 -08:00
|
|
|
);
|
2020-04-02 17:40:26 +09:00
|
|
|
context.rotate(-element.angle);
|
|
|
|
context.translate(-cx, -cy);
|
2020-02-19 08:25:01 -08:00
|
|
|
context.scale(window.devicePixelRatio, window.devicePixelRatio);
|
2020-01-28 12:25:13 -08:00
|
|
|
}
|
2020-01-07 19:04:52 +04:00
|
|
|
|
2020-01-28 12:25:13 -08:00
|
|
|
export function renderElement(
|
2020-04-08 09:49:52 -07:00
|
|
|
element: NonDeletedExcalidrawElement,
|
2020-01-28 12:25:13 -08:00
|
|
|
rc: RoughCanvas,
|
|
|
|
context: CanvasRenderingContext2D,
|
2020-02-19 08:25:01 -08:00
|
|
|
renderOptimizations: boolean,
|
|
|
|
sceneState: SceneState,
|
2020-01-28 12:25:13 -08:00
|
|
|
) {
|
|
|
|
const generator = rc.generator;
|
|
|
|
switch (element.type) {
|
|
|
|
case "selection": {
|
2020-02-19 08:25:01 -08:00
|
|
|
context.translate(
|
|
|
|
element.x + sceneState.scrollX,
|
|
|
|
element.y + sceneState.scrollY,
|
|
|
|
);
|
2020-01-28 12:25:13 -08:00
|
|
|
const fillStyle = context.fillStyle;
|
|
|
|
context.fillStyle = "rgba(0, 0, 255, 0.10)";
|
|
|
|
context.fillRect(0, 0, element.width, element.height);
|
|
|
|
context.fillStyle = fillStyle;
|
2020-03-14 17:24:28 -07:00
|
|
|
context.translate(
|
|
|
|
-element.x - sceneState.scrollX,
|
|
|
|
-element.y - sceneState.scrollY,
|
|
|
|
);
|
2020-01-28 12:25:13 -08:00
|
|
|
break;
|
2020-01-12 04:00:00 +04:00
|
|
|
}
|
2020-01-28 12:25:13 -08:00
|
|
|
case "rectangle":
|
|
|
|
case "diamond":
|
|
|
|
case "ellipse":
|
2020-02-04 13:45:22 +04:00
|
|
|
case "line":
|
2020-02-19 08:25:01 -08:00
|
|
|
case "arrow":
|
|
|
|
case "text": {
|
2020-03-08 10:20:55 -07:00
|
|
|
const elementWithCanvas = generateElement(element, generator, sceneState);
|
2020-02-19 08:25:01 -08:00
|
|
|
|
|
|
|
if (renderOptimizations) {
|
2020-03-08 10:20:55 -07:00
|
|
|
drawElementFromCanvas(elementWithCanvas, rc, context, sceneState);
|
2020-02-19 08:25:01 -08:00
|
|
|
} else {
|
2020-04-02 17:40:26 +09:00
|
|
|
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element);
|
|
|
|
const cx = (x1 + x2) / 2 + sceneState.scrollX;
|
|
|
|
const cy = (y1 + y2) / 2 + sceneState.scrollY;
|
|
|
|
const shiftX = (x2 - x1) / 2 - (element.x - x1);
|
|
|
|
const shiftY = (y2 - y1) / 2 - (element.y - y1);
|
|
|
|
context.translate(cx, cy);
|
|
|
|
context.rotate(element.angle);
|
|
|
|
context.translate(-shiftX, -shiftY);
|
2020-02-19 08:25:01 -08:00
|
|
|
drawElementOnCanvas(element, rc, context);
|
2020-04-02 17:40:26 +09:00
|
|
|
context.translate(shiftX, shiftY);
|
|
|
|
context.rotate(-element.angle);
|
|
|
|
context.translate(-cx, -cy);
|
2020-02-19 08:25:01 -08:00
|
|
|
}
|
2020-01-28 12:25:13 -08:00
|
|
|
break;
|
2020-01-15 22:07:19 +03:00
|
|
|
}
|
2020-01-28 12:25:13 -08:00
|
|
|
default: {
|
2020-03-17 20:55:40 +01:00
|
|
|
// @ts-ignore
|
2020-02-19 08:25:01 -08:00
|
|
|
throw new Error(`Unimplemented type ${element.type}`);
|
2020-01-28 12:25:13 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-15 22:07:19 +03:00
|
|
|
|
2020-01-28 12:25:13 -08:00
|
|
|
export function renderElementToSvg(
|
2020-04-08 09:49:52 -07:00
|
|
|
element: NonDeletedExcalidrawElement,
|
2020-01-28 12:25:13 -08:00
|
|
|
rsvg: RoughSVG,
|
|
|
|
svgRoot: SVGElement,
|
|
|
|
offsetX?: number,
|
|
|
|
offsetY?: number,
|
|
|
|
) {
|
2020-04-02 17:40:26 +09:00
|
|
|
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element);
|
|
|
|
const cx = (x2 - x1) / 2 - (element.x - x1);
|
|
|
|
const cy = (y2 - y1) / 2 - (element.y - y1);
|
|
|
|
const degree = (180 * element.angle) / Math.PI;
|
2020-01-28 12:25:13 -08:00
|
|
|
const generator = rsvg.generator;
|
|
|
|
switch (element.type) {
|
|
|
|
case "selection": {
|
|
|
|
// Since this is used only during editing experience, which is canvas based,
|
|
|
|
// this should not happen
|
|
|
|
throw new Error("Selection rendering is not supported for SVG");
|
|
|
|
}
|
|
|
|
case "rectangle":
|
|
|
|
case "diamond":
|
2020-02-24 21:08:13 +01:00
|
|
|
case "ellipse": {
|
|
|
|
generateElement(element, generator);
|
2020-03-08 10:20:55 -07:00
|
|
|
const node = rsvg.draw(getShapeForElement(element) as Drawable);
|
2020-02-24 21:08:13 +01:00
|
|
|
const opacity = element.opacity / 100;
|
|
|
|
if (opacity !== 1) {
|
|
|
|
node.setAttribute("stroke-opacity", `${opacity}`);
|
|
|
|
node.setAttribute("fill-opacity", `${opacity}`);
|
|
|
|
}
|
|
|
|
node.setAttribute(
|
|
|
|
"transform",
|
2020-04-02 17:40:26 +09:00
|
|
|
`translate(${offsetX || 0} ${
|
|
|
|
offsetY || 0
|
|
|
|
}) rotate(${degree} ${cx} ${cy})`,
|
2020-02-24 21:08:13 +01:00
|
|
|
);
|
|
|
|
svgRoot.appendChild(node);
|
|
|
|
break;
|
|
|
|
}
|
2020-02-24 18:30:21 +01:00
|
|
|
case "line":
|
2020-01-28 12:25:13 -08:00
|
|
|
case "arrow": {
|
|
|
|
generateElement(element, generator);
|
|
|
|
const group = svgRoot.ownerDocument!.createElementNS(SVG_NS, "g");
|
|
|
|
const opacity = element.opacity / 100;
|
2020-03-23 13:05:07 +02:00
|
|
|
(getShapeForElement(element) as Drawable[]).forEach((shape) => {
|
2020-01-28 12:25:13 -08:00
|
|
|
const node = rsvg.draw(shape);
|
|
|
|
if (opacity !== 1) {
|
|
|
|
node.setAttribute("stroke-opacity", `${opacity}`);
|
|
|
|
node.setAttribute("fill-opacity", `${opacity}`);
|
|
|
|
}
|
|
|
|
node.setAttribute(
|
|
|
|
"transform",
|
2020-04-02 17:40:26 +09:00
|
|
|
`translate(${offsetX || 0} ${
|
|
|
|
offsetY || 0
|
|
|
|
}) rotate(${degree} ${cx} ${cy})`,
|
2020-01-28 12:25:13 -08:00
|
|
|
);
|
2020-04-12 16:03:49 +09:00
|
|
|
if (
|
|
|
|
element.type === "line" &&
|
|
|
|
isPathALoop(element.points) &&
|
|
|
|
element.backgroundColor !== "transparent"
|
|
|
|
) {
|
2020-04-12 11:46:46 +09:00
|
|
|
node.setAttribute("fill-rule", "evenodd");
|
|
|
|
}
|
2020-01-28 12:25:13 -08:00
|
|
|
group.appendChild(node);
|
|
|
|
});
|
|
|
|
svgRoot.appendChild(group);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
if (isTextElement(element)) {
|
|
|
|
const opacity = element.opacity / 100;
|
|
|
|
const node = svgRoot.ownerDocument!.createElementNS(SVG_NS, "g");
|
|
|
|
if (opacity !== 1) {
|
|
|
|
node.setAttribute("stroke-opacity", `${opacity}`);
|
|
|
|
node.setAttribute("fill-opacity", `${opacity}`);
|
|
|
|
}
|
|
|
|
node.setAttribute(
|
|
|
|
"transform",
|
2020-04-02 17:40:26 +09:00
|
|
|
`translate(${offsetX || 0} ${
|
|
|
|
offsetY || 0
|
|
|
|
}) rotate(${degree} ${cx} ${cy})`,
|
2020-01-28 12:25:13 -08:00
|
|
|
);
|
|
|
|
const lines = element.text.replace(/\r\n?/g, "\n").split("\n");
|
|
|
|
const lineHeight = element.height / lines.length;
|
2020-04-12 01:17:20 +09:00
|
|
|
const verticalOffset = element.height - element.baseline;
|
|
|
|
const horizontalOffset =
|
|
|
|
element.textAlign === "center"
|
|
|
|
? element.width / 2
|
|
|
|
: element.textAlign === "right"
|
|
|
|
? element.width
|
|
|
|
: 0;
|
2020-03-23 13:05:07 +02:00
|
|
|
const fontSplit = element.font.split(" ").filter((d) => !!d.trim());
|
2020-01-28 12:25:13 -08:00
|
|
|
let fontFamily = fontSplit[0];
|
|
|
|
let fontSize = "20px";
|
|
|
|
if (fontSplit.length > 1) {
|
|
|
|
fontFamily = fontSplit[1];
|
|
|
|
fontSize = fontSplit[0];
|
|
|
|
}
|
2020-04-12 01:17:20 +09:00
|
|
|
const textAnchor =
|
|
|
|
element.textAlign === "center"
|
|
|
|
? "middle"
|
|
|
|
: element.textAlign === "right"
|
|
|
|
? "end"
|
|
|
|
: "start";
|
2020-01-28 12:25:13 -08:00
|
|
|
for (let i = 0; i < lines.length; i++) {
|
|
|
|
const text = svgRoot.ownerDocument!.createElementNS(SVG_NS, "text");
|
|
|
|
text.textContent = lines[i];
|
2020-04-12 01:17:20 +09:00
|
|
|
text.setAttribute("x", `${horizontalOffset}`);
|
|
|
|
text.setAttribute("y", `${(i + 1) * lineHeight - verticalOffset}`);
|
2020-01-28 12:25:13 -08:00
|
|
|
text.setAttribute("font-family", fontFamily);
|
|
|
|
text.setAttribute("font-size", fontSize);
|
|
|
|
text.setAttribute("fill", element.strokeColor);
|
2020-04-12 01:17:20 +09:00
|
|
|
text.setAttribute("text-anchor", textAnchor);
|
2020-01-28 12:25:13 -08:00
|
|
|
node.appendChild(text);
|
|
|
|
}
|
|
|
|
svgRoot.appendChild(node);
|
|
|
|
} else {
|
2020-03-17 20:55:40 +01:00
|
|
|
// @ts-ignore
|
2020-02-02 20:04:35 +02:00
|
|
|
throw new Error(`Unimplemented type ${element.type}`);
|
2020-01-28 12:25:13 -08:00
|
|
|
}
|
2020-01-24 10:35:51 -08:00
|
|
|
}
|
2020-01-06 19:34:22 +04:00
|
|
|
}
|
|
|
|
}
|