Skip to content

Commit

Permalink
fix: points not being normalized on single-elem resize (#5581)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwelle committed Aug 16, 2022
1 parent 27cf5ed commit ad0c4c4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/element/bounds.ts
Expand Up @@ -495,6 +495,7 @@ export const getResizedElementAbsoluteCoords = (
element: ExcalidrawElement,
nextWidth: number,
nextHeight: number,
normalizePoints: boolean,
): [number, number, number, number] => {
if (!(isLinearElement(element) || isFreeDrawElement(element))) {
return [
Expand All @@ -508,7 +509,8 @@ export const getResizedElementAbsoluteCoords = (
const points = rescalePoints(
0,
nextWidth,
rescalePoints(1, nextHeight, element.points),
rescalePoints(1, nextHeight, element.points, normalizePoints),
normalizePoints,
);

let bounds: [number, number, number, number];
Expand Down
1 change: 1 addition & 0 deletions src/element/newElement.ts
Expand Up @@ -198,6 +198,7 @@ const getAdjustedDimensions = (
element,
nextWidth,
nextHeight,
false,
);
const deltaX1 = (x1 - nextX1) / 2;
const deltaY1 = (y1 - nextY1) / 2;
Expand Down
16 changes: 14 additions & 2 deletions src/element/resizeElements.ts
Expand Up @@ -264,13 +264,15 @@ const rescalePointsInElement = (
element: NonDeletedExcalidrawElement,
width: number,
height: number,
normalizePoints: boolean,
) =>
isLinearElement(element) || isFreeDrawElement(element)
? {
points: rescalePoints(
0,
width,
rescalePoints(1, height, element.points),
rescalePoints(1, height, element.points, normalizePoints),
normalizePoints,
),
}
: {};
Expand Down Expand Up @@ -374,6 +376,7 @@ const resizeSingleTextElement = (
element,
nextWidth,
nextHeight,
false,
);
const deltaX1 = (x1 - nextX1) / 2;
const deltaY1 = (y1 - nextY1) / 2;
Expand Down Expand Up @@ -415,6 +418,7 @@ export const resizeSingleElement = (
stateAtResizeStart,
stateAtResizeStart.width,
stateAtResizeStart.height,
true,
);
const startTopLeft: Point = [x1, y1];
const startBottomRight: Point = [x2, y2];
Expand All @@ -432,6 +436,7 @@ export const resizeSingleElement = (
element,
element.width,
element.height,
true,
);

const boundsCurrentWidth = esx2 - esx1;
Expand Down Expand Up @@ -525,6 +530,7 @@ export const resizeSingleElement = (
stateAtResizeStart,
eleNewWidth,
eleNewHeight,
true,
);
const newBoundsWidth = newBoundsX2 - newBoundsX1;
const newBoundsHeight = newBoundsY2 - newBoundsY1;
Expand Down Expand Up @@ -595,6 +601,7 @@ export const resizeSingleElement = (
stateAtResizeStart,
eleNewWidth,
eleNewHeight,
true,
);
// For linear elements (x,y) are the coordinates of the first drawn point not the top-left corner
// So we need to readjust (x,y) to be where the first point should be
Expand Down Expand Up @@ -725,7 +732,12 @@ const resizeMultipleElements = (
const y = anchorY + (element.orig.y - anchorY) * scale;

// readjust points for linear & free draw elements
const rescaledPoints = rescalePointsInElement(element.orig, width, height);
const rescaledPoints = rescalePointsInElement(
element.orig,
width,
height,
false,
);

const update: {
width: number;
Expand Down
28 changes: 27 additions & 1 deletion src/points.ts
Expand Up @@ -14,17 +14,43 @@ 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);
const minCoordinate = Math.min(...coordinates);
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;
};

2 comments on commit ad0c4c4

@vercel
Copy link

@vercel vercel bot commented on ad0c4c4 Aug 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

excalidraw-package-example – ./src/packages/excalidraw

excalidraw-package-example-git-master-excalidraw.vercel.app
excalidraw-package-example-excalidraw.vercel.app
excalidraw-package-example.vercel.app

@vercel
Copy link

@vercel vercel bot commented on ad0c4c4 Aug 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.