2020-04-08 09:49:52 -07:00
|
|
|
import {
|
|
|
|
ExcalidrawElement,
|
|
|
|
ExcalidrawTextElement,
|
|
|
|
NonDeletedExcalidrawElement,
|
|
|
|
} from "../element/types";
|
2020-05-12 20:10:11 +01:00
|
|
|
import { isTextElement, isLinearElement } 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";
|
2020-06-07 10:55:08 +01:00
|
|
|
import {
|
|
|
|
SVG_NS,
|
|
|
|
distance,
|
|
|
|
getFontString,
|
|
|
|
getFontFamilyString,
|
|
|
|
isRTL,
|
|
|
|
} 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-05-14 17:04:33 +02:00
|
|
|
const DASHARRAY_DASHED = [12, 8];
|
|
|
|
const DASHARRAY_DOTTED = [3, 6];
|
|
|
|
|
2020-03-08 10:20:55 -07:00
|
|
|
export interface ExcalidrawElementWithCanvas {
|
|
|
|
element: ExcalidrawElement | ExcalidrawTextElement;
|
|
|
|
canvas: HTMLCanvasElement;
|
|
|
|
canvasZoom: number;
|
|
|
|
canvasOffsetX: number;
|
|
|
|
canvasOffsetY: number;
|
|
|
|
}
|
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
const generateElementCanvas = (
|
2020-04-08 09:49:52 -07:00
|
|
|
element: NonDeletedExcalidrawElement,
|
2020-03-08 10:20:55 -07:00
|
|
|
zoom: number,
|
2020-05-20 16:21:37 +03:00
|
|
|
): 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
|
|
|
|
2020-03-08 10:20:55 -07:00
|
|
|
let canvasOffsetX = 0;
|
|
|
|
let canvasOffsetY = 0;
|
|
|
|
|
2020-05-12 20:10:11 +01:00
|
|
|
if (isLinearElement(element)) {
|
2020-02-19 08:25:01 -08:00
|
|
|
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-05-20 16:21:37 +03:00
|
|
|
};
|
2020-02-19 08:25:01 -08:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
const drawElementOnCanvas = (
|
2020-04-08 09:49:52 -07:00
|
|
|
element: NonDeletedExcalidrawElement,
|
2020-02-19 08:25:01 -08:00
|
|
|
rc: RoughCanvas,
|
|
|
|
context: CanvasRenderingContext2D,
|
2020-05-20 16:21:37 +03:00
|
|
|
) => {
|
2020-02-19 08:25:01 -08:00
|
|
|
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":
|
2020-05-12 20:10:11 +01:00
|
|
|
case "draw":
|
2020-02-19 08:25:01 -08:00
|
|
|
case "line": {
|
2020-05-14 17:04:33 +02:00
|
|
|
(getShapeForElement(element) as Drawable[]).forEach((shape) => {
|
|
|
|
rc.draw(shape);
|
|
|
|
});
|
2020-02-19 08:25:01 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
if (isTextElement(element)) {
|
2020-06-07 10:55:08 +01:00
|
|
|
const rtl = isRTL(element.text);
|
|
|
|
const shouldTemporarilyAttach = rtl && !context.canvas.isConnected;
|
|
|
|
if (shouldTemporarilyAttach) {
|
|
|
|
// to correctly render RTL text mixed with LTR, we have to append it
|
|
|
|
// to the DOM
|
|
|
|
document.body.appendChild(context.canvas);
|
|
|
|
}
|
|
|
|
context.canvas.setAttribute("dir", rtl ? "rtl" : "ltr");
|
2020-02-19 08:25:01 -08:00
|
|
|
const font = context.font;
|
2020-05-27 15:14:50 +02:00
|
|
|
context.font = getFontString(element);
|
2020-02-19 08:25:01 -08:00
|
|
|
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-06-07 10:55:08 +01:00
|
|
|
|
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],
|
2020-06-07 10:55:08 +01:00
|
|
|
horizontalOffset,
|
2020-04-08 21:00:27 +01:00
|
|
|
(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-06-07 10:55:08 +01:00
|
|
|
if (shouldTemporarilyAttach) {
|
|
|
|
context.canvas.remove();
|
|
|
|
}
|
2020-02-19 08:25:01 -08:00
|
|
|
} else {
|
|
|
|
throw new Error(`Unimplemented type ${element.type}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
context.globalAlpha = 1;
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
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
|
|
|
|
>();
|
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const getShapeForElement = (element: ExcalidrawElement) =>
|
|
|
|
shapeCache.get(element);
|
2020-03-08 10:20:55 -07:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const invalidateShapeForElement = (element: ExcalidrawElement) =>
|
2020-03-08 10:20:55 -07:00
|
|
|
shapeCache.delete(element);
|
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const generateRoughOptions = (element: ExcalidrawElement): Options => {
|
2020-05-17 23:01:35 +09:00
|
|
|
const options: Options = {
|
|
|
|
seed: element.seed,
|
|
|
|
strokeLineDash:
|
2020-05-14 17:04:33 +02:00
|
|
|
element.strokeStyle === "dashed"
|
|
|
|
? DASHARRAY_DASHED
|
|
|
|
: element.strokeStyle === "dotted"
|
|
|
|
? DASHARRAY_DOTTED
|
2020-05-17 23:01:35 +09:00
|
|
|
: undefined,
|
2020-05-14 17:04:33 +02:00
|
|
|
// for non-solid strokes, disable multiStroke because it tends to make
|
|
|
|
// dashes/dots overlay each other
|
2020-05-17 23:01:35 +09:00
|
|
|
disableMultiStroke: element.strokeStyle !== "solid",
|
2020-05-14 17:04:33 +02:00
|
|
|
// for non-solid strokes, increase the width a bit to make it visually
|
|
|
|
// similar to solid strokes, because we're also disabling multiStroke
|
2020-05-17 23:01:35 +09:00
|
|
|
strokeWidth:
|
2020-05-14 17:04:33 +02:00
|
|
|
element.strokeStyle !== "solid"
|
|
|
|
? element.strokeWidth + 0.5
|
2020-05-17 23:01:35 +09:00
|
|
|
: element.strokeWidth,
|
2020-05-14 17:04:33 +02:00
|
|
|
// when increasing strokeWidth, we must explicitly set fillWeight and
|
|
|
|
// hachureGap because if not specified, roughjs uses strokeWidth to
|
|
|
|
// calculate them (and we don't want the fills to be modified)
|
2020-05-17 23:01:35 +09:00
|
|
|
fillWeight: element.strokeWidth / 2,
|
|
|
|
hachureGap: element.strokeWidth * 4,
|
|
|
|
roughness: element.roughness,
|
|
|
|
stroke: element.strokeColor,
|
|
|
|
};
|
2020-05-14 17:04:33 +02:00
|
|
|
|
2020-05-17 23:01:35 +09:00
|
|
|
switch (element.type) {
|
|
|
|
case "rectangle":
|
|
|
|
case "diamond":
|
|
|
|
case "ellipse": {
|
|
|
|
options.fillStyle = element.fillStyle;
|
|
|
|
options.fill =
|
|
|
|
element.backgroundColor === "transparent"
|
|
|
|
? undefined
|
|
|
|
: element.backgroundColor;
|
|
|
|
if (element.type === "ellipse") {
|
|
|
|
options.curveFitting = 1;
|
|
|
|
}
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
case "line":
|
2020-05-21 12:57:54 -07:00
|
|
|
case "draw": {
|
2020-05-17 23:01:35 +09:00
|
|
|
// If shape is a line and is a closed shape,
|
|
|
|
// fill the shape if a color is set.
|
2020-05-21 12:57:54 -07:00
|
|
|
if (isPathALoop(element.points)) {
|
|
|
|
options.fillStyle = element.fillStyle;
|
|
|
|
options.fill =
|
|
|
|
element.backgroundColor === "transparent"
|
|
|
|
? undefined
|
|
|
|
: element.backgroundColor;
|
2020-05-17 23:01:35 +09:00
|
|
|
}
|
|
|
|
return options;
|
|
|
|
}
|
2020-05-21 12:57:54 -07:00
|
|
|
case "arrow":
|
|
|
|
return options;
|
2020-05-17 23:01:35 +09:00
|
|
|
default: {
|
|
|
|
throw new Error(`Unimplemented type ${element.type}`);
|
|
|
|
}
|
|
|
|
}
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-05-17 23:01:35 +09:00
|
|
|
|
2020-06-09 17:36:25 +02:00
|
|
|
const generateElementShape = (
|
2020-05-17 23:01:35 +09:00
|
|
|
element: NonDeletedExcalidrawElement,
|
|
|
|
generator: RoughGenerator,
|
2020-05-20 16:21:37 +03:00
|
|
|
) => {
|
2020-05-17 23:01:35 +09:00
|
|
|
let shape = shapeCache.get(element) || null;
|
|
|
|
if (!shape) {
|
|
|
|
elementWithCanvasCache.delete(element);
|
|
|
|
|
|
|
|
switch (element.type) {
|
|
|
|
case "rectangle":
|
2020-08-15 00:59:43 +09:00
|
|
|
if (element.strokeSharpness === "round") {
|
|
|
|
const w = element.width;
|
|
|
|
const h = element.height;
|
|
|
|
const r = Math.min(w, h) * 0.25;
|
|
|
|
shape = generator.path(
|
|
|
|
`M ${r} 0 L ${w - r} 0 Q ${w} 0, ${w} ${r} L ${w} ${
|
|
|
|
h - r
|
|
|
|
} Q ${w} ${h}, ${w - r} ${h} L ${r} ${h} Q 0 ${h}, 0 ${
|
|
|
|
h - r
|
|
|
|
} L 0 ${r} Q 0 0, ${r} 0`,
|
|
|
|
generateRoughOptions(element),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
shape = generator.rectangle(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
element.width,
|
|
|
|
element.height,
|
|
|
|
generateRoughOptions(element),
|
|
|
|
);
|
|
|
|
}
|
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],
|
|
|
|
],
|
2020-05-17 23:01:35 +09:00
|
|
|
generateRoughOptions(element),
|
2020-01-28 12:25:13 -08:00
|
|
|
);
|
|
|
|
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,
|
2020-05-17 23:01:35 +09:00
|
|
|
generateRoughOptions(element),
|
2020-01-28 12:25:13 -08:00
|
|
|
);
|
|
|
|
break;
|
2020-02-04 13:45:22 +04:00
|
|
|
case "line":
|
2020-05-12 20:10:11 +01:00
|
|
|
case "draw":
|
2020-01-28 12:25:13 -08:00
|
|
|
case "arrow": {
|
2020-05-17 23:01:35 +09:00
|
|
|
const options = generateRoughOptions(element);
|
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
|
|
|
|
|
|
|
// curve is always the first element
|
|
|
|
// this simplifies finding the curve for an element
|
2020-08-15 00:59:43 +09:00
|
|
|
if (element.strokeSharpness === "sharp") {
|
|
|
|
if (options.fill) {
|
|
|
|
shape = [generator.polygon(points as [number, number][], options)];
|
|
|
|
} else {
|
|
|
|
shape = [
|
|
|
|
generator.linearPath(points as [number, number][], options),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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-08-15 00:59:43 +09:00
|
|
|
const arrowPoints = getArrowPoints(element, shape);
|
|
|
|
if (arrowPoints) {
|
|
|
|
const [x2, y2, x3, y3, x4, y4] = arrowPoints;
|
|
|
|
// for dotted arrows caps, reduce gap to make it more legible
|
|
|
|
if (element.strokeStyle === "dotted") {
|
|
|
|
options.strokeLineDash = [3, 4];
|
|
|
|
// for solid/dashed, keep solid arrow cap
|
|
|
|
} else {
|
|
|
|
delete options.strokeLineDash;
|
|
|
|
}
|
|
|
|
shape.push(
|
|
|
|
...[
|
|
|
|
generator.line(x3, y3, x2, y2, options),
|
|
|
|
generator.line(x4, y4, x2, y2, options),
|
|
|
|
],
|
|
|
|
);
|
2020-05-14 17:04:33 +02:00
|
|
|
}
|
2020-02-04 13:45:22 +04:00
|
|
|
}
|
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-06-09 17:36:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const generateElementWithCanvas = (
|
|
|
|
element: NonDeletedExcalidrawElement,
|
|
|
|
sceneState?: SceneState,
|
|
|
|
) => {
|
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-05-20 16:21:37 +03:00
|
|
|
};
|
2020-02-19 08:25:01 -08:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
const 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-05-20 16:21:37 +03:00
|
|
|
) => {
|
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-06-07 10:55:08 +01:00
|
|
|
|
|
|
|
// Clear the nested element we appended to the DOM
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-01-07 19:04:52 +04:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const 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-05-20 16:21:37 +03:00
|
|
|
) => {
|
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-05-12 20:10:11 +01:00
|
|
|
case "draw":
|
2020-02-19 08:25:01 -08:00
|
|
|
case "arrow":
|
|
|
|
case "text": {
|
2020-06-09 17:36:25 +02:00
|
|
|
generateElementShape(element, generator);
|
2020-02-19 08:25:01 -08:00
|
|
|
if (renderOptimizations) {
|
2020-06-09 17:36:25 +02:00
|
|
|
const elementWithCanvas = generateElementWithCanvas(
|
2020-06-07 10:55:08 +01:00
|
|
|
element,
|
|
|
|
sceneState,
|
|
|
|
);
|
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-05-20 16:21:37 +03:00
|
|
|
};
|
2020-01-15 22:07:19 +03:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const 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-05-20 16:21:37 +03:00
|
|
|
) => {
|
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": {
|
2020-06-09 17:36:25 +02:00
|
|
|
generateElementShape(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-05-12 20:10:11 +01:00
|
|
|
case "draw":
|
2020-01-28 12:25:13 -08:00
|
|
|
case "arrow": {
|
2020-06-09 17:36:25 +02:00
|
|
|
generateElementShape(element, generator);
|
2020-01-28 12:25:13 -08:00
|
|
|
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 (
|
2020-05-12 20:10:11 +01:00
|
|
|
(element.type === "line" || element.type === "draw") &&
|
2020-04-12 16:03:49 +09:00
|
|
|
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 =
|
2020-06-03 12:12:43 +02:00
|
|
|
element.textAlign === "center"
|
|
|
|
? element.width / 2
|
|
|
|
: element.textAlign === "right"
|
|
|
|
? element.width
|
|
|
|
: 0;
|
2020-06-07 10:55:08 +01:00
|
|
|
const direction = isRTL(element.text) ? "rtl" : "ltr";
|
2020-04-12 01:17:20 +09:00
|
|
|
const textAnchor =
|
|
|
|
element.textAlign === "center"
|
|
|
|
? "middle"
|
2020-06-07 10:55:08 +01:00
|
|
|
: element.textAlign === "right" || direction === "rtl"
|
2020-04-12 01:17:20 +09:00
|
|
|
? "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-05-27 15:14:50 +02:00
|
|
|
text.setAttribute("font-family", getFontFamilyString(element));
|
|
|
|
text.setAttribute("font-size", `${element.fontSize}px`);
|
2020-01-28 12:25:13 -08:00
|
|
|
text.setAttribute("fill", element.strokeColor);
|
2020-04-12 01:17:20 +09:00
|
|
|
text.setAttribute("text-anchor", textAnchor);
|
2020-05-13 14:30:53 +02:00
|
|
|
text.setAttribute("style", "white-space: pre;");
|
2020-06-07 10:55:08 +01:00
|
|
|
text.setAttribute("direction", direction);
|
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
|
|
|
}
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|