2020-01-06 20:24:54 +04:00
|
|
|
import { RoughCanvas } from "roughjs/bin/canvas";
|
2020-01-28 12:25:13 -08:00
|
|
|
import { RoughSVG } from "roughjs/bin/svg";
|
2020-01-06 20:24:54 +04:00
|
|
|
|
|
|
|
import { ExcalidrawElement } from "../element/types";
|
2020-01-07 19:04:52 +04:00
|
|
|
import { getElementAbsoluteCoords, handlerRectangles } from "../element";
|
|
|
|
|
2020-01-07 23:49:39 +04:00
|
|
|
import { roundRect } from "./roundRect";
|
2020-01-07 19:04:52 +04:00
|
|
|
import { SceneState } from "../scene/types";
|
2020-01-06 20:24:54 +04:00
|
|
|
import {
|
2020-01-07 19:04:52 +04:00
|
|
|
getScrollBars,
|
|
|
|
SCROLLBAR_COLOR,
|
2020-01-24 12:04:54 +02:00
|
|
|
SCROLLBAR_WIDTH,
|
2020-01-07 19:04:52 +04:00
|
|
|
} from "../scene/scrollbars";
|
2020-01-06 20:24:54 +04:00
|
|
|
|
2020-01-28 12:25:13 -08:00
|
|
|
import { renderElement, renderElementToSvg } from "./renderElement";
|
2020-01-06 20:24:54 +04:00
|
|
|
|
|
|
|
export function renderScene(
|
2020-01-09 19:22:04 +04:00
|
|
|
elements: readonly ExcalidrawElement[],
|
2020-01-06 20:24:54 +04:00
|
|
|
rc: RoughCanvas,
|
|
|
|
canvas: HTMLCanvasElement,
|
|
|
|
sceneState: SceneState,
|
|
|
|
// extra options, currently passed by export helper
|
|
|
|
{
|
|
|
|
offsetX,
|
|
|
|
offsetY,
|
|
|
|
renderScrollbars = true,
|
2020-01-24 12:04:54 +02:00
|
|
|
renderSelection = true,
|
2020-01-06 20:24:54 +04:00
|
|
|
}: {
|
|
|
|
offsetX?: number;
|
|
|
|
offsetY?: number;
|
|
|
|
renderScrollbars?: boolean;
|
|
|
|
renderSelection?: boolean;
|
2020-01-24 12:04:54 +02:00
|
|
|
} = {},
|
2020-01-06 20:24:54 +04:00
|
|
|
) {
|
|
|
|
if (!canvas) return;
|
|
|
|
const context = canvas.getContext("2d")!;
|
|
|
|
|
|
|
|
const fillStyle = context.fillStyle;
|
|
|
|
if (typeof sceneState.viewBackgroundColor === "string") {
|
2020-01-21 18:07:08 +01:00
|
|
|
const hasTransparence =
|
|
|
|
sceneState.viewBackgroundColor === "transparent" ||
|
|
|
|
sceneState.viewBackgroundColor.length === 5 ||
|
|
|
|
sceneState.viewBackgroundColor.length === 9;
|
|
|
|
if (hasTransparence) {
|
|
|
|
context.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
}
|
2020-01-06 20:24:54 +04:00
|
|
|
context.fillStyle = sceneState.viewBackgroundColor;
|
|
|
|
context.fillRect(0, 0, canvas.width, canvas.height);
|
|
|
|
} else {
|
|
|
|
context.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
}
|
|
|
|
context.fillStyle = fillStyle;
|
|
|
|
|
|
|
|
sceneState = {
|
|
|
|
...sceneState,
|
|
|
|
scrollX: typeof offsetX === "number" ? offsetX : sceneState.scrollX,
|
2020-01-24 12:04:54 +02:00
|
|
|
scrollY: typeof offsetY === "number" ? offsetY : sceneState.scrollY,
|
2020-01-06 20:24:54 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
elements.forEach(element => {
|
2020-01-22 15:14:19 +01:00
|
|
|
if (
|
|
|
|
!isVisibleElement(
|
|
|
|
element,
|
|
|
|
sceneState.scrollX,
|
|
|
|
sceneState.scrollY,
|
|
|
|
// If canvas is scaled for high pixelDeviceRatio width and height
|
|
|
|
// setted in the `style` attribute
|
|
|
|
parseInt(canvas.style.width) || canvas.width,
|
2020-01-24 12:04:54 +02:00
|
|
|
parseInt(canvas.style.height) || canvas.height,
|
2020-01-22 15:14:19 +01:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-12 03:38:41 +05:00
|
|
|
context.translate(
|
|
|
|
element.x + sceneState.scrollX,
|
2020-01-24 12:04:54 +02:00
|
|
|
element.y + sceneState.scrollY,
|
2020-01-12 03:38:41 +05:00
|
|
|
);
|
|
|
|
renderElement(element, rc, context);
|
2020-01-31 18:56:55 +01:00
|
|
|
context.translate(
|
|
|
|
-element.x - sceneState.scrollX,
|
|
|
|
-element.y - sceneState.scrollY,
|
|
|
|
);
|
2020-01-07 19:04:52 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
if (renderSelection) {
|
|
|
|
const selectedElements = elements.filter(el => el.isSelected);
|
|
|
|
|
|
|
|
selectedElements.forEach(element => {
|
2020-01-06 20:24:54 +04:00
|
|
|
const margin = 4;
|
|
|
|
|
2020-01-07 19:04:52 +04:00
|
|
|
const [
|
|
|
|
elementX1,
|
|
|
|
elementY1,
|
|
|
|
elementX2,
|
2020-01-24 12:04:54 +02:00
|
|
|
elementY2,
|
2020-01-07 19:04:52 +04:00
|
|
|
] = getElementAbsoluteCoords(element);
|
2020-01-06 20:24:54 +04:00
|
|
|
const lineDash = context.getLineDash();
|
|
|
|
context.setLineDash([8, 4]);
|
|
|
|
context.strokeRect(
|
|
|
|
elementX1 - margin + sceneState.scrollX,
|
|
|
|
elementY1 - margin + sceneState.scrollY,
|
|
|
|
elementX2 - elementX1 + margin * 2,
|
2020-01-24 12:04:54 +02:00
|
|
|
elementY2 - elementY1 + margin * 2,
|
2020-01-06 20:24:54 +04:00
|
|
|
);
|
|
|
|
context.setLineDash(lineDash);
|
2020-01-07 19:04:52 +04:00
|
|
|
});
|
2020-01-06 20:24:54 +04:00
|
|
|
|
2020-01-07 19:04:52 +04:00
|
|
|
if (selectedElements.length === 1 && selectedElements[0].type !== "text") {
|
|
|
|
const handlers = handlerRectangles(selectedElements[0], sceneState);
|
2020-02-01 15:49:18 +04:00
|
|
|
Object.values(handlers)
|
|
|
|
.filter(handler => handler !== undefined)
|
|
|
|
.forEach(handler => {
|
|
|
|
context.strokeRect(handler[0], handler[1], handler[2], handler[3]);
|
|
|
|
});
|
2020-01-06 20:24:54 +04:00
|
|
|
}
|
2020-01-07 19:04:52 +04:00
|
|
|
}
|
2020-01-06 20:24:54 +04:00
|
|
|
|
|
|
|
if (renderScrollbars) {
|
|
|
|
const scrollBars = getScrollBars(
|
|
|
|
elements,
|
|
|
|
context.canvas.width / window.devicePixelRatio,
|
|
|
|
context.canvas.height / window.devicePixelRatio,
|
|
|
|
sceneState.scrollX,
|
2020-01-24 12:04:54 +02:00
|
|
|
sceneState.scrollY,
|
2020-01-06 20:24:54 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
const strokeStyle = context.strokeStyle;
|
|
|
|
context.fillStyle = SCROLLBAR_COLOR;
|
|
|
|
context.strokeStyle = "rgba(255,255,255,0.8)";
|
|
|
|
[scrollBars.horizontal, scrollBars.vertical].forEach(scrollBar => {
|
|
|
|
if (scrollBar)
|
|
|
|
roundRect(
|
|
|
|
context,
|
|
|
|
scrollBar.x,
|
|
|
|
scrollBar.y,
|
|
|
|
scrollBar.width,
|
|
|
|
scrollBar.height,
|
2020-01-24 12:04:54 +02:00
|
|
|
SCROLLBAR_WIDTH / 2,
|
2020-01-06 20:24:54 +04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
context.strokeStyle = strokeStyle;
|
|
|
|
context.fillStyle = fillStyle;
|
|
|
|
}
|
|
|
|
}
|
2020-01-22 15:14:19 +01:00
|
|
|
|
|
|
|
function isVisibleElement(
|
|
|
|
element: ExcalidrawElement,
|
|
|
|
scrollX: number,
|
|
|
|
scrollY: number,
|
|
|
|
canvasWidth: number,
|
2020-01-24 12:04:54 +02:00
|
|
|
canvasHeight: number,
|
2020-01-22 15:14:19 +01:00
|
|
|
) {
|
|
|
|
let [x1, y1, x2, y2] = getElementAbsoluteCoords(element);
|
2020-02-01 15:49:18 +04:00
|
|
|
if (element.type !== "arrow") {
|
|
|
|
x1 += scrollX;
|
|
|
|
y1 += scrollY;
|
|
|
|
x2 += scrollX;
|
|
|
|
y2 += scrollY;
|
|
|
|
return x2 >= 0 && x1 <= canvasWidth && y2 >= 0 && y1 <= canvasHeight;
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
x2 + scrollX >= 0 &&
|
|
|
|
x1 + scrollX <= canvasWidth &&
|
|
|
|
y2 + scrollY >= 0 &&
|
|
|
|
y1 + scrollY <= canvasHeight
|
|
|
|
);
|
|
|
|
}
|
2020-01-22 15:14:19 +01:00
|
|
|
}
|
2020-01-28 12:25:13 -08:00
|
|
|
|
|
|
|
// This should be only called for exporting purposes
|
|
|
|
export function renderSceneToSvg(
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
rsvg: RoughSVG,
|
|
|
|
svgRoot: SVGElement,
|
|
|
|
{
|
|
|
|
offsetX = 0,
|
|
|
|
offsetY = 0,
|
|
|
|
}: {
|
|
|
|
offsetX?: number;
|
|
|
|
offsetY?: number;
|
|
|
|
} = {},
|
|
|
|
) {
|
|
|
|
if (!svgRoot) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// render elements
|
|
|
|
elements.forEach(element => {
|
|
|
|
renderElementToSvg(
|
|
|
|
element,
|
|
|
|
rsvg,
|
|
|
|
svgRoot,
|
|
|
|
element.x + offsetX,
|
|
|
|
element.y + offsetY,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|