Unify bounds calculation

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>
This commit is contained in:
Mark Tolmacs 2024-09-24 18:25:31 +02:00
parent c0915c6b98
commit 0c47bd0004
No known key found for this signature in database
9 changed files with 80 additions and 94 deletions

View file

@ -2,7 +2,6 @@ import type {
ExcalidrawElement,
ExcalidrawLinearElement,
ExcalidrawFreeDrawElement,
NonDeleted,
ExcalidrawTextElementWithContainer,
ElementsMap,
} from "./types";
@ -643,33 +642,6 @@ export const getClosestElementBounds = (
return getElementBounds(closestElement, elementsMap);
};
export interface BoundingBox {
minX: number;
minY: number;
maxX: number;
maxY: number;
midX: number;
midY: number;
width: number;
height: number;
}
export const getCommonBoundingBox = (
elements: ExcalidrawElement[] | readonly NonDeleted<ExcalidrawElement>[],
): BoundingBox => {
const [minX, minY, maxX, maxY] = getCommonBounds(elements);
return {
minX,
minY,
maxX,
maxY,
width: maxX - minX,
height: maxY - minY,
midX: (minX + maxX) / 2,
midY: (minY + maxY) / 2,
};
};
/**
* returns scene coords of user's editor viewport (visible canvas area) bounds
*/

View file

@ -17,7 +17,6 @@ import {
getElementAbsoluteCoords,
getCommonBounds,
getResizedElementAbsoluteCoords,
getCommonBoundingBox,
} from "./bounds";
import {
isArrowElement,
@ -808,9 +807,11 @@ export const resizeMultipleElements = (
return [...acc, { ...text, ...xy }];
}, [] as ExcalidrawTextElementWithContainer[]);
const { minX, minY, maxX, maxY, midX, midY } = getCommonBoundingBox(
const [minX, minY, maxX, maxY] = getCommonBounds(
targetElements.map(({ orig }) => orig).concat(boundTextElements),
);
const midX = (minX + maxX) / 2;
const midY = (minY + maxY) / 2;
const width = maxX - minX;
const height = maxY - minY;