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
|
|
|
|
2020-03-08 10:20:55 -07:00
|
|
|
import { FlooredNumber, AppState } from "../types";
|
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-02-15 21:03:32 +01:00
|
|
|
import { getZoomTranslation } from "../scene/zoom";
|
2020-02-16 22:54:50 +01:00
|
|
|
import { getSelectedElements } from "../scene/selection";
|
2020-01-06 20:24:54 +04:00
|
|
|
|
2020-01-28 12:25:13 -08:00
|
|
|
import { renderElement, renderElementToSvg } from "./renderElement";
|
2020-03-14 12:18:57 -07:00
|
|
|
import colors from "../colors";
|
|
|
|
|
|
|
|
function colorForClientId(clientId: string) {
|
|
|
|
// Naive way of getting an integer out of the clientId
|
|
|
|
const sum = clientId.split("").reduce((a, str) => a + str.charCodeAt(0), 0);
|
|
|
|
return colors.elementBackground[sum % colors.elementBackground.length];
|
|
|
|
}
|
2020-01-06 20:24:54 +04:00
|
|
|
|
|
|
|
export function renderScene(
|
2020-01-09 19:22:04 +04:00
|
|
|
elements: readonly ExcalidrawElement[],
|
2020-03-08 10:20:55 -07:00
|
|
|
appState: AppState,
|
2020-02-05 22:47:10 +04:00
|
|
|
selectionElement: ExcalidrawElement | null,
|
2020-01-06 20:24:54 +04:00
|
|
|
rc: RoughCanvas,
|
|
|
|
canvas: HTMLCanvasElement,
|
|
|
|
sceneState: SceneState,
|
|
|
|
// extra options, currently passed by export helper
|
|
|
|
{
|
|
|
|
renderScrollbars = true,
|
2020-01-24 12:04:54 +02:00
|
|
|
renderSelection = true,
|
2020-02-19 08:25:01 -08:00
|
|
|
// Whether to employ render optimizations to improve performance.
|
|
|
|
// Should not be turned on for export operations and similar, because it
|
|
|
|
// doesn't guarantee pixel-perfect output.
|
|
|
|
renderOptimizations = false,
|
2020-01-06 20:24:54 +04:00
|
|
|
}: {
|
|
|
|
renderScrollbars?: boolean;
|
|
|
|
renderSelection?: boolean;
|
2020-02-19 08:25:01 -08:00
|
|
|
renderOptimizations?: boolean;
|
2020-01-24 12:04:54 +02:00
|
|
|
} = {},
|
2020-03-01 21:43:35 +01:00
|
|
|
) {
|
2020-02-02 20:04:35 +02:00
|
|
|
if (!canvas) {
|
2020-03-01 21:43:35 +01:00
|
|
|
return { atLeastOneVisibleElement: false };
|
2020-02-02 20:04:35 +02:00
|
|
|
}
|
2020-02-15 21:03:32 +01:00
|
|
|
|
2020-01-06 20:24:54 +04:00
|
|
|
const context = canvas.getContext("2d")!;
|
|
|
|
|
2020-02-15 21:03:32 +01:00
|
|
|
// Get initial scale transform as reference for later usage
|
|
|
|
const initialContextTransform = context.getTransform();
|
|
|
|
|
|
|
|
// When doing calculations based on canvas width we should used normalized one
|
|
|
|
const normalizedCanvasWidth =
|
|
|
|
canvas.width / getContextTransformScaleX(initialContextTransform);
|
|
|
|
const normalizedCanvasHeight =
|
|
|
|
canvas.height / getContextTransformScaleY(initialContextTransform);
|
|
|
|
|
2020-02-19 08:25:01 -08:00
|
|
|
const zoomTranslation = getZoomTranslation(canvas, sceneState.zoom);
|
|
|
|
function applyZoom(context: CanvasRenderingContext2D): void {
|
|
|
|
context.save();
|
|
|
|
|
|
|
|
// Handle zoom scaling
|
2020-02-15 21:03:32 +01:00
|
|
|
context.setTransform(
|
|
|
|
getContextTransformScaleX(initialContextTransform) * sceneState.zoom,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
getContextTransformScaleY(initialContextTransform) * sceneState.zoom,
|
|
|
|
getContextTransformTranslateX(context.getTransform()),
|
|
|
|
getContextTransformTranslateY(context.getTransform()),
|
|
|
|
);
|
2020-02-19 08:25:01 -08:00
|
|
|
// Handle zoom translation
|
2020-02-15 21:03:32 +01:00
|
|
|
context.setTransform(
|
|
|
|
getContextTransformScaleX(context.getTransform()),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
getContextTransformScaleY(context.getTransform()),
|
|
|
|
getContextTransformTranslateX(initialContextTransform) -
|
|
|
|
zoomTranslation.x,
|
|
|
|
getContextTransformTranslateY(initialContextTransform) -
|
|
|
|
zoomTranslation.y,
|
|
|
|
);
|
|
|
|
}
|
2020-02-19 08:25:01 -08:00
|
|
|
function resetZoom(context: CanvasRenderingContext2D): void {
|
|
|
|
context.restore();
|
|
|
|
}
|
2020-02-15 21:03:32 +01:00
|
|
|
|
|
|
|
// Paint background
|
|
|
|
context.save();
|
2020-01-06 20:24:54 +04:00
|
|
|
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) {
|
2020-02-15 21:03:32 +01:00
|
|
|
context.clearRect(0, 0, normalizedCanvasWidth, normalizedCanvasHeight);
|
2020-01-21 18:07:08 +01:00
|
|
|
}
|
2020-01-06 20:24:54 +04:00
|
|
|
context.fillStyle = sceneState.viewBackgroundColor;
|
2020-02-15 21:03:32 +01:00
|
|
|
context.fillRect(0, 0, normalizedCanvasWidth, normalizedCanvasHeight);
|
2020-01-06 20:24:54 +04:00
|
|
|
} else {
|
2020-02-15 21:03:32 +01:00
|
|
|
context.clearRect(0, 0, normalizedCanvasWidth, normalizedCanvasHeight);
|
2020-01-06 20:24:54 +04:00
|
|
|
}
|
2020-02-15 21:03:32 +01:00
|
|
|
context.restore();
|
2020-01-06 20:24:54 +04:00
|
|
|
|
2020-02-15 21:03:32 +01:00
|
|
|
// Paint visible elements
|
|
|
|
const visibleElements = elements.filter(element =>
|
|
|
|
isVisibleElement(
|
|
|
|
element,
|
|
|
|
normalizedCanvasWidth,
|
|
|
|
normalizedCanvasHeight,
|
|
|
|
sceneState,
|
|
|
|
),
|
|
|
|
);
|
2020-01-06 20:24:54 +04:00
|
|
|
|
2020-02-19 08:25:01 -08:00
|
|
|
applyZoom(context);
|
2020-02-15 21:03:32 +01:00
|
|
|
visibleElements.forEach(element => {
|
2020-02-19 08:25:01 -08:00
|
|
|
renderElement(element, rc, context, renderOptimizations, sceneState);
|
2020-01-07 19:04:52 +04:00
|
|
|
});
|
2020-02-19 08:25:01 -08:00
|
|
|
resetZoom(context);
|
2020-01-07 19:04:52 +04:00
|
|
|
|
2020-02-15 21:03:32 +01:00
|
|
|
// Pain selection element
|
2020-02-05 22:47:10 +04:00
|
|
|
if (selectionElement) {
|
2020-02-19 08:25:01 -08:00
|
|
|
applyZoom(context);
|
|
|
|
renderElement(
|
|
|
|
selectionElement,
|
|
|
|
rc,
|
|
|
|
context,
|
|
|
|
renderOptimizations,
|
|
|
|
sceneState,
|
|
|
|
);
|
|
|
|
resetZoom(context);
|
2020-02-05 22:47:10 +04:00
|
|
|
}
|
|
|
|
|
2020-02-15 21:03:32 +01:00
|
|
|
// Pain selected elements
|
2020-01-07 19:04:52 +04:00
|
|
|
if (renderSelection) {
|
2020-03-08 10:20:55 -07:00
|
|
|
const selectedElements = getSelectedElements(elements, appState);
|
2020-02-15 21:03:32 +01:00
|
|
|
const dashledLinePadding = 4 / sceneState.zoom;
|
2020-01-07 19:04:52 +04:00
|
|
|
|
2020-02-19 08:25:01 -08:00
|
|
|
applyZoom(context);
|
2020-02-15 21:03:32 +01:00
|
|
|
context.translate(sceneState.scrollX, sceneState.scrollY);
|
2020-01-07 19:04:52 +04:00
|
|
|
selectedElements.forEach(element => {
|
|
|
|
const [
|
|
|
|
elementX1,
|
|
|
|
elementY1,
|
|
|
|
elementX2,
|
2020-01-24 12:04:54 +02:00
|
|
|
elementY2,
|
2020-01-07 19:04:52 +04:00
|
|
|
] = getElementAbsoluteCoords(element);
|
2020-02-15 21:03:32 +01:00
|
|
|
|
|
|
|
const elementWidth = elementX2 - elementX1;
|
|
|
|
const elementHeight = elementY2 - elementY1;
|
|
|
|
|
|
|
|
const initialLineDash = context.getLineDash();
|
|
|
|
context.setLineDash([8 / sceneState.zoom, 4 / sceneState.zoom]);
|
2020-03-14 14:29:48 -07:00
|
|
|
const lineWidth = context.lineWidth;
|
|
|
|
context.lineWidth = 1 / sceneState.zoom;
|
2020-01-06 20:24:54 +04:00
|
|
|
context.strokeRect(
|
2020-02-15 21:03:32 +01:00
|
|
|
elementX1 - dashledLinePadding,
|
|
|
|
elementY1 - dashledLinePadding,
|
|
|
|
elementWidth + dashledLinePadding * 2,
|
|
|
|
elementHeight + dashledLinePadding * 2,
|
2020-01-06 20:24:54 +04:00
|
|
|
);
|
2020-03-14 14:29:48 -07:00
|
|
|
context.lineWidth = lineWidth;
|
2020-02-15 21:03:32 +01:00
|
|
|
context.setLineDash(initialLineDash);
|
2020-01-07 19:04:52 +04:00
|
|
|
});
|
2020-02-19 08:25:01 -08:00
|
|
|
resetZoom(context);
|
2020-01-06 20:24:54 +04:00
|
|
|
|
2020-02-15 21:03:32 +01:00
|
|
|
// Paint resize handlers
|
2020-01-07 19:04:52 +04:00
|
|
|
if (selectedElements.length === 1 && selectedElements[0].type !== "text") {
|
2020-02-19 08:25:01 -08:00
|
|
|
applyZoom(context);
|
2020-02-15 21:03:32 +01:00
|
|
|
context.translate(sceneState.scrollX, sceneState.scrollY);
|
2020-03-09 16:01:29 -04:00
|
|
|
context.fillStyle = "#fff";
|
2020-02-15 21:03:32 +01:00
|
|
|
const handlers = handlerRectangles(selectedElements[0], sceneState.zoom);
|
2020-02-01 15:49:18 +04:00
|
|
|
Object.values(handlers)
|
|
|
|
.filter(handler => handler !== undefined)
|
|
|
|
.forEach(handler => {
|
2020-03-14 14:29:48 -07:00
|
|
|
const lineWidth = context.lineWidth;
|
|
|
|
context.lineWidth = 1 / sceneState.zoom;
|
2020-03-09 16:01:29 -04:00
|
|
|
context.fillRect(handler[0], handler[1], handler[2], handler[3]);
|
2020-02-01 15:49:18 +04:00
|
|
|
context.strokeRect(handler[0], handler[1], handler[2], handler[3]);
|
2020-03-14 14:29:48 -07:00
|
|
|
context.lineWidth = lineWidth;
|
2020-02-01 15:49:18 +04:00
|
|
|
});
|
2020-02-19 08:25:01 -08:00
|
|
|
resetZoom(context);
|
2020-01-06 20:24:54 +04:00
|
|
|
}
|
2020-01-07 19:04:52 +04:00
|
|
|
}
|
2020-01-06 20:24:54 +04:00
|
|
|
|
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
|
|
|
// Paint remote pointers
|
|
|
|
for (const clientId in sceneState.remotePointerViewportCoords) {
|
2020-03-14 13:52:42 -07:00
|
|
|
let { x, y } = sceneState.remotePointerViewportCoords[clientId];
|
|
|
|
|
|
|
|
const width = 9;
|
|
|
|
const height = 14;
|
|
|
|
|
|
|
|
const isOutOfBounds =
|
|
|
|
x < 0 ||
|
|
|
|
x > normalizedCanvasWidth - width ||
|
|
|
|
y < 0 ||
|
|
|
|
y > normalizedCanvasHeight - height;
|
|
|
|
|
|
|
|
x = Math.max(x, 0);
|
|
|
|
x = Math.min(x, normalizedCanvasWidth - width);
|
|
|
|
y = Math.max(y, 0);
|
|
|
|
y = Math.min(y, normalizedCanvasHeight - height);
|
2020-03-14 12:18:57 -07:00
|
|
|
|
|
|
|
const color = colorForClientId(clientId);
|
|
|
|
|
|
|
|
const strokeStyle = context.strokeStyle;
|
|
|
|
const fillStyle = context.fillStyle;
|
2020-03-14 13:52:42 -07:00
|
|
|
const globalAlpha = context.globalAlpha;
|
2020-03-14 12:18:57 -07:00
|
|
|
context.strokeStyle = color;
|
|
|
|
context.fillStyle = color;
|
2020-03-14 13:52:42 -07:00
|
|
|
if (isOutOfBounds) {
|
|
|
|
context.globalAlpha = 0.2;
|
|
|
|
}
|
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
|
|
|
context.beginPath();
|
2020-03-14 12:08:45 -07:00
|
|
|
context.moveTo(x, y);
|
|
|
|
context.lineTo(x + 1, y + 14);
|
|
|
|
context.lineTo(x + 4, y + 9);
|
|
|
|
context.lineTo(x + 9, y + 10);
|
|
|
|
context.lineTo(x, y);
|
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
|
|
|
context.fill();
|
|
|
|
context.stroke();
|
2020-03-14 12:18:57 -07:00
|
|
|
context.strokeStyle = strokeStyle;
|
|
|
|
context.fillStyle = fillStyle;
|
2020-03-14 13:52:42 -07:00
|
|
|
context.globalAlpha = globalAlpha;
|
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
|
|
|
}
|
|
|
|
|
2020-02-15 21:03:32 +01:00
|
|
|
// Paint scrollbars
|
2020-01-06 20:24:54 +04:00
|
|
|
if (renderScrollbars) {
|
|
|
|
const scrollBars = getScrollBars(
|
|
|
|
elements,
|
2020-02-15 21:03:32 +01:00
|
|
|
normalizedCanvasWidth,
|
|
|
|
normalizedCanvasHeight,
|
|
|
|
sceneState,
|
2020-01-06 20:24:54 +04:00
|
|
|
);
|
|
|
|
|
2020-02-15 21:03:32 +01:00
|
|
|
context.save();
|
2020-01-06 20:24:54 +04:00
|
|
|
context.fillStyle = SCROLLBAR_COLOR;
|
|
|
|
context.strokeStyle = "rgba(255,255,255,0.8)";
|
|
|
|
[scrollBars.horizontal, scrollBars.vertical].forEach(scrollBar => {
|
2020-02-02 20:04:35 +02:00
|
|
|
if (scrollBar) {
|
2020-01-06 20:24:54 +04:00
|
|
|
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
|
|
|
);
|
2020-02-02 20:04:35 +02:00
|
|
|
}
|
2020-01-06 20:24:54 +04:00
|
|
|
});
|
2020-02-15 21:03:32 +01:00
|
|
|
context.restore();
|
2020-03-01 21:43:35 +01:00
|
|
|
return { atLeastOneVisibleElement: visibleElements.length > 0, scrollBars };
|
2020-01-06 20:24:54 +04:00
|
|
|
}
|
2020-02-01 16:52:10 +00:00
|
|
|
|
2020-03-01 21:43:35 +01:00
|
|
|
return { atLeastOneVisibleElement: visibleElements.length > 0 };
|
2020-01-06 20:24:54 +04:00
|
|
|
}
|
2020-01-22 15:14:19 +01:00
|
|
|
|
|
|
|
function isVisibleElement(
|
|
|
|
element: ExcalidrawElement,
|
2020-02-15 21:03:32 +01:00
|
|
|
viewportWidth: number,
|
|
|
|
viewportHeight: number,
|
|
|
|
{
|
|
|
|
scrollX,
|
|
|
|
scrollY,
|
|
|
|
zoom,
|
|
|
|
}: {
|
2020-02-19 08:25:01 -08:00
|
|
|
scrollX: FlooredNumber;
|
|
|
|
scrollY: FlooredNumber;
|
2020-02-15 21:03:32 +01:00
|
|
|
zoom: number;
|
|
|
|
},
|
2020-01-22 15:14:19 +01:00
|
|
|
) {
|
2020-02-15 21:03:32 +01:00
|
|
|
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element);
|
|
|
|
|
|
|
|
// Apply zoom
|
|
|
|
const viewportWidthWithZoom = viewportWidth / zoom;
|
|
|
|
const viewportHeightWithZoom = viewportHeight / zoom;
|
|
|
|
|
|
|
|
const viewportWidthDiff = viewportWidth - viewportWidthWithZoom;
|
|
|
|
const viewportHeightDiff = viewportHeight - viewportHeightWithZoom;
|
|
|
|
|
2020-02-02 20:04:35 +02:00
|
|
|
return (
|
2020-02-15 21:03:32 +01:00
|
|
|
x2 + scrollX - viewportWidthDiff / 2 >= 0 &&
|
|
|
|
x1 + scrollX - viewportWidthDiff / 2 <= viewportWidthWithZoom &&
|
|
|
|
y2 + scrollY - viewportHeightDiff / 2 >= 0 &&
|
|
|
|
y1 + scrollY - viewportHeightDiff / 2 <= viewportHeightWithZoom
|
2020-02-02 20:04:35 +02:00
|
|
|
);
|
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,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2020-02-15 21:03:32 +01:00
|
|
|
|
|
|
|
function getContextTransformScaleX(transform: DOMMatrix): number {
|
|
|
|
return transform.a;
|
|
|
|
}
|
|
|
|
function getContextTransformScaleY(transform: DOMMatrix): number {
|
|
|
|
return transform.d;
|
|
|
|
}
|
|
|
|
function getContextTransformTranslateX(transform: DOMMatrix): number {
|
|
|
|
return transform.e;
|
|
|
|
}
|
|
|
|
function getContextTransformTranslateY(transform: DOMMatrix): number {
|
|
|
|
return transform.f;
|
|
|
|
}
|