feat: resize multiple elements including two-point lines (#1607)
This commit is contained in:
parent
6b628bb1a6
commit
4d2e8f9ad1
@ -114,6 +114,7 @@ const getLinearElementAbsoluteCoords = (
|
||||
element: ExcalidrawLinearElement,
|
||||
): [number, number, number, number] => {
|
||||
if (element.points.length < 2 || !getShapeForElement(element)) {
|
||||
// XXX this is just a poor estimate and not very useful
|
||||
const { minX, minY, maxX, maxY } = element.points.reduce(
|
||||
(limits, [x, y]) => {
|
||||
limits.minY = Math.min(limits.minY, y);
|
||||
@ -219,6 +220,7 @@ const getLinearElementRotatedBounds = (
|
||||
cy: number,
|
||||
): [number, number, number, number] => {
|
||||
if (element.points.length < 2 || !getShapeForElement(element)) {
|
||||
// XXX this is just a poor estimate and not very useful
|
||||
const { minX, minY, maxX, maxY } = element.points.reduce(
|
||||
(limits, [x, y]) => {
|
||||
[x, y] = rotate(element.x + x, element.y + y, cx, cy, element.angle);
|
||||
@ -311,7 +313,7 @@ export const getResizedElementAbsoluteCoords = (
|
||||
nextWidth: number,
|
||||
nextHeight: number,
|
||||
): [number, number, number, number] => {
|
||||
if (!isLinearElement(element) || element.points.length <= 2) {
|
||||
if (!isLinearElement(element)) {
|
||||
return [
|
||||
element.x,
|
||||
element.y,
|
||||
|
@ -407,7 +407,7 @@ export const canResizeMutlipleElements = (
|
||||
return elements.every(
|
||||
(element) =>
|
||||
["rectangle", "diamond", "ellipse"].includes(element.type) ||
|
||||
(isLinearElement(element) && element.points.length > 2),
|
||||
isLinearElement(element),
|
||||
);
|
||||
};
|
||||
|
||||
|
@ -18,21 +18,28 @@ export function rescalePoints(
|
||||
const prevMinDimension = Math.min(...prevDimValues);
|
||||
const prevDimensionSize = prevMaxDimension - prevMinDimension;
|
||||
|
||||
const dimensionScaleFactor = nextDimensionSize / prevDimensionSize;
|
||||
const dimensionScaleFactor =
|
||||
prevDimensionSize === 0 ? 1 : nextDimensionSize / prevDimensionSize;
|
||||
|
||||
let nextMinDimension = Infinity;
|
||||
|
||||
const scaledPoints = prevPoints.map((prevPoint) =>
|
||||
prevPoint.map((value, currentDimension) => {
|
||||
if (currentDimension !== dimension) {
|
||||
return value;
|
||||
}
|
||||
const scaledValue = value * dimensionScaleFactor;
|
||||
nextMinDimension = Math.min(scaledValue, nextMinDimension);
|
||||
return scaledValue;
|
||||
}),
|
||||
const scaledPoints = prevPoints.map(
|
||||
(prevPoint) =>
|
||||
prevPoint.map((value, currentDimension) => {
|
||||
if (currentDimension !== dimension) {
|
||||
return value;
|
||||
}
|
||||
const scaledValue = value * dimensionScaleFactor;
|
||||
nextMinDimension = Math.min(scaledValue, nextMinDimension);
|
||||
return scaledValue;
|
||||
}) as [number, number],
|
||||
);
|
||||
|
||||
if (scaledPoints.length === 2) {
|
||||
// we don't tranlate two-point lines
|
||||
return scaledPoints;
|
||||
}
|
||||
|
||||
const translation = prevMinDimension - nextMinDimension;
|
||||
|
||||
const nextPoints = scaledPoints.map(
|
||||
|
Loading…
x
Reference in New Issue
Block a user