mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
fix: points not being normalized on single-elem resize (#5581)
This commit is contained in:
parent
27cf5ed17e
commit
ad0c4c4c78
4 changed files with 45 additions and 4 deletions
|
@ -14,6 +14,7 @@ export const rescalePoints = (
|
|||
dimension: 0 | 1,
|
||||
newSize: number,
|
||||
points: readonly Point[],
|
||||
normalize: boolean,
|
||||
): Point[] => {
|
||||
const coordinates = points.map((point) => point[dimension]);
|
||||
const maxCoordinate = Math.max(...coordinates);
|
||||
|
@ -21,10 +22,35 @@ export const rescalePoints = (
|
|||
const size = maxCoordinate - minCoordinate;
|
||||
const scale = size === 0 ? 1 : newSize / size;
|
||||
|
||||
return points.map((point): Point => {
|
||||
let nextMinCoordinate = Infinity;
|
||||
|
||||
const scaledPoints = points.map((point): Point => {
|
||||
const newCoordinate = point[dimension] * scale;
|
||||
const newPoint = [...point];
|
||||
newPoint[dimension] = newCoordinate;
|
||||
if (newCoordinate < nextMinCoordinate) {
|
||||
nextMinCoordinate = newCoordinate;
|
||||
}
|
||||
return newPoint as unknown as Point;
|
||||
});
|
||||
|
||||
if (!normalize) {
|
||||
return scaledPoints;
|
||||
}
|
||||
|
||||
if (scaledPoints.length === 2) {
|
||||
// we don't translate two-point lines
|
||||
return scaledPoints;
|
||||
}
|
||||
|
||||
const translation = minCoordinate - nextMinCoordinate;
|
||||
|
||||
const nextPoints = scaledPoints.map(
|
||||
(scaledPoint) =>
|
||||
scaledPoint.map((value, currentDimension) => {
|
||||
return currentDimension === dimension ? value + translation : value;
|
||||
}) as [number, number],
|
||||
);
|
||||
|
||||
return nextPoints;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue