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-04-10 18:09:29 -04:00
|
|
|
import oc from "open-color";
|
2020-01-06 20:24:54 +04:00
|
|
|
|
2020-03-08 10:20:55 -07:00
|
|
|
import { FlooredNumber, AppState } from "../types";
|
2020-04-08 09:49:52 -07:00
|
|
|
import {
|
|
|
|
ExcalidrawElement,
|
|
|
|
NonDeletedExcalidrawElement,
|
2020-06-01 11:35:44 +02:00
|
|
|
ExcalidrawLinearElement,
|
|
|
|
NonDeleted,
|
2020-05-26 13:07:46 -07:00
|
|
|
GroupId,
|
2020-04-08 09:49:52 -07:00
|
|
|
} from "../element/types";
|
2020-04-07 17:49:59 +09:00
|
|
|
import {
|
|
|
|
getElementAbsoluteCoords,
|
|
|
|
OMIT_SIDES_FOR_MULTIPLE_ELEMENTS,
|
|
|
|
handlerRectanglesFromCoords,
|
|
|
|
handlerRectangles,
|
2020-06-22 17:00:09 +09:00
|
|
|
getElementBounds,
|
2020-04-07 17:49:59 +09:00
|
|
|
getCommonBounds,
|
|
|
|
} from "../element";
|
2020-01-07 19:04:52 +04:00
|
|
|
|
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-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-06-19 11:36:49 +01:00
|
|
|
import { getClientColors } from "../clients";
|
2020-06-01 11:35:44 +02:00
|
|
|
import { LinearElementEditor } from "../element/linearElementEditor";
|
2020-05-26 13:07:46 -07:00
|
|
|
import {
|
|
|
|
isSelectedViaGroup,
|
|
|
|
getSelectedGroupIds,
|
|
|
|
getElementsInGroup,
|
|
|
|
} from "../groups";
|
2020-03-14 12:18:57 -07:00
|
|
|
|
2020-04-02 17:40:26 +09:00
|
|
|
type HandlerRectanglesRet = keyof ReturnType<typeof handlerRectangles>;
|
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
const strokeRectWithRotation = (
|
2020-04-02 17:40:26 +09:00
|
|
|
context: CanvasRenderingContext2D,
|
|
|
|
x: number,
|
|
|
|
y: number,
|
|
|
|
width: number,
|
|
|
|
height: number,
|
|
|
|
cx: number,
|
|
|
|
cy: number,
|
|
|
|
angle: number,
|
|
|
|
fill?: boolean,
|
2020-05-20 16:21:37 +03:00
|
|
|
) => {
|
2020-04-02 17:40:26 +09:00
|
|
|
context.translate(cx, cy);
|
|
|
|
context.rotate(angle);
|
|
|
|
if (fill) {
|
|
|
|
context.fillRect(x - cx, y - cy, width, height);
|
|
|
|
}
|
|
|
|
context.strokeRect(x - cx, y - cy, width, height);
|
|
|
|
context.rotate(-angle);
|
|
|
|
context.translate(-cx, -cy);
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-04-02 17:40:26 +09:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
const strokeCircle = (
|
2020-04-02 17:40:26 +09:00
|
|
|
context: CanvasRenderingContext2D,
|
|
|
|
x: number,
|
|
|
|
y: number,
|
|
|
|
width: number,
|
|
|
|
height: number,
|
2020-05-20 16:21:37 +03:00
|
|
|
) => {
|
2020-04-02 17:40:26 +09:00
|
|
|
context.beginPath();
|
|
|
|
context.arc(x + width / 2, y + height / 2, width / 2, 0, Math.PI * 2);
|
|
|
|
context.fill();
|
|
|
|
context.stroke();
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-04-02 17:40:26 +09:00
|
|
|
|
2020-06-24 17:16:03 +09:00
|
|
|
const strokeGrid = (
|
2020-06-24 00:24:52 +09:00
|
|
|
context: CanvasRenderingContext2D,
|
|
|
|
gridSize: number,
|
|
|
|
offsetX: number,
|
|
|
|
offsetY: number,
|
|
|
|
width: number,
|
|
|
|
height: number,
|
|
|
|
) => {
|
|
|
|
const origStrokeStyle = context.strokeStyle;
|
|
|
|
context.strokeStyle = "rgba(0,0,0,0.1)";
|
|
|
|
context.beginPath();
|
|
|
|
for (let x = offsetX; x < offsetX + width + gridSize * 2; x += gridSize) {
|
|
|
|
context.moveTo(x, offsetY - gridSize);
|
|
|
|
context.lineTo(x, offsetY + height + gridSize * 2);
|
|
|
|
}
|
|
|
|
for (let y = offsetY; y < offsetY + height + gridSize * 2; y += gridSize) {
|
|
|
|
context.moveTo(offsetX - gridSize, y);
|
|
|
|
context.lineTo(offsetX + width + gridSize * 2, y);
|
|
|
|
}
|
|
|
|
context.stroke();
|
|
|
|
context.strokeStyle = origStrokeStyle;
|
|
|
|
};
|
|
|
|
|
2020-06-01 11:35:44 +02:00
|
|
|
const renderLinearPointHandles = (
|
|
|
|
context: CanvasRenderingContext2D,
|
|
|
|
appState: AppState,
|
|
|
|
sceneState: SceneState,
|
|
|
|
element: NonDeleted<ExcalidrawLinearElement>,
|
|
|
|
) => {
|
|
|
|
context.translate(sceneState.scrollX, sceneState.scrollY);
|
|
|
|
const origStrokeStyle = context.strokeStyle;
|
|
|
|
const lineWidth = context.lineWidth;
|
|
|
|
context.lineWidth = 1 / sceneState.zoom;
|
|
|
|
|
|
|
|
LinearElementEditor.getPointsGlobalCoordinates(element).forEach(
|
|
|
|
(point, idx) => {
|
|
|
|
context.strokeStyle = "red";
|
|
|
|
context.setLineDash([]);
|
|
|
|
context.fillStyle =
|
|
|
|
appState.editingLinearElement?.activePointIndex === idx
|
|
|
|
? "rgba(255, 127, 127, 0.9)"
|
|
|
|
: "rgba(255, 255, 255, 0.9)";
|
|
|
|
const { POINT_HANDLE_SIZE } = LinearElementEditor;
|
|
|
|
strokeCircle(
|
|
|
|
context,
|
|
|
|
point[0] - POINT_HANDLE_SIZE / 2 / sceneState.zoom,
|
|
|
|
point[1] - POINT_HANDLE_SIZE / 2 / sceneState.zoom,
|
|
|
|
POINT_HANDLE_SIZE / sceneState.zoom,
|
|
|
|
POINT_HANDLE_SIZE / sceneState.zoom,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
context.setLineDash([]);
|
|
|
|
context.lineWidth = lineWidth;
|
|
|
|
context.translate(-sceneState.scrollX, -sceneState.scrollY);
|
|
|
|
context.strokeStyle = origStrokeStyle;
|
|
|
|
};
|
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const renderScene = (
|
2020-04-08 09:49:52 -07:00
|
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
2020-03-08 10:20:55 -07:00
|
|
|
appState: AppState,
|
2020-04-08 09:49:52 -07:00
|
|
|
selectionElement: NonDeletedExcalidrawElement | null,
|
2020-03-14 17:24:28 -07:00
|
|
|
scale: number,
|
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-06-24 17:16:03 +09:00
|
|
|
renderGrid = true,
|
2020-01-06 20:24:54 +04:00
|
|
|
}: {
|
|
|
|
renderScrollbars?: boolean;
|
|
|
|
renderSelection?: boolean;
|
2020-02-19 08:25:01 -08:00
|
|
|
renderOptimizations?: boolean;
|
2020-06-24 17:16:03 +09:00
|
|
|
renderGrid?: boolean;
|
2020-01-24 12:04:54 +02:00
|
|
|
} = {},
|
2020-05-20 16:21:37 +03: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-03-15 12:25:18 -07:00
|
|
|
context.scale(scale, scale);
|
2020-01-06 20:24:54 +04:00
|
|
|
|
2020-02-15 21:03:32 +01:00
|
|
|
// When doing calculations based on canvas width we should used normalized one
|
2020-03-14 17:24:28 -07:00
|
|
|
const normalizedCanvasWidth = canvas.width / scale;
|
|
|
|
const normalizedCanvasHeight = canvas.height / scale;
|
2020-02-15 21:03:32 +01:00
|
|
|
|
|
|
|
// Paint background
|
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" ||
|
2020-04-25 23:06:16 +02:00
|
|
|
sceneState.viewBackgroundColor.length === 5 || // #RGBA
|
|
|
|
sceneState.viewBackgroundColor.length === 9 || // #RRGGBBA
|
|
|
|
/(hsla|rgba)\(/.test(sceneState.viewBackgroundColor);
|
2020-01-21 18:07:08 +01:00
|
|
|
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-03-14 17:24:28 -07:00
|
|
|
const fillStyle = context.fillStyle;
|
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-03-14 17:24:28 -07:00
|
|
|
context.fillStyle = fillStyle;
|
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-03-14 17:24:28 -07:00
|
|
|
|
|
|
|
// Apply zoom
|
|
|
|
const zoomTranslationX = (-normalizedCanvasWidth * (sceneState.zoom - 1)) / 2;
|
|
|
|
const zoomTranslationY =
|
|
|
|
(-normalizedCanvasHeight * (sceneState.zoom - 1)) / 2;
|
|
|
|
context.translate(zoomTranslationX, zoomTranslationY);
|
|
|
|
context.scale(sceneState.zoom, sceneState.zoom);
|
2020-01-06 20:24:54 +04:00
|
|
|
|
2020-06-24 00:24:52 +09:00
|
|
|
// Grid
|
2020-06-24 17:16:03 +09:00
|
|
|
if (renderGrid && appState.gridSize) {
|
|
|
|
strokeGrid(
|
2020-06-24 00:24:52 +09:00
|
|
|
context,
|
|
|
|
appState.gridSize,
|
|
|
|
-Math.ceil(zoomTranslationX / sceneState.zoom / appState.gridSize) *
|
|
|
|
appState.gridSize +
|
|
|
|
(sceneState.scrollX % appState.gridSize),
|
|
|
|
-Math.ceil(zoomTranslationY / sceneState.zoom / appState.gridSize) *
|
|
|
|
appState.gridSize +
|
|
|
|
(sceneState.scrollY % appState.gridSize),
|
|
|
|
normalizedCanvasWidth / sceneState.zoom,
|
|
|
|
normalizedCanvasHeight / sceneState.zoom,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-15 21:03:32 +01:00
|
|
|
// Paint visible elements
|
2020-03-23 13:05:07 +02:00
|
|
|
const visibleElements = elements.filter((element) =>
|
2020-02-15 21:03:32 +01:00
|
|
|
isVisibleElement(
|
|
|
|
element,
|
|
|
|
normalizedCanvasWidth,
|
|
|
|
normalizedCanvasHeight,
|
|
|
|
sceneState,
|
|
|
|
),
|
|
|
|
);
|
2020-01-06 20:24:54 +04:00
|
|
|
|
2020-03-23 13:05:07 +02:00
|
|
|
visibleElements.forEach((element) => {
|
2020-02-19 08:25:01 -08:00
|
|
|
renderElement(element, rc, context, renderOptimizations, sceneState);
|
2020-07-26 20:52:25 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (appState.editingLinearElement) {
|
|
|
|
const element = LinearElementEditor.getElement(
|
|
|
|
appState.editingLinearElement.elementId,
|
|
|
|
);
|
|
|
|
if (element) {
|
2020-06-01 11:35:44 +02:00
|
|
|
renderLinearPointHandles(context, appState, sceneState, element);
|
|
|
|
}
|
2020-07-26 20:52:25 +02:00
|
|
|
}
|
2020-01-07 19:04:52 +04:00
|
|
|
|
2020-06-01 11:35:44 +02:00
|
|
|
// Paint selection element
|
2020-02-05 22:47:10 +04:00
|
|
|
if (selectionElement) {
|
2020-02-19 08:25:01 -08:00
|
|
|
renderElement(
|
|
|
|
selectionElement,
|
|
|
|
rc,
|
|
|
|
context,
|
|
|
|
renderOptimizations,
|
|
|
|
sceneState,
|
|
|
|
);
|
2020-02-05 22:47:10 +04:00
|
|
|
}
|
|
|
|
|
2020-04-04 16:02:16 +02:00
|
|
|
// Paint selected elements
|
2020-06-01 11:35:44 +02:00
|
|
|
if (
|
|
|
|
renderSelection &&
|
|
|
|
!appState.multiElement &&
|
|
|
|
!appState.editingLinearElement
|
|
|
|
) {
|
2020-02-15 21:03:32 +01:00
|
|
|
context.translate(sceneState.scrollX, sceneState.scrollY);
|
2020-04-04 16:02:16 +02:00
|
|
|
|
|
|
|
const selections = elements.reduce((acc, element) => {
|
|
|
|
const selectionColors = [];
|
|
|
|
// local user
|
2020-05-26 13:07:46 -07:00
|
|
|
if (
|
|
|
|
appState.selectedElementIds[element.id] &&
|
|
|
|
!isSelectedViaGroup(appState, element)
|
|
|
|
) {
|
2020-04-10 18:09:29 -04:00
|
|
|
selectionColors.push(oc.black);
|
2020-04-04 16:02:16 +02:00
|
|
|
}
|
|
|
|
// remote users
|
|
|
|
if (sceneState.remoteSelectedElementIds[element.id]) {
|
|
|
|
selectionColors.push(
|
|
|
|
...sceneState.remoteSelectedElementIds[element.id].map((socketId) => {
|
2020-06-19 11:36:49 +01:00
|
|
|
const { background } = getClientColors(socketId);
|
2020-04-04 16:02:16 +02:00
|
|
|
return background;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (selectionColors.length) {
|
2020-05-26 13:07:46 -07:00
|
|
|
const [
|
|
|
|
elementX1,
|
|
|
|
elementY1,
|
|
|
|
elementX2,
|
|
|
|
elementY2,
|
|
|
|
] = getElementAbsoluteCoords(element);
|
|
|
|
acc.push({
|
|
|
|
angle: element.angle,
|
|
|
|
elementX1,
|
|
|
|
elementY1,
|
|
|
|
elementX2,
|
|
|
|
elementY2,
|
|
|
|
selectionColors,
|
|
|
|
});
|
2020-04-04 16:02:16 +02:00
|
|
|
}
|
|
|
|
return acc;
|
2020-05-26 13:07:46 -07:00
|
|
|
}, [] as { angle: number; elementX1: number; elementY1: number; elementX2: number; elementY2: number; selectionColors: string[] }[]);
|
|
|
|
|
|
|
|
function addSelectionForGroupId(groupId: GroupId) {
|
|
|
|
const groupElements = getElementsInGroup(elements, groupId);
|
|
|
|
const [elementX1, elementY1, elementX2, elementY2] = getCommonBounds(
|
|
|
|
groupElements,
|
|
|
|
);
|
|
|
|
selections.push({
|
|
|
|
angle: 0,
|
|
|
|
elementX1,
|
|
|
|
elementX2,
|
|
|
|
elementY1,
|
|
|
|
elementY2,
|
|
|
|
selectionColors: [oc.black],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const groupId of getSelectedGroupIds(appState)) {
|
|
|
|
// TODO: support multiplayer selected group IDs
|
|
|
|
addSelectionForGroupId(groupId);
|
|
|
|
}
|
2020-04-04 16:02:16 +02:00
|
|
|
|
2020-05-26 13:07:46 -07:00
|
|
|
if (appState.editingGroupId) {
|
|
|
|
addSelectionForGroupId(appState.editingGroupId);
|
|
|
|
}
|
|
|
|
|
|
|
|
selections.forEach(
|
|
|
|
({
|
|
|
|
angle,
|
2020-01-07 19:04:52 +04:00
|
|
|
elementX1,
|
|
|
|
elementY1,
|
|
|
|
elementX2,
|
2020-01-24 12:04:54 +02:00
|
|
|
elementY2,
|
2020-05-26 13:07:46 -07:00
|
|
|
selectionColors,
|
|
|
|
}) => {
|
|
|
|
const elementWidth = elementX2 - elementX1;
|
|
|
|
const elementHeight = elementY2 - elementY1;
|
|
|
|
|
|
|
|
const initialLineDash = context.getLineDash();
|
|
|
|
const lineWidth = context.lineWidth;
|
|
|
|
const lineDashOffset = context.lineDashOffset;
|
|
|
|
const strokeStyle = context.strokeStyle;
|
|
|
|
|
|
|
|
const dashedLinePadding = 4 / sceneState.zoom;
|
|
|
|
const dashWidth = 8 / sceneState.zoom;
|
|
|
|
const spaceWidth = 4 / sceneState.zoom;
|
|
|
|
|
|
|
|
context.lineWidth = 1 / sceneState.zoom;
|
|
|
|
|
|
|
|
const count = selectionColors.length;
|
|
|
|
for (var i = 0; i < count; ++i) {
|
|
|
|
context.strokeStyle = selectionColors[i];
|
|
|
|
context.setLineDash([
|
|
|
|
dashWidth,
|
|
|
|
spaceWidth + (dashWidth + spaceWidth) * (count - 1),
|
|
|
|
]);
|
|
|
|
context.lineDashOffset = (dashWidth + spaceWidth) * i;
|
|
|
|
strokeRectWithRotation(
|
|
|
|
context,
|
|
|
|
elementX1 - dashedLinePadding,
|
|
|
|
elementY1 - dashedLinePadding,
|
|
|
|
elementWidth + dashedLinePadding * 2,
|
|
|
|
elementHeight + dashedLinePadding * 2,
|
|
|
|
elementX1 + elementWidth / 2,
|
|
|
|
elementY1 + elementHeight / 2,
|
|
|
|
angle,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
context.lineDashOffset = lineDashOffset;
|
|
|
|
context.strokeStyle = strokeStyle;
|
|
|
|
context.lineWidth = lineWidth;
|
|
|
|
context.setLineDash(initialLineDash);
|
|
|
|
},
|
|
|
|
);
|
2020-03-14 17:24:28 -07:00
|
|
|
context.translate(-sceneState.scrollX, -sceneState.scrollY);
|
2020-01-06 20:24:54 +04:00
|
|
|
|
2020-04-04 16:02:16 +02:00
|
|
|
const locallySelectedElements = getSelectedElements(elements, appState);
|
|
|
|
|
2020-02-15 21:03:32 +01:00
|
|
|
// Paint resize handlers
|
2020-04-04 16:02:16 +02:00
|
|
|
if (locallySelectedElements.length === 1) {
|
2020-02-15 21:03:32 +01:00
|
|
|
context.translate(sceneState.scrollX, sceneState.scrollY);
|
2020-04-10 18:09:29 -04:00
|
|
|
context.fillStyle = oc.white;
|
2020-04-04 16:02:16 +02:00
|
|
|
const handlers = handlerRectangles(
|
|
|
|
locallySelectedElements[0],
|
|
|
|
sceneState.zoom,
|
|
|
|
);
|
2020-04-02 17:40:26 +09:00
|
|
|
Object.keys(handlers).forEach((key) => {
|
|
|
|
const handler = handlers[key as HandlerRectanglesRet];
|
|
|
|
if (handler !== undefined) {
|
2020-03-14 14:29:48 -07:00
|
|
|
const lineWidth = context.lineWidth;
|
|
|
|
context.lineWidth = 1 / sceneState.zoom;
|
2020-04-02 17:40:26 +09:00
|
|
|
if (key === "rotation") {
|
|
|
|
strokeCircle(
|
|
|
|
context,
|
|
|
|
handler[0],
|
|
|
|
handler[1],
|
|
|
|
handler[2],
|
|
|
|
handler[3],
|
|
|
|
);
|
2020-05-28 07:17:15 +09:00
|
|
|
} else {
|
2020-04-02 17:40:26 +09:00
|
|
|
strokeRectWithRotation(
|
|
|
|
context,
|
|
|
|
handler[0],
|
|
|
|
handler[1],
|
|
|
|
handler[2],
|
|
|
|
handler[3],
|
|
|
|
handler[0] + handler[2] / 2,
|
|
|
|
handler[1] + handler[3] / 2,
|
2020-04-04 16:02:16 +02:00
|
|
|
locallySelectedElements[0].angle,
|
2020-04-02 17:40:26 +09:00
|
|
|
true, // fill before stroke
|
|
|
|
);
|
|
|
|
}
|
2020-03-14 14:29:48 -07:00
|
|
|
context.lineWidth = lineWidth;
|
2020-04-02 17:40:26 +09:00
|
|
|
}
|
|
|
|
});
|
2020-03-14 17:24:28 -07:00
|
|
|
context.translate(-sceneState.scrollX, -sceneState.scrollY);
|
2020-07-26 19:21:38 +09:00
|
|
|
} else if (locallySelectedElements.length > 1 && !appState.isRotating) {
|
2020-06-08 18:25:20 +09:00
|
|
|
const dashedLinePadding = 4 / sceneState.zoom;
|
|
|
|
context.translate(sceneState.scrollX, sceneState.scrollY);
|
|
|
|
context.fillStyle = oc.white;
|
|
|
|
const [x1, y1, x2, y2] = getCommonBounds(locallySelectedElements);
|
|
|
|
const initialLineDash = context.getLineDash();
|
|
|
|
context.setLineDash([2 / sceneState.zoom]);
|
|
|
|
const lineWidth = context.lineWidth;
|
|
|
|
context.lineWidth = 1 / sceneState.zoom;
|
|
|
|
strokeRectWithRotation(
|
|
|
|
context,
|
|
|
|
x1 - dashedLinePadding,
|
|
|
|
y1 - dashedLinePadding,
|
|
|
|
x2 - x1 + dashedLinePadding * 2,
|
|
|
|
y2 - y1 + dashedLinePadding * 2,
|
|
|
|
(x1 + x2) / 2,
|
|
|
|
(y1 + y2) / 2,
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
context.lineWidth = lineWidth;
|
|
|
|
context.setLineDash(initialLineDash);
|
|
|
|
const handlers = handlerRectanglesFromCoords(
|
|
|
|
[x1, y1, x2, y2],
|
|
|
|
0,
|
|
|
|
sceneState.zoom,
|
|
|
|
undefined,
|
|
|
|
OMIT_SIDES_FOR_MULTIPLE_ELEMENTS,
|
|
|
|
);
|
|
|
|
Object.keys(handlers).forEach((key) => {
|
|
|
|
const handler = handlers[key as HandlerRectanglesRet];
|
|
|
|
if (handler !== undefined) {
|
|
|
|
const lineWidth = context.lineWidth;
|
|
|
|
context.lineWidth = 1 / sceneState.zoom;
|
2020-07-26 19:21:38 +09:00
|
|
|
if (key === "rotation") {
|
|
|
|
strokeCircle(
|
|
|
|
context,
|
|
|
|
handler[0],
|
|
|
|
handler[1],
|
|
|
|
handler[2],
|
|
|
|
handler[3],
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
strokeRectWithRotation(
|
|
|
|
context,
|
|
|
|
handler[0],
|
|
|
|
handler[1],
|
|
|
|
handler[2],
|
|
|
|
handler[3],
|
|
|
|
handler[0] + handler[2] / 2,
|
|
|
|
handler[1] + handler[3] / 2,
|
|
|
|
0,
|
|
|
|
true, // fill before stroke
|
|
|
|
);
|
|
|
|
}
|
2020-06-08 18:25:20 +09:00
|
|
|
context.lineWidth = lineWidth;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
context.translate(-sceneState.scrollX, -sceneState.scrollY);
|
2020-01-06 20:24:54 +04:00
|
|
|
}
|
2020-01-07 19:04:52 +04:00
|
|
|
}
|
2020-01-06 20:24:54 +04:00
|
|
|
|
2020-03-14 17:24:28 -07:00
|
|
|
// Reset zoom
|
|
|
|
context.scale(1 / sceneState.zoom, 1 / sceneState.zoom);
|
|
|
|
context.translate(-zoomTranslationX, -zoomTranslationY);
|
|
|
|
|
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];
|
2020-04-07 14:02:42 +01:00
|
|
|
const username = sceneState.remotePointerUsernames[clientId];
|
2020-03-14 13:52:42 -07:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-06-19 11:36:49 +01:00
|
|
|
const { background, stroke } = getClientColors(clientId);
|
2020-03-14 12:18:57 -07:00
|
|
|
|
|
|
|
const strokeStyle = context.strokeStyle;
|
|
|
|
const fillStyle = context.fillStyle;
|
2020-03-14 13:52:42 -07:00
|
|
|
const globalAlpha = context.globalAlpha;
|
2020-03-15 20:05:12 +02:00
|
|
|
context.strokeStyle = stroke;
|
|
|
|
context.fillStyle = background;
|
2020-03-14 13:52:42 -07:00
|
|
|
if (isOutOfBounds) {
|
|
|
|
context.globalAlpha = 0.2;
|
|
|
|
}
|
2020-04-04 16:12:19 +01:00
|
|
|
|
|
|
|
if (
|
|
|
|
sceneState.remotePointerButton &&
|
|
|
|
sceneState.remotePointerButton[clientId] === "down"
|
|
|
|
) {
|
|
|
|
context.beginPath();
|
|
|
|
context.arc(x, y, 15, 0, 2 * Math.PI, false);
|
|
|
|
context.lineWidth = 3;
|
|
|
|
context.strokeStyle = "#ffffff88";
|
|
|
|
context.stroke();
|
|
|
|
context.closePath();
|
|
|
|
|
|
|
|
context.beginPath();
|
|
|
|
context.arc(x, y, 15, 0, 2 * Math.PI, false);
|
|
|
|
context.lineWidth = 1;
|
|
|
|
context.strokeStyle = stroke;
|
|
|
|
context.stroke();
|
|
|
|
context.closePath();
|
|
|
|
}
|
|
|
|
|
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-04-07 14:02:42 +01:00
|
|
|
|
|
|
|
if (!isOutOfBounds && username) {
|
|
|
|
const offsetX = x + width;
|
|
|
|
const offsetY = y + height;
|
|
|
|
const paddingHorizontal = 4;
|
|
|
|
const paddingVertical = 4;
|
|
|
|
const measure = context.measureText(username);
|
|
|
|
const measureHeight =
|
|
|
|
measure.actualBoundingBoxDescent + measure.actualBoundingBoxAscent;
|
|
|
|
|
|
|
|
// Border
|
|
|
|
context.fillStyle = stroke;
|
|
|
|
context.globalAlpha = globalAlpha;
|
|
|
|
context.fillRect(
|
|
|
|
offsetX - 1,
|
|
|
|
offsetY - 1,
|
|
|
|
measure.width + 2 * paddingHorizontal + 2,
|
|
|
|
measureHeight + 2 * paddingVertical + 2,
|
|
|
|
);
|
|
|
|
// Background
|
|
|
|
context.fillStyle = background;
|
|
|
|
context.fillRect(
|
|
|
|
offsetX,
|
|
|
|
offsetY,
|
|
|
|
measure.width + 2 * paddingHorizontal,
|
|
|
|
measureHeight + 2 * paddingVertical,
|
|
|
|
);
|
2020-04-10 18:09:29 -04:00
|
|
|
context.fillStyle = oc.white;
|
2020-04-07 14:02:42 +01:00
|
|
|
context.fillText(
|
|
|
|
username,
|
|
|
|
offsetX + paddingHorizontal,
|
|
|
|
offsetY + paddingVertical + measure.actualBoundingBoxAscent,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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;
|
2020-04-04 16:12:19 +01:00
|
|
|
context.closePath();
|
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-03-15 12:25:18 -07:00
|
|
|
let scrollBars;
|
2020-01-06 20:24:54 +04:00
|
|
|
if (renderScrollbars) {
|
2020-03-15 12:25:18 -07:00
|
|
|
scrollBars = getScrollBars(
|
2020-01-06 20:24:54 +04:00
|
|
|
elements,
|
2020-02-15 21:03:32 +01:00
|
|
|
normalizedCanvasWidth,
|
|
|
|
normalizedCanvasHeight,
|
|
|
|
sceneState,
|
2020-01-06 20:24:54 +04:00
|
|
|
);
|
|
|
|
|
2020-03-14 17:24:28 -07:00
|
|
|
const fillStyle = context.fillStyle;
|
|
|
|
const strokeStyle = context.strokeStyle;
|
2020-01-06 20:24:54 +04:00
|
|
|
context.fillStyle = SCROLLBAR_COLOR;
|
|
|
|
context.strokeStyle = "rgba(255,255,255,0.8)";
|
2020-03-23 13:05:07 +02:00
|
|
|
[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-03-14 17:24:28 -07:00
|
|
|
context.fillStyle = fillStyle;
|
|
|
|
context.strokeStyle = strokeStyle;
|
2020-01-06 20:24:54 +04:00
|
|
|
}
|
2020-02-01 16:52:10 +00:00
|
|
|
|
2020-03-15 12:25:18 -07:00
|
|
|
context.scale(1 / scale, 1 / scale);
|
|
|
|
|
|
|
|
return { atLeastOneVisibleElement: visibleElements.length > 0, scrollBars };
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-01-22 15:14:19 +01:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
const isVisibleElement = (
|
2020-01-22 15:14:19 +01:00
|
|
|
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-05-20 16:21:37 +03:00
|
|
|
) => {
|
2020-06-22 17:00:09 +09:00
|
|
|
const [x1, y1, x2, y2] = getElementBounds(element);
|
2020-02-15 21:03:32 +01:00
|
|
|
|
|
|
|
// 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-05-20 16:21:37 +03:00
|
|
|
};
|
2020-01-28 12:25:13 -08:00
|
|
|
|
|
|
|
// This should be only called for exporting purposes
|
2020-05-20 16:21:37 +03:00
|
|
|
export const renderSceneToSvg = (
|
2020-04-08 09:49:52 -07:00
|
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
2020-01-28 12:25:13 -08:00
|
|
|
rsvg: RoughSVG,
|
|
|
|
svgRoot: SVGElement,
|
|
|
|
{
|
|
|
|
offsetX = 0,
|
|
|
|
offsetY = 0,
|
|
|
|
}: {
|
|
|
|
offsetX?: number;
|
|
|
|
offsetY?: number;
|
|
|
|
} = {},
|
2020-05-20 16:21:37 +03:00
|
|
|
) => {
|
2020-01-28 12:25:13 -08:00
|
|
|
if (!svgRoot) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// render elements
|
2020-03-23 13:05:07 +02:00
|
|
|
elements.forEach((element) => {
|
2020-03-20 15:19:20 +01:00
|
|
|
if (!element.isDeleted) {
|
|
|
|
renderElementToSvg(
|
|
|
|
element,
|
|
|
|
rsvg,
|
|
|
|
svgRoot,
|
|
|
|
element.x + offsetX,
|
|
|
|
element.y + offsetY,
|
|
|
|
);
|
|
|
|
}
|
2020-01-28 12:25:13 -08:00
|
|
|
});
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|