feat(editor): implement overlap box selection
This commit is contained in:
@@ -1281,7 +1281,7 @@ export const elementCenterPoint = (
|
||||
xOffset: number = 0,
|
||||
yOffset: number = 0,
|
||||
) => {
|
||||
if (isLinearElement(element)) {
|
||||
if (isLinearElement(element) || isFreeDrawElement(element)) {
|
||||
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element, elementsMap);
|
||||
const [x, y] = pointFrom<GlobalPoint>((x1 + x2) / 2, (y1 + y2) / 2);
|
||||
|
||||
|
||||
@@ -154,14 +154,11 @@ export const hitElementItself = ({
|
||||
|
||||
// Hit test against the extended, rotated bounding box of the element first
|
||||
const bounds = getElementBounds(element, elementsMap, true);
|
||||
const hitBounds = isPointWithinBounds(
|
||||
pointFrom(bounds[0] - threshold, bounds[1] - threshold),
|
||||
pointRotateRads(
|
||||
point,
|
||||
getCenterForBounds(bounds),
|
||||
-element.angle as Radians,
|
||||
),
|
||||
pointFrom(bounds[2] + threshold, bounds[3] + threshold),
|
||||
const hitBounds = isPointInRotatedBounds(
|
||||
point,
|
||||
bounds,
|
||||
element.angle,
|
||||
threshold,
|
||||
);
|
||||
|
||||
// PERF: Bail out early if the point is not even in the
|
||||
@@ -192,18 +189,98 @@ export const hitElementItself = ({
|
||||
return result;
|
||||
};
|
||||
|
||||
const getBoundsCorners = (bounds: Bounds, angle: Radians = 0 as Radians) => {
|
||||
const [x1, y1, x2, y2] = bounds;
|
||||
const center = getCenterForBounds(bounds);
|
||||
const corners = [
|
||||
pointFrom<GlobalPoint>(x1, y1),
|
||||
pointFrom<GlobalPoint>(x2, y1),
|
||||
pointFrom<GlobalPoint>(x2, y2),
|
||||
pointFrom<GlobalPoint>(x1, y2),
|
||||
] as const;
|
||||
|
||||
if (angle === 0) {
|
||||
return corners;
|
||||
}
|
||||
|
||||
return corners.map((point) => pointRotateRads(point, center, angle)) as [
|
||||
GlobalPoint,
|
||||
GlobalPoint,
|
||||
GlobalPoint,
|
||||
GlobalPoint,
|
||||
];
|
||||
};
|
||||
|
||||
const getBoundsEdges = (
|
||||
corners: readonly [GlobalPoint, GlobalPoint, GlobalPoint, GlobalPoint],
|
||||
) =>
|
||||
[
|
||||
lineSegment(corners[0], corners[1]),
|
||||
lineSegment(corners[1], corners[2]),
|
||||
lineSegment(corners[2], corners[3]),
|
||||
lineSegment(corners[3], corners[0]),
|
||||
] as const;
|
||||
|
||||
const isPointInRotatedBounds = (
|
||||
point: GlobalPoint,
|
||||
bounds: Bounds,
|
||||
angle: Radians,
|
||||
tolerance = 0,
|
||||
) => {
|
||||
const adjustedPoint =
|
||||
angle === 0
|
||||
? point
|
||||
: pointRotateRads(point, getCenterForBounds(bounds), -angle as Radians);
|
||||
|
||||
return isPointWithinBounds(
|
||||
pointFrom(bounds[0] - tolerance, bounds[1] - tolerance),
|
||||
adjustedPoint,
|
||||
pointFrom(bounds[2] + tolerance, bounds[3] + tolerance),
|
||||
);
|
||||
};
|
||||
|
||||
export const hitElementBoundingBox = (
|
||||
point: GlobalPoint,
|
||||
element: ExcalidrawElement,
|
||||
elementsMap: ElementsMap,
|
||||
tolerance = 0,
|
||||
) => {
|
||||
let [x1, y1, x2, y2] = getElementBounds(element, elementsMap);
|
||||
x1 -= tolerance;
|
||||
y1 -= tolerance;
|
||||
x2 += tolerance;
|
||||
y2 += tolerance;
|
||||
return isPointWithinBounds(pointFrom(x1, y1), point, pointFrom(x2, y2));
|
||||
const bounds = getElementBounds(element, elementsMap, true);
|
||||
return isPointInRotatedBounds(point, bounds, element.angle, tolerance);
|
||||
};
|
||||
|
||||
export const doBoundsIntersectElementBoundingBox = (
|
||||
intersectorBounds: Bounds,
|
||||
element: ExcalidrawElement,
|
||||
elementsMap: ElementsMap,
|
||||
) => {
|
||||
const [x1, y1, x2, y2] = intersectorBounds;
|
||||
const intersectorCorners = [
|
||||
pointFrom<GlobalPoint>(x1, y1),
|
||||
pointFrom<GlobalPoint>(x2, y1),
|
||||
pointFrom<GlobalPoint>(x2, y2),
|
||||
pointFrom<GlobalPoint>(x1, y2),
|
||||
] as const;
|
||||
const intersectorEdges = getBoundsEdges(intersectorCorners);
|
||||
|
||||
const elementBounds = getElementBounds(element, elementsMap, true);
|
||||
const elementBoundsCorners = getBoundsCorners(elementBounds, element.angle);
|
||||
const elementBoundsEdges = getBoundsEdges(elementBoundsCorners);
|
||||
|
||||
return (
|
||||
elementBoundsCorners.some((point) =>
|
||||
isPointWithinBounds(intersectorCorners[0], point, intersectorCorners[2]),
|
||||
) ||
|
||||
intersectorCorners.some((point) =>
|
||||
isPointInRotatedBounds(point, elementBounds, element.angle),
|
||||
) ||
|
||||
intersectorEdges.some((selectionEdge) =>
|
||||
elementBoundsEdges.some(
|
||||
(elementBoundsEdge) =>
|
||||
!!lineSegmentIntersectionPoints(selectionEdge, elementBoundsEdge),
|
||||
),
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
export const hitElementBoundingBoxOnly = (
|
||||
|
||||
@@ -99,7 +99,7 @@ export const isElementContainingFrame = (
|
||||
element: ExcalidrawElement,
|
||||
frame: ExcalidrawFrameLikeElement,
|
||||
elementsMap: ElementsMap,
|
||||
) => {
|
||||
): boolean => {
|
||||
return getElementsWithinSelection([frame], element, elementsMap).some(
|
||||
(e) => e.id === frame.id,
|
||||
);
|
||||
@@ -140,7 +140,7 @@ export const elementOverlapsWithFrame = (
|
||||
element: ExcalidrawElement,
|
||||
frame: ExcalidrawFrameLikeElement,
|
||||
elementsMap: ElementsMap,
|
||||
) => {
|
||||
): boolean => {
|
||||
return (
|
||||
elementsAreInFrameBounds([element], frame, elementsMap) ||
|
||||
isElementIntersectingFrame(element, frame, elementsMap) ||
|
||||
|
||||
@@ -1,11 +1,27 @@
|
||||
import { arrayToMap, isShallowEqual } from "@excalidraw/common";
|
||||
import { arrayToMap, isShallowEqual, type Bounds } from "@excalidraw/common";
|
||||
import {
|
||||
lineSegment,
|
||||
pointFrom,
|
||||
type GlobalPoint,
|
||||
type LineSegment,
|
||||
} from "@excalidraw/math";
|
||||
|
||||
import type {
|
||||
AppState,
|
||||
BoxSelectionMode,
|
||||
InteractiveCanvasAppState,
|
||||
} from "@excalidraw/excalidraw/types";
|
||||
|
||||
import { getElementAbsoluteCoords, getElementBounds } from "./bounds";
|
||||
import {
|
||||
getElementAbsoluteCoords,
|
||||
getElementBounds,
|
||||
getElementLineSegments,
|
||||
} from "./bounds";
|
||||
import {
|
||||
doBoundsIntersectElementBoundingBox,
|
||||
intersectElementWithLineSegment,
|
||||
shouldTestInside,
|
||||
} from "./collision";
|
||||
import { isElementInViewport } from "./sizeHelpers";
|
||||
import {
|
||||
isBoundToContainer,
|
||||
@@ -30,6 +46,180 @@ import type {
|
||||
NonDeletedExcalidrawElement,
|
||||
} from "./types";
|
||||
|
||||
// Broad-phase only for overlap mode. This decides whether plain AABB overlap
|
||||
// should be refined via the element's rotated local bounds. Elements that fail
|
||||
// `shouldTestInside()` still go through the outline-specific path below, so
|
||||
// this is intentionally not the whole overlap-selection policy.
|
||||
const shouldUseRotatedOverlapBroadPhase = (
|
||||
element: NonDeletedExcalidrawElement,
|
||||
) =>
|
||||
element.angle !== 0 &&
|
||||
(isTextElement(element) ||
|
||||
element.type === "freedraw" ||
|
||||
element.type === "image");
|
||||
|
||||
const clipLineSegmentToBounds = (
|
||||
segment: LineSegment<GlobalPoint>,
|
||||
bounds: Bounds,
|
||||
): LineSegment<GlobalPoint> | null => {
|
||||
const [minX, minY, maxX, maxY] = bounds;
|
||||
const [[x1, y1], [x2, y2]] = segment;
|
||||
const deltaX = x2 - x1;
|
||||
const deltaY = y2 - y1;
|
||||
let tMin = 0;
|
||||
let tMax = 1;
|
||||
|
||||
const clip = (p: number, q: number) => {
|
||||
if (p === 0) {
|
||||
return q >= 0;
|
||||
}
|
||||
|
||||
const ratio = q / p;
|
||||
|
||||
if (p < 0) {
|
||||
if (ratio > tMax) {
|
||||
return false;
|
||||
}
|
||||
tMin = Math.max(tMin, ratio);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (ratio < tMin) {
|
||||
return false;
|
||||
}
|
||||
tMax = Math.min(tMax, ratio);
|
||||
return true;
|
||||
};
|
||||
|
||||
if (
|
||||
!clip(-deltaX, x1 - minX) ||
|
||||
!clip(deltaX, maxX - x1) ||
|
||||
!clip(-deltaY, y1 - minY) ||
|
||||
!clip(deltaY, maxY - y1)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return lineSegment(
|
||||
pointFrom<GlobalPoint>(x1 + tMin * deltaX, y1 + tMin * deltaY),
|
||||
pointFrom<GlobalPoint>(x1 + tMax * deltaX, y1 + tMax * deltaY),
|
||||
);
|
||||
};
|
||||
|
||||
const isPointWithinAabb = (point: GlobalPoint, bounds: Bounds) =>
|
||||
point[0] >= bounds[0] &&
|
||||
point[0] <= bounds[2] &&
|
||||
point[1] >= bounds[1] &&
|
||||
point[1] <= bounds[3];
|
||||
|
||||
const shouldSkipElementFromSelection = (element: NonDeletedExcalidrawElement) =>
|
||||
element.locked || element.type === "selection" || isBoundToContainer(element);
|
||||
|
||||
const getFrameBoundsForSelection = (
|
||||
element: NonDeletedExcalidrawElement,
|
||||
elementsMap: ElementsMap,
|
||||
): Bounds | null => {
|
||||
if (!element.frameId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const containingFrame = getContainingFrame(element, elementsMap);
|
||||
return containingFrame
|
||||
? (getElementBounds(containingFrame, elementsMap) as Bounds)
|
||||
: null;
|
||||
};
|
||||
|
||||
const finalizeElementsInSelection = (
|
||||
elementsInSelection: NonDeletedExcalidrawElement[],
|
||||
excludeElementsInFrames: boolean,
|
||||
elementsMap: ElementsMap,
|
||||
): NonDeletedExcalidrawElement[] => {
|
||||
elementsInSelection = excludeElementsInFrames
|
||||
? excludeElementsInFramesFromSelection(elementsInSelection)
|
||||
: elementsInSelection;
|
||||
|
||||
return elementsInSelection.filter((element) => {
|
||||
const containingFrame = getContainingFrame(element, elementsMap);
|
||||
|
||||
if (containingFrame) {
|
||||
return elementOverlapsWithFrame(element, containingFrame, elementsMap);
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
};
|
||||
|
||||
const getSelectionEdges = (
|
||||
selectionBounds: Bounds,
|
||||
): readonly LineSegment<GlobalPoint>[] => {
|
||||
const selectionTopLeft = pointFrom<GlobalPoint>(
|
||||
selectionBounds[0],
|
||||
selectionBounds[1],
|
||||
);
|
||||
const selectionBottomRight = pointFrom<GlobalPoint>(
|
||||
selectionBounds[2],
|
||||
selectionBounds[3],
|
||||
);
|
||||
|
||||
return [
|
||||
lineSegment(
|
||||
selectionTopLeft,
|
||||
pointFrom<GlobalPoint>(selectionBounds[2], selectionBounds[1]),
|
||||
),
|
||||
lineSegment(
|
||||
pointFrom<GlobalPoint>(selectionBounds[2], selectionBounds[1]),
|
||||
selectionBottomRight,
|
||||
),
|
||||
lineSegment(
|
||||
selectionBottomRight,
|
||||
pointFrom<GlobalPoint>(selectionBounds[0], selectionBounds[3]),
|
||||
),
|
||||
lineSegment(
|
||||
pointFrom<GlobalPoint>(selectionBounds[0], selectionBounds[3]),
|
||||
selectionTopLeft,
|
||||
),
|
||||
];
|
||||
};
|
||||
|
||||
const getVisibleElementOutlineSegments = (
|
||||
element: NonDeletedExcalidrawElement,
|
||||
frameBounds: Bounds | null,
|
||||
elementsMap: ElementsMap,
|
||||
) =>
|
||||
frameBounds
|
||||
? getElementLineSegments(element, elementsMap).flatMap((segment) => {
|
||||
const clippedSegment = clipLineSegmentToBounds(segment, frameBounds);
|
||||
return clippedSegment ? [clippedSegment] : [];
|
||||
})
|
||||
: getElementLineSegments(element, elementsMap);
|
||||
|
||||
const doesSelectionIntersectElementOutline = (
|
||||
element: NonDeletedExcalidrawElement,
|
||||
frameBounds: Bounds | null,
|
||||
selectionEdges: readonly LineSegment<GlobalPoint>[],
|
||||
elementsMap: ElementsMap,
|
||||
) =>
|
||||
selectionEdges.some((selectionEdge) =>
|
||||
intersectElementWithLineSegment(
|
||||
element,
|
||||
elementsMap,
|
||||
selectionEdge,
|
||||
0,
|
||||
true,
|
||||
).some((point) => !frameBounds || isPointWithinAabb(point, frameBounds)),
|
||||
);
|
||||
|
||||
const doesSelectionContainElementOutline = (
|
||||
outlineSegments: readonly LineSegment<GlobalPoint>[],
|
||||
selectionBounds: Bounds,
|
||||
) =>
|
||||
outlineSegments.length > 0 &&
|
||||
outlineSegments.every(
|
||||
(outlineSegment) =>
|
||||
isPointWithinAabb(outlineSegment[0], selectionBounds) &&
|
||||
isPointWithinAabb(outlineSegment[1], selectionBounds),
|
||||
);
|
||||
|
||||
/**
|
||||
* Frames and their containing elements are not to be selected at the same time.
|
||||
* Given an array of selected elements, if there are frames and their containing elements
|
||||
@@ -62,55 +252,153 @@ export const getElementsWithinSelection = (
|
||||
selection: NonDeletedExcalidrawElement,
|
||||
elementsMap: ElementsMap,
|
||||
excludeElementsInFrames: boolean = true,
|
||||
) => {
|
||||
const [selectionX1, selectionY1, selectionX2, selectionY2] =
|
||||
boxSelectionMode: BoxSelectionMode = "contain",
|
||||
): NonDeletedExcalidrawElement[] => {
|
||||
const [selectionStartX, selectionStartY, selectionEndX, selectionEndY] =
|
||||
getElementAbsoluteCoords(selection, elementsMap);
|
||||
const selectionX1 = Math.min(selectionStartX, selectionEndX);
|
||||
const selectionY1 = Math.min(selectionStartY, selectionEndY);
|
||||
const selectionX2 = Math.max(selectionStartX, selectionEndX);
|
||||
const selectionY2 = Math.max(selectionStartY, selectionEndY);
|
||||
const selectionBounds = [
|
||||
selectionX1,
|
||||
selectionY1,
|
||||
selectionX2,
|
||||
selectionY2,
|
||||
] as Bounds;
|
||||
|
||||
let elementsInSelection = elements.filter((element) => {
|
||||
let [elementX1, elementY1, elementX2, elementY2] = getElementBounds(
|
||||
element,
|
||||
elementsMap,
|
||||
);
|
||||
if (boxSelectionMode !== "overlap") {
|
||||
const elementsInSelection: NonDeletedExcalidrawElement[] = [];
|
||||
|
||||
const containingFrame = getContainingFrame(element, elementsMap);
|
||||
if (containingFrame) {
|
||||
const [fx1, fy1, fx2, fy2] = getElementBounds(
|
||||
containingFrame,
|
||||
elementsMap,
|
||||
);
|
||||
for (const element of elements) {
|
||||
if (shouldSkipElementFromSelection(element)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
elementX1 = Math.max(fx1, elementX1);
|
||||
elementY1 = Math.max(fy1, elementY1);
|
||||
elementX2 = Math.min(fx2, elementX2);
|
||||
elementY2 = Math.min(fy2, elementY2);
|
||||
const elementBounds = getElementBounds(element, elementsMap) as Bounds;
|
||||
const frameBounds = getFrameBoundsForSelection(element, elementsMap);
|
||||
let elementX1 = elementBounds[0];
|
||||
let elementY1 = elementBounds[1];
|
||||
let elementX2 = elementBounds[2];
|
||||
let elementY2 = elementBounds[3];
|
||||
|
||||
if (frameBounds) {
|
||||
elementX1 = Math.max(frameBounds[0], elementX1);
|
||||
elementY1 = Math.max(frameBounds[1], elementY1);
|
||||
elementX2 = Math.min(frameBounds[2], elementX2);
|
||||
elementY2 = Math.min(frameBounds[3], elementY2);
|
||||
}
|
||||
|
||||
if (
|
||||
selectionX1 <= elementX1 &&
|
||||
selectionY1 <= elementY1 &&
|
||||
selectionX2 >= elementX2 &&
|
||||
selectionY2 >= elementY2
|
||||
) {
|
||||
elementsInSelection.push(element);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
element.locked === false &&
|
||||
element.type !== "selection" &&
|
||||
!isBoundToContainer(element) &&
|
||||
return finalizeElementsInSelection(
|
||||
elementsInSelection,
|
||||
excludeElementsInFrames,
|
||||
elementsMap,
|
||||
);
|
||||
}
|
||||
|
||||
const selectionEdges = getSelectionEdges(selectionBounds);
|
||||
const elementsInSelection: NonDeletedExcalidrawElement[] = [];
|
||||
|
||||
for (const element of elements) {
|
||||
if (shouldSkipElementFromSelection(element)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const elementBounds = getElementBounds(element, elementsMap) as Bounds;
|
||||
const frameBounds = getFrameBoundsForSelection(element, elementsMap);
|
||||
let elementX1 = elementBounds[0];
|
||||
let elementY1 = elementBounds[1];
|
||||
let elementX2 = elementBounds[2];
|
||||
let elementY2 = elementBounds[3];
|
||||
|
||||
if (frameBounds) {
|
||||
elementX1 = Math.max(frameBounds[0], elementX1);
|
||||
elementY1 = Math.max(frameBounds[1], elementY1);
|
||||
elementX2 = Math.min(frameBounds[2], elementX2);
|
||||
elementY2 = Math.min(frameBounds[3], elementY2);
|
||||
}
|
||||
|
||||
const isSelectionContainingElement =
|
||||
selectionX1 <= elementX1 &&
|
||||
selectionY1 <= elementY1 &&
|
||||
selectionX2 >= elementX2 &&
|
||||
selectionY2 >= elementY2
|
||||
);
|
||||
});
|
||||
selectionY2 >= elementY2;
|
||||
|
||||
elementsInSelection = excludeElementsInFrames
|
||||
? excludeElementsInFramesFromSelection(elementsInSelection)
|
||||
: elementsInSelection;
|
||||
const isSelectionOverlappingElementAabb =
|
||||
selectionX1 <= elementX2 &&
|
||||
selectionY1 <= elementY2 &&
|
||||
selectionX2 >= elementX1 &&
|
||||
selectionY2 >= elementY1;
|
||||
const isSelectionOverlappingElement = shouldUseRotatedOverlapBroadPhase(
|
||||
element,
|
||||
)
|
||||
? isSelectionOverlappingElementAabb &&
|
||||
doBoundsIntersectElementBoundingBox(
|
||||
selectionBounds,
|
||||
element,
|
||||
elementsMap,
|
||||
)
|
||||
: isSelectionOverlappingElementAabb;
|
||||
|
||||
elementsInSelection = elementsInSelection.filter((element) => {
|
||||
const containingFrame = getContainingFrame(element, elementsMap);
|
||||
const shouldSelectFromInside = shouldTestInside(element);
|
||||
|
||||
if (containingFrame) {
|
||||
return elementOverlapsWithFrame(element, containingFrame, elementsMap);
|
||||
if (shouldSelectFromInside) {
|
||||
if (isSelectionOverlappingElement) {
|
||||
elementsInSelection.push(element);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
if (!isSelectionOverlappingElement) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return elementsInSelection;
|
||||
if (isSelectionContainingElement) {
|
||||
elementsInSelection.push(element);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
doesSelectionIntersectElementOutline(
|
||||
element,
|
||||
frameBounds,
|
||||
selectionEdges,
|
||||
elementsMap,
|
||||
)
|
||||
) {
|
||||
elementsInSelection.push(element);
|
||||
continue;
|
||||
}
|
||||
|
||||
const outlineSegments = getVisibleElementOutlineSegments(
|
||||
element,
|
||||
frameBounds,
|
||||
elementsMap,
|
||||
);
|
||||
|
||||
if (
|
||||
outlineSegments.length > 0 &&
|
||||
doesSelectionContainElementOutline(outlineSegments, selectionBounds)
|
||||
) {
|
||||
elementsInSelection.push(element);
|
||||
}
|
||||
}
|
||||
|
||||
return finalizeElementsInSelection(
|
||||
elementsInSelection,
|
||||
excludeElementsInFrames,
|
||||
elementsMap,
|
||||
);
|
||||
};
|
||||
|
||||
export const getVisibleAndNonSelectedElements = (
|
||||
|
||||
Reference in New Issue
Block a user