2023-08-02 15:04:21 +05:00
|
|
|
import { MIN_FONT_SIZE, SHIFT_LOCKING_ANGLE } from "../constants";
|
2020-04-07 17:49:59 +09:00
|
|
|
import { rescalePoints } from "../points";
|
2020-04-10 01:10:35 +09:00
|
|
|
|
2020-12-06 22:39:31 +00:00
|
|
|
import {
|
|
|
|
rotate,
|
|
|
|
adjustXYWithRotation,
|
|
|
|
centerPoint,
|
|
|
|
rotatePoint,
|
|
|
|
} from "../math";
|
2020-04-08 09:49:52 -07:00
|
|
|
import {
|
|
|
|
ExcalidrawLinearElement,
|
2020-05-28 07:17:15 +09:00
|
|
|
ExcalidrawTextElement,
|
2020-04-08 09:49:52 -07:00
|
|
|
NonDeletedExcalidrawElement,
|
|
|
|
NonDeleted,
|
2022-12-05 21:03:13 +05:30
|
|
|
ExcalidrawElement,
|
|
|
|
ExcalidrawTextElementWithContainer,
|
2023-05-25 19:27:41 +05:00
|
|
|
ExcalidrawImageElement,
|
2020-04-08 09:49:52 -07:00
|
|
|
} from "./types";
|
2023-05-25 19:27:41 +05:00
|
|
|
import type { Mutable } from "../utility-types";
|
2020-05-05 00:25:40 +09:00
|
|
|
import {
|
|
|
|
getElementAbsoluteCoords,
|
|
|
|
getCommonBounds,
|
|
|
|
getResizedElementAbsoluteCoords,
|
2022-08-13 22:53:10 +05:00
|
|
|
getCommonBoundingBox,
|
2023-05-25 19:27:41 +05:00
|
|
|
getElementPointsCoords,
|
2020-05-05 00:25:40 +09:00
|
|
|
} from "./bounds";
|
2021-05-09 16:42:10 +01:00
|
|
|
import {
|
2022-12-05 21:03:13 +05:30
|
|
|
isArrowElement,
|
2022-11-30 15:55:01 +05:30
|
|
|
isBoundToContainer,
|
2023-06-15 00:42:01 +08:00
|
|
|
isFrameElement,
|
2021-05-09 16:42:10 +01:00
|
|
|
isFreeDrawElement,
|
2023-05-25 19:27:41 +05:00
|
|
|
isImageElement,
|
2021-05-09 16:42:10 +01:00
|
|
|
isLinearElement,
|
|
|
|
isTextElement,
|
|
|
|
} from "./typeChecks";
|
2020-04-07 17:49:59 +09:00
|
|
|
import { mutateElement } from "./mutateElement";
|
2021-12-16 21:14:03 +05:30
|
|
|
import { getFontString } from "../utils";
|
2020-08-08 21:04:15 -07:00
|
|
|
import { updateBoundElements } from "./binding";
|
2020-08-10 14:16:39 +02:00
|
|
|
import {
|
|
|
|
TransformHandleType,
|
|
|
|
MaybeTransformHandleType,
|
2020-12-06 22:39:31 +00:00
|
|
|
TransformHandleDirection,
|
2020-08-10 14:16:39 +02:00
|
|
|
} from "./transformHandles";
|
2021-06-01 23:52:13 +05:30
|
|
|
import { Point, PointerDownState } from "../types";
|
2021-12-16 21:14:03 +05:30
|
|
|
import Scene from "../scene/Scene";
|
|
|
|
import {
|
|
|
|
getApproxMinLineWidth,
|
2022-01-13 21:35:38 +05:30
|
|
|
getBoundTextElement,
|
2021-12-16 21:14:03 +05:30
|
|
|
getBoundTextElementId,
|
2022-11-30 15:55:01 +05:30
|
|
|
getContainerElement,
|
2021-12-16 21:14:03 +05:30
|
|
|
handleBindTextResize,
|
2023-04-25 18:06:23 +05:30
|
|
|
getBoundTextMaxWidth,
|
2023-03-22 11:32:38 +05:30
|
|
|
getApproxMinLineHeight,
|
2023-04-10 18:52:46 +05:30
|
|
|
measureText,
|
2023-04-25 18:06:23 +05:30
|
|
|
getBoundTextMaxHeight,
|
2021-12-16 21:14:03 +05:30
|
|
|
} from "./textElement";
|
2023-05-25 19:27:41 +05:00
|
|
|
import { LinearElementEditor } from "./linearElementEditor";
|
2020-04-07 17:49:59 +09:00
|
|
|
|
2021-03-26 11:45:08 -04:00
|
|
|
export const normalizeAngle = (angle: number): number => {
|
2023-05-25 19:27:41 +05:00
|
|
|
if (angle < 0) {
|
|
|
|
return angle + 2 * Math.PI;
|
|
|
|
}
|
2020-07-26 19:21:38 +09:00
|
|
|
if (angle >= 2 * Math.PI) {
|
|
|
|
return angle - 2 * Math.PI;
|
|
|
|
}
|
|
|
|
return angle;
|
|
|
|
};
|
|
|
|
|
2020-08-28 17:20:06 +09:00
|
|
|
// Returns true when transform (resizing/rotation) happened
|
|
|
|
export const transformElements = (
|
2020-09-11 17:22:40 +02:00
|
|
|
pointerDownState: PointerDownState,
|
2020-08-10 14:16:39 +02:00
|
|
|
transformHandleType: MaybeTransformHandleType,
|
2020-07-26 19:21:38 +09:00
|
|
|
selectedElements: readonly NonDeletedExcalidrawElement[],
|
2020-05-11 00:41:36 +09:00
|
|
|
resizeArrowDirection: "origin" | "end",
|
2021-10-21 22:05:48 +02:00
|
|
|
shouldRotateWithDiscreteAngle: boolean,
|
|
|
|
shouldResizeFromCenter: boolean,
|
|
|
|
shouldMaintainAspectRatio: boolean,
|
2020-05-09 17:57:00 +09:00
|
|
|
pointerX: number,
|
|
|
|
pointerY: number,
|
2020-07-26 19:21:38 +09:00
|
|
|
centerX: number,
|
|
|
|
centerY: number,
|
2020-05-05 00:25:40 +09:00
|
|
|
) => {
|
|
|
|
if (selectedElements.length === 1) {
|
|
|
|
const [element] = selectedElements;
|
2020-08-10 14:16:39 +02:00
|
|
|
if (transformHandleType === "rotation") {
|
2020-06-24 00:24:52 +09:00
|
|
|
rotateSingleElement(
|
|
|
|
element,
|
|
|
|
pointerX,
|
|
|
|
pointerY,
|
2021-10-21 22:05:48 +02:00
|
|
|
shouldRotateWithDiscreteAngle,
|
2022-12-05 21:03:13 +05:30
|
|
|
pointerDownState.originalElements,
|
2020-06-24 00:24:52 +09:00
|
|
|
);
|
2020-08-08 21:04:15 -07:00
|
|
|
updateBoundElements(element);
|
2020-05-28 07:17:15 +09:00
|
|
|
} else if (
|
2020-12-06 22:39:31 +00:00
|
|
|
isTextElement(element) &&
|
2020-08-10 14:16:39 +02:00
|
|
|
(transformHandleType === "nw" ||
|
|
|
|
transformHandleType === "ne" ||
|
|
|
|
transformHandleType === "sw" ||
|
|
|
|
transformHandleType === "se")
|
2020-05-28 07:17:15 +09:00
|
|
|
) {
|
|
|
|
resizeSingleTextElement(
|
|
|
|
element,
|
2020-08-10 14:16:39 +02:00
|
|
|
transformHandleType,
|
2021-10-21 22:05:48 +02:00
|
|
|
shouldResizeFromCenter,
|
2020-05-28 07:17:15 +09:00
|
|
|
pointerX,
|
|
|
|
pointerY,
|
|
|
|
);
|
2020-09-08 12:03:49 -04:00
|
|
|
updateBoundElements(element);
|
2020-08-10 14:16:39 +02:00
|
|
|
} else if (transformHandleType) {
|
2021-01-16 18:49:13 +00:00
|
|
|
resizeSingleElement(
|
2022-02-22 18:45:59 +05:30
|
|
|
pointerDownState.originalElements,
|
2021-10-21 22:05:48 +02:00
|
|
|
shouldMaintainAspectRatio,
|
2021-01-16 18:49:13 +00:00
|
|
|
element,
|
|
|
|
transformHandleType,
|
2021-10-21 22:05:48 +02:00
|
|
|
shouldResizeFromCenter,
|
2021-01-16 18:49:13 +00:00
|
|
|
pointerX,
|
|
|
|
pointerY,
|
|
|
|
);
|
2020-05-05 00:25:40 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2020-07-26 19:21:38 +09:00
|
|
|
} else if (selectedElements.length > 1) {
|
2020-08-10 14:16:39 +02:00
|
|
|
if (transformHandleType === "rotation") {
|
2020-07-26 19:21:38 +09:00
|
|
|
rotateMultipleElements(
|
2020-09-11 17:22:40 +02:00
|
|
|
pointerDownState,
|
2020-07-26 19:21:38 +09:00
|
|
|
selectedElements,
|
|
|
|
pointerX,
|
|
|
|
pointerY,
|
2021-10-21 22:05:48 +02:00
|
|
|
shouldRotateWithDiscreteAngle,
|
2020-07-26 19:21:38 +09:00
|
|
|
centerX,
|
|
|
|
centerY,
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
} else if (
|
2020-08-10 14:16:39 +02:00
|
|
|
transformHandleType === "nw" ||
|
|
|
|
transformHandleType === "ne" ||
|
|
|
|
transformHandleType === "sw" ||
|
|
|
|
transformHandleType === "se"
|
2020-07-26 19:21:38 +09:00
|
|
|
) {
|
|
|
|
resizeMultipleElements(
|
2022-08-13 22:53:10 +05:00
|
|
|
pointerDownState,
|
2020-07-26 19:21:38 +09:00
|
|
|
selectedElements,
|
2020-08-10 14:16:39 +02:00
|
|
|
transformHandleType,
|
2022-08-13 22:53:10 +05:00
|
|
|
shouldResizeFromCenter,
|
2020-07-26 19:21:38 +09:00
|
|
|
pointerX,
|
|
|
|
pointerY,
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
2020-05-05 00:25:40 +09:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
const rotateSingleElement = (
|
|
|
|
element: NonDeletedExcalidrawElement,
|
2020-05-09 17:57:00 +09:00
|
|
|
pointerX: number,
|
|
|
|
pointerY: number,
|
2021-10-21 22:05:48 +02:00
|
|
|
shouldRotateWithDiscreteAngle: boolean,
|
2022-12-05 21:03:13 +05:30
|
|
|
originalElements: Map<string, NonDeleted<ExcalidrawElement>>,
|
2020-05-05 00:25:40 +09:00
|
|
|
) => {
|
|
|
|
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element);
|
|
|
|
const cx = (x1 + x2) / 2;
|
|
|
|
const cy = (y1 + y2) / 2;
|
2023-06-15 00:42:01 +08:00
|
|
|
let angle: number;
|
|
|
|
if (isFrameElement(element)) {
|
|
|
|
angle = 0;
|
|
|
|
} else {
|
|
|
|
angle = (5 * Math.PI) / 2 + Math.atan2(pointerY - cy, pointerX - cx);
|
|
|
|
if (shouldRotateWithDiscreteAngle) {
|
|
|
|
angle += SHIFT_LOCKING_ANGLE / 2;
|
|
|
|
angle -= angle % SHIFT_LOCKING_ANGLE;
|
|
|
|
}
|
|
|
|
angle = normalizeAngle(angle);
|
2020-05-05 00:25:40 +09:00
|
|
|
}
|
2021-12-16 21:14:03 +05:30
|
|
|
const boundTextElementId = getBoundTextElementId(element);
|
2022-12-05 21:03:13 +05:30
|
|
|
|
|
|
|
mutateElement(element, { angle });
|
2021-12-16 21:14:03 +05:30
|
|
|
if (boundTextElementId) {
|
2022-12-18 23:06:01 +01:00
|
|
|
const textElement =
|
|
|
|
Scene.getScene(element)?.getElement<ExcalidrawTextElementWithContainer>(
|
|
|
|
boundTextElementId,
|
|
|
|
);
|
2022-12-05 21:03:13 +05:30
|
|
|
|
2022-12-18 23:06:01 +01:00
|
|
|
if (textElement && !isArrowElement(element)) {
|
2022-12-05 21:03:13 +05:30
|
|
|
mutateElement(textElement, { angle });
|
|
|
|
}
|
2021-12-16 21:14:03 +05:30
|
|
|
}
|
2020-05-05 00:25:40 +09:00
|
|
|
};
|
|
|
|
|
2020-05-14 23:56:14 +09:00
|
|
|
const rescalePointsInElement = (
|
|
|
|
element: NonDeletedExcalidrawElement,
|
|
|
|
width: number,
|
|
|
|
height: number,
|
2022-08-16 21:51:43 +02:00
|
|
|
normalizePoints: boolean,
|
2020-05-14 23:56:14 +09:00
|
|
|
) =>
|
2021-05-09 16:42:10 +01:00
|
|
|
isLinearElement(element) || isFreeDrawElement(element)
|
2020-05-14 23:56:14 +09:00
|
|
|
? {
|
|
|
|
points: rescalePoints(
|
|
|
|
0,
|
|
|
|
width,
|
2022-08-16 21:51:43 +02:00
|
|
|
rescalePoints(1, height, element.points, normalizePoints),
|
|
|
|
normalizePoints,
|
2020-05-14 23:56:14 +09:00
|
|
|
),
|
|
|
|
}
|
|
|
|
: {};
|
|
|
|
|
2023-02-23 16:33:10 +05:30
|
|
|
const measureFontSizeFromWidth = (
|
2020-06-08 18:25:20 +09:00
|
|
|
element: NonDeleted<ExcalidrawTextElement>,
|
|
|
|
nextWidth: number,
|
2023-04-10 18:52:46 +05:30
|
|
|
nextHeight: number,
|
|
|
|
): { size: number; baseline: number } | null => {
|
2020-07-09 22:22:10 +09:00
|
|
|
// We only use width to scale font on resize
|
2022-11-30 15:55:01 +05:30
|
|
|
let width = element.width;
|
|
|
|
|
|
|
|
const hasContainer = isBoundToContainer(element);
|
|
|
|
if (hasContainer) {
|
2022-12-18 23:06:01 +01:00
|
|
|
const container = getContainerElement(element);
|
|
|
|
if (container) {
|
2023-04-25 18:06:23 +05:30
|
|
|
width = getBoundTextMaxWidth(container);
|
2022-12-18 23:06:01 +01:00
|
|
|
}
|
2022-11-30 15:55:01 +05:30
|
|
|
}
|
|
|
|
const nextFontSize = element.fontSize * (nextWidth / width);
|
2020-07-09 22:22:10 +09:00
|
|
|
if (nextFontSize < MIN_FONT_SIZE) {
|
|
|
|
return null;
|
2020-06-08 18:25:20 +09:00
|
|
|
}
|
2023-04-10 18:52:46 +05:30
|
|
|
const metrics = measureText(
|
|
|
|
element.text,
|
|
|
|
getFontString({ fontSize: nextFontSize, fontFamily: element.fontFamily }),
|
|
|
|
element.lineHeight,
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
size: nextFontSize,
|
|
|
|
baseline: metrics.baseline + (nextHeight - metrics.height),
|
|
|
|
};
|
2020-06-08 18:25:20 +09:00
|
|
|
};
|
|
|
|
|
2020-08-10 14:16:39 +02:00
|
|
|
const getSidesForTransformHandle = (
|
|
|
|
transformHandleType: TransformHandleType,
|
2021-10-21 22:05:48 +02:00
|
|
|
shouldResizeFromCenter: boolean,
|
2020-06-25 21:21:27 +02:00
|
|
|
) => {
|
|
|
|
return {
|
|
|
|
n:
|
2020-08-10 14:16:39 +02:00
|
|
|
/^(n|ne|nw)$/.test(transformHandleType) ||
|
2021-10-21 22:05:48 +02:00
|
|
|
(shouldResizeFromCenter && /^(s|se|sw)$/.test(transformHandleType)),
|
2020-06-25 21:21:27 +02:00
|
|
|
s:
|
2020-08-10 14:16:39 +02:00
|
|
|
/^(s|se|sw)$/.test(transformHandleType) ||
|
2021-10-21 22:05:48 +02:00
|
|
|
(shouldResizeFromCenter && /^(n|ne|nw)$/.test(transformHandleType)),
|
2020-06-25 21:21:27 +02:00
|
|
|
w:
|
2020-08-10 14:16:39 +02:00
|
|
|
/^(w|nw|sw)$/.test(transformHandleType) ||
|
2021-10-21 22:05:48 +02:00
|
|
|
(shouldResizeFromCenter && /^(e|ne|se)$/.test(transformHandleType)),
|
2020-06-25 21:21:27 +02:00
|
|
|
e:
|
2020-08-10 14:16:39 +02:00
|
|
|
/^(e|ne|se)$/.test(transformHandleType) ||
|
2021-10-21 22:05:48 +02:00
|
|
|
(shouldResizeFromCenter && /^(w|nw|sw)$/.test(transformHandleType)),
|
2020-06-25 21:21:27 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-05-28 07:17:15 +09:00
|
|
|
const resizeSingleTextElement = (
|
|
|
|
element: NonDeleted<ExcalidrawTextElement>,
|
2020-08-10 14:16:39 +02:00
|
|
|
transformHandleType: "nw" | "ne" | "sw" | "se",
|
2021-10-21 22:05:48 +02:00
|
|
|
shouldResizeFromCenter: boolean,
|
2020-05-28 07:17:15 +09:00
|
|
|
pointerX: number,
|
|
|
|
pointerY: number,
|
|
|
|
) => {
|
|
|
|
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element);
|
|
|
|
const cx = (x1 + x2) / 2;
|
|
|
|
const cy = (y1 + y2) / 2;
|
|
|
|
// rotation pointer with reverse angle
|
|
|
|
const [rotatedX, rotatedY] = rotate(
|
|
|
|
pointerX,
|
|
|
|
pointerY,
|
|
|
|
cx,
|
|
|
|
cy,
|
|
|
|
-element.angle,
|
|
|
|
);
|
2020-11-06 22:06:30 +02:00
|
|
|
let scale: number;
|
2020-08-10 14:16:39 +02:00
|
|
|
switch (transformHandleType) {
|
2020-05-28 07:17:15 +09:00
|
|
|
case "se":
|
|
|
|
scale = Math.max(
|
|
|
|
(rotatedX - x1) / (x2 - x1),
|
|
|
|
(rotatedY - y1) / (y2 - y1),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case "nw":
|
|
|
|
scale = Math.max(
|
|
|
|
(x2 - rotatedX) / (x2 - x1),
|
|
|
|
(y2 - rotatedY) / (y2 - y1),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case "ne":
|
|
|
|
scale = Math.max(
|
|
|
|
(rotatedX - x1) / (x2 - x1),
|
|
|
|
(y2 - rotatedY) / (y2 - y1),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case "sw":
|
|
|
|
scale = Math.max(
|
|
|
|
(x2 - rotatedX) / (x2 - x1),
|
|
|
|
(rotatedY - y1) / (y2 - y1),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (scale > 0) {
|
2020-06-08 18:25:20 +09:00
|
|
|
const nextWidth = element.width * scale;
|
|
|
|
const nextHeight = element.height * scale;
|
2023-04-10 18:52:46 +05:30
|
|
|
const metrics = measureFontSizeFromWidth(element, nextWidth, nextHeight);
|
|
|
|
if (metrics === null) {
|
2020-05-28 07:17:15 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const [nextX1, nextY1, nextX2, nextY2] = getResizedElementAbsoluteCoords(
|
|
|
|
element,
|
2020-06-08 18:25:20 +09:00
|
|
|
nextWidth,
|
|
|
|
nextHeight,
|
2022-08-16 21:51:43 +02:00
|
|
|
false,
|
2020-05-28 07:17:15 +09:00
|
|
|
);
|
|
|
|
const deltaX1 = (x1 - nextX1) / 2;
|
|
|
|
const deltaY1 = (y1 - nextY1) / 2;
|
|
|
|
const deltaX2 = (x2 - nextX2) / 2;
|
|
|
|
const deltaY2 = (y2 - nextY2) / 2;
|
|
|
|
const [nextElementX, nextElementY] = adjustXYWithRotation(
|
2021-10-21 22:05:48 +02:00
|
|
|
getSidesForTransformHandle(transformHandleType, shouldResizeFromCenter),
|
2020-05-28 07:17:15 +09:00
|
|
|
element.x,
|
|
|
|
element.y,
|
|
|
|
element.angle,
|
|
|
|
deltaX1,
|
|
|
|
deltaY1,
|
|
|
|
deltaX2,
|
|
|
|
deltaY2,
|
|
|
|
);
|
|
|
|
mutateElement(element, {
|
2023-04-10 18:52:46 +05:30
|
|
|
fontSize: metrics.size,
|
2020-06-08 18:25:20 +09:00
|
|
|
width: nextWidth,
|
|
|
|
height: nextHeight,
|
2023-04-10 18:52:46 +05:30
|
|
|
baseline: metrics.baseline,
|
2020-05-28 07:17:15 +09:00
|
|
|
x: nextElementX,
|
|
|
|
y: nextElementY,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-26 11:45:08 -04:00
|
|
|
export const resizeSingleElement = (
|
2022-02-22 18:45:59 +05:30
|
|
|
originalElements: PointerDownState["originalElements"],
|
2021-10-21 22:05:48 +02:00
|
|
|
shouldMaintainAspectRatio: boolean,
|
2020-05-05 00:25:40 +09:00
|
|
|
element: NonDeletedExcalidrawElement,
|
2020-12-06 22:39:31 +00:00
|
|
|
transformHandleDirection: TransformHandleDirection,
|
2021-10-21 22:05:48 +02:00
|
|
|
shouldResizeFromCenter: boolean,
|
2020-12-06 22:39:31 +00:00
|
|
|
pointerX: number,
|
|
|
|
pointerY: number,
|
|
|
|
) => {
|
2022-02-22 18:45:59 +05:30
|
|
|
const stateAtResizeStart = originalElements.get(element.id)!;
|
2021-01-16 18:49:13 +00:00
|
|
|
// Gets bounds corners
|
|
|
|
const [x1, y1, x2, y2] = getResizedElementAbsoluteCoords(
|
|
|
|
stateAtResizeStart,
|
|
|
|
stateAtResizeStart.width,
|
|
|
|
stateAtResizeStart.height,
|
2022-08-16 21:51:43 +02:00
|
|
|
true,
|
2021-01-16 18:49:13 +00:00
|
|
|
);
|
2020-12-06 22:39:31 +00:00
|
|
|
const startTopLeft: Point = [x1, y1];
|
|
|
|
const startBottomRight: Point = [x2, y2];
|
|
|
|
const startCenter: Point = centerPoint(startTopLeft, startBottomRight);
|
|
|
|
|
|
|
|
// Calculate new dimensions based on cursor position
|
|
|
|
const rotatedPointer = rotatePoint(
|
|
|
|
[pointerX, pointerY],
|
|
|
|
startCenter,
|
|
|
|
-stateAtResizeStart.angle,
|
|
|
|
);
|
2021-01-16 18:49:13 +00:00
|
|
|
|
2021-05-09 16:42:10 +01:00
|
|
|
// Get bounds corners rendered on screen
|
2021-01-16 18:49:13 +00:00
|
|
|
const [esx1, esy1, esx2, esy2] = getResizedElementAbsoluteCoords(
|
|
|
|
element,
|
|
|
|
element.width,
|
|
|
|
element.height,
|
2022-08-16 21:51:43 +02:00
|
|
|
true,
|
2021-01-16 18:49:13 +00:00
|
|
|
);
|
2021-12-16 21:14:03 +05:30
|
|
|
|
2021-01-16 18:49:13 +00:00
|
|
|
const boundsCurrentWidth = esx2 - esx1;
|
|
|
|
const boundsCurrentHeight = esy2 - esy1;
|
|
|
|
|
|
|
|
// It's important we set the initial scale value based on the width and height at resize start,
|
|
|
|
// otherwise previous dimensions affected by modifiers will be taken into account.
|
|
|
|
const atStartBoundsWidth = startBottomRight[0] - startTopLeft[0];
|
|
|
|
const atStartBoundsHeight = startBottomRight[1] - startTopLeft[1];
|
|
|
|
let scaleX = atStartBoundsWidth / boundsCurrentWidth;
|
|
|
|
let scaleY = atStartBoundsHeight / boundsCurrentHeight;
|
|
|
|
|
2023-04-10 18:52:46 +05:30
|
|
|
let boundTextFont: { fontSize?: number; baseline?: number } = {};
|
2022-02-22 18:45:59 +05:30
|
|
|
const boundTextElement = getBoundTextElement(element);
|
|
|
|
|
2020-12-06 22:39:31 +00:00
|
|
|
if (transformHandleDirection.includes("e")) {
|
2021-01-16 18:49:13 +00:00
|
|
|
scaleX = (rotatedPointer[0] - startTopLeft[0]) / boundsCurrentWidth;
|
2020-12-06 22:39:31 +00:00
|
|
|
}
|
|
|
|
if (transformHandleDirection.includes("s")) {
|
2021-01-16 18:49:13 +00:00
|
|
|
scaleY = (rotatedPointer[1] - startTopLeft[1]) / boundsCurrentHeight;
|
2020-12-06 22:39:31 +00:00
|
|
|
}
|
|
|
|
if (transformHandleDirection.includes("w")) {
|
2021-01-16 18:49:13 +00:00
|
|
|
scaleX = (startBottomRight[0] - rotatedPointer[0]) / boundsCurrentWidth;
|
2020-12-06 22:39:31 +00:00
|
|
|
}
|
|
|
|
if (transformHandleDirection.includes("n")) {
|
2021-01-16 18:49:13 +00:00
|
|
|
scaleY = (startBottomRight[1] - rotatedPointer[1]) / boundsCurrentHeight;
|
2020-12-06 22:39:31 +00:00
|
|
|
}
|
2022-02-18 18:20:55 +05:30
|
|
|
|
2021-01-16 18:49:13 +00:00
|
|
|
// Linear elements dimensions differ from bounds dimensions
|
|
|
|
const eleInitialWidth = stateAtResizeStart.width;
|
|
|
|
const eleInitialHeight = stateAtResizeStart.height;
|
|
|
|
// We have to use dimensions of element on screen, otherwise the scaling of the
|
|
|
|
// dimensions won't match the cursor for linear elements.
|
|
|
|
let eleNewWidth = element.width * scaleX;
|
|
|
|
let eleNewHeight = element.height * scaleY;
|
2020-12-06 22:39:31 +00:00
|
|
|
|
|
|
|
// adjust dimensions for resizing from center
|
2021-10-21 22:05:48 +02:00
|
|
|
if (shouldResizeFromCenter) {
|
2021-01-16 18:49:13 +00:00
|
|
|
eleNewWidth = 2 * eleNewWidth - eleInitialWidth;
|
|
|
|
eleNewHeight = 2 * eleNewHeight - eleInitialHeight;
|
2020-12-06 22:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// adjust dimensions to keep sides ratio
|
2021-10-21 22:05:48 +02:00
|
|
|
if (shouldMaintainAspectRatio) {
|
2021-01-16 18:49:13 +00:00
|
|
|
const widthRatio = Math.abs(eleNewWidth) / eleInitialWidth;
|
|
|
|
const heightRatio = Math.abs(eleNewHeight) / eleInitialHeight;
|
2020-12-06 22:39:31 +00:00
|
|
|
if (transformHandleDirection.length === 1) {
|
2021-01-16 18:49:13 +00:00
|
|
|
eleNewHeight *= widthRatio;
|
|
|
|
eleNewWidth *= heightRatio;
|
2020-12-06 22:39:31 +00:00
|
|
|
}
|
|
|
|
if (transformHandleDirection.length === 2) {
|
|
|
|
const ratio = Math.max(widthRatio, heightRatio);
|
2021-01-16 18:49:13 +00:00
|
|
|
eleNewWidth = eleInitialWidth * ratio * Math.sign(eleNewWidth);
|
|
|
|
eleNewHeight = eleInitialHeight * ratio * Math.sign(eleNewHeight);
|
2020-12-06 22:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-18 18:20:55 +05:30
|
|
|
if (boundTextElement) {
|
2022-02-22 18:45:59 +05:30
|
|
|
const stateOfBoundTextElementAtResize = originalElements.get(
|
|
|
|
boundTextElement.id,
|
|
|
|
) as typeof boundTextElement | undefined;
|
|
|
|
if (stateOfBoundTextElementAtResize) {
|
2023-04-10 18:52:46 +05:30
|
|
|
boundTextFont = {
|
|
|
|
fontSize: stateOfBoundTextElementAtResize.fontSize,
|
|
|
|
baseline: stateOfBoundTextElementAtResize.baseline,
|
|
|
|
};
|
2022-02-22 18:45:59 +05:30
|
|
|
}
|
|
|
|
if (shouldMaintainAspectRatio) {
|
2023-02-22 16:28:12 +05:30
|
|
|
const updatedElement = {
|
|
|
|
...element,
|
|
|
|
width: eleNewWidth,
|
|
|
|
height: eleNewHeight,
|
|
|
|
};
|
|
|
|
|
2023-04-10 18:52:46 +05:30
|
|
|
const nextFont = measureFontSizeFromWidth(
|
2022-02-22 18:45:59 +05:30
|
|
|
boundTextElement,
|
2023-04-25 18:06:23 +05:30
|
|
|
getBoundTextMaxWidth(updatedElement),
|
|
|
|
getBoundTextMaxHeight(updatedElement, boundTextElement),
|
2022-02-22 18:45:59 +05:30
|
|
|
);
|
2023-04-10 18:52:46 +05:30
|
|
|
if (nextFont === null) {
|
2022-02-22 18:45:59 +05:30
|
|
|
return;
|
|
|
|
}
|
2023-04-10 18:52:46 +05:30
|
|
|
boundTextFont = {
|
|
|
|
fontSize: nextFont.size,
|
|
|
|
baseline: nextFont.baseline,
|
|
|
|
};
|
2022-02-22 18:45:59 +05:30
|
|
|
} else {
|
2023-03-22 11:32:38 +05:30
|
|
|
const minWidth = getApproxMinLineWidth(
|
|
|
|
getFontString(boundTextElement),
|
|
|
|
boundTextElement.lineHeight,
|
|
|
|
);
|
|
|
|
const minHeight = getApproxMinLineHeight(
|
|
|
|
boundTextElement.fontSize,
|
|
|
|
boundTextElement.lineHeight,
|
|
|
|
);
|
2022-02-22 18:45:59 +05:30
|
|
|
eleNewWidth = Math.ceil(Math.max(eleNewWidth, minWidth));
|
|
|
|
eleNewHeight = Math.ceil(Math.max(eleNewHeight, minHeight));
|
|
|
|
}
|
2022-02-18 18:20:55 +05:30
|
|
|
}
|
|
|
|
|
2021-11-01 15:24:05 +02:00
|
|
|
const [newBoundsX1, newBoundsY1, newBoundsX2, newBoundsY2] =
|
|
|
|
getResizedElementAbsoluteCoords(
|
|
|
|
stateAtResizeStart,
|
|
|
|
eleNewWidth,
|
|
|
|
eleNewHeight,
|
2022-08-16 21:51:43 +02:00
|
|
|
true,
|
2021-11-01 15:24:05 +02:00
|
|
|
);
|
2021-01-16 18:49:13 +00:00
|
|
|
const newBoundsWidth = newBoundsX2 - newBoundsX1;
|
|
|
|
const newBoundsHeight = newBoundsY2 - newBoundsY1;
|
|
|
|
|
2020-12-06 22:39:31 +00:00
|
|
|
// Calculate new topLeft based on fixed corner during resize
|
2021-01-16 18:49:13 +00:00
|
|
|
let newTopLeft = [...startTopLeft] as [number, number];
|
2020-12-06 22:39:31 +00:00
|
|
|
if (["n", "w", "nw"].includes(transformHandleDirection)) {
|
|
|
|
newTopLeft = [
|
2021-01-16 18:49:13 +00:00
|
|
|
startBottomRight[0] - Math.abs(newBoundsWidth),
|
|
|
|
startBottomRight[1] - Math.abs(newBoundsHeight),
|
2020-12-06 22:39:31 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
if (transformHandleDirection === "ne") {
|
2021-01-16 18:49:13 +00:00
|
|
|
const bottomLeft = [startTopLeft[0], startBottomRight[1]];
|
|
|
|
newTopLeft = [bottomLeft[0], bottomLeft[1] - Math.abs(newBoundsHeight)];
|
2020-12-06 22:39:31 +00:00
|
|
|
}
|
|
|
|
if (transformHandleDirection === "sw") {
|
2021-01-16 18:49:13 +00:00
|
|
|
const topRight = [startBottomRight[0], startTopLeft[1]];
|
|
|
|
newTopLeft = [topRight[0] - Math.abs(newBoundsWidth), topRight[1]];
|
2020-12-06 22:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Keeps opposite handle fixed during resize
|
2021-10-21 22:05:48 +02:00
|
|
|
if (shouldMaintainAspectRatio) {
|
2020-12-06 22:39:31 +00:00
|
|
|
if (["s", "n"].includes(transformHandleDirection)) {
|
2021-01-16 18:49:13 +00:00
|
|
|
newTopLeft[0] = startCenter[0] - newBoundsWidth / 2;
|
2020-12-06 22:39:31 +00:00
|
|
|
}
|
|
|
|
if (["e", "w"].includes(transformHandleDirection)) {
|
2021-01-16 18:49:13 +00:00
|
|
|
newTopLeft[1] = startCenter[1] - newBoundsHeight / 2;
|
2020-12-06 22:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flip horizontally
|
2021-01-16 18:49:13 +00:00
|
|
|
if (eleNewWidth < 0) {
|
2020-12-06 22:39:31 +00:00
|
|
|
if (transformHandleDirection.includes("e")) {
|
2021-01-16 18:49:13 +00:00
|
|
|
newTopLeft[0] -= Math.abs(newBoundsWidth);
|
2020-12-06 22:39:31 +00:00
|
|
|
}
|
|
|
|
if (transformHandleDirection.includes("w")) {
|
2021-01-16 18:49:13 +00:00
|
|
|
newTopLeft[0] += Math.abs(newBoundsWidth);
|
2020-12-06 22:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Flip vertically
|
2021-01-16 18:49:13 +00:00
|
|
|
if (eleNewHeight < 0) {
|
2020-12-06 22:39:31 +00:00
|
|
|
if (transformHandleDirection.includes("s")) {
|
2021-01-16 18:49:13 +00:00
|
|
|
newTopLeft[1] -= Math.abs(newBoundsHeight);
|
2020-12-06 22:39:31 +00:00
|
|
|
}
|
|
|
|
if (transformHandleDirection.includes("n")) {
|
2021-01-16 18:49:13 +00:00
|
|
|
newTopLeft[1] += Math.abs(newBoundsHeight);
|
2020-12-06 22:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 22:05:48 +02:00
|
|
|
if (shouldResizeFromCenter) {
|
2021-01-16 18:49:13 +00:00
|
|
|
newTopLeft[0] = startCenter[0] - Math.abs(newBoundsWidth) / 2;
|
|
|
|
newTopLeft[1] = startCenter[1] - Math.abs(newBoundsHeight) / 2;
|
2020-12-06 22:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// adjust topLeft to new rotation point
|
|
|
|
const angle = stateAtResizeStart.angle;
|
|
|
|
const rotatedTopLeft = rotatePoint(newTopLeft, startCenter, angle);
|
|
|
|
const newCenter: Point = [
|
2021-01-16 18:49:13 +00:00
|
|
|
newTopLeft[0] + Math.abs(newBoundsWidth) / 2,
|
|
|
|
newTopLeft[1] + Math.abs(newBoundsHeight) / 2,
|
2020-12-06 22:39:31 +00:00
|
|
|
];
|
|
|
|
const rotatedNewCenter = rotatePoint(newCenter, startCenter, angle);
|
|
|
|
newTopLeft = rotatePoint(rotatedTopLeft, rotatedNewCenter, -angle);
|
|
|
|
|
2021-01-16 18:49:13 +00:00
|
|
|
// Readjust points for linear elements
|
2022-12-05 21:03:13 +05:30
|
|
|
let rescaledElementPointsY;
|
|
|
|
let rescaledPoints;
|
|
|
|
|
|
|
|
if (isLinearElement(element) || isFreeDrawElement(element)) {
|
|
|
|
rescaledElementPointsY = rescalePoints(
|
|
|
|
1,
|
|
|
|
eleNewHeight,
|
|
|
|
(stateAtResizeStart as ExcalidrawLinearElement).points,
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
|
|
|
|
rescaledPoints = rescalePoints(
|
|
|
|
0,
|
|
|
|
eleNewWidth,
|
|
|
|
rescaledElementPointsY,
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-16 18:49:13 +00:00
|
|
|
// 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
|
|
|
|
const newOrigin = [...newTopLeft];
|
|
|
|
newOrigin[0] += stateAtResizeStart.x - newBoundsX1;
|
|
|
|
newOrigin[1] += stateAtResizeStart.y - newBoundsY1;
|
|
|
|
const resizedElement = {
|
|
|
|
width: Math.abs(eleNewWidth),
|
|
|
|
height: Math.abs(eleNewHeight),
|
|
|
|
x: newOrigin[0],
|
|
|
|
y: newOrigin[1],
|
2022-12-05 21:03:13 +05:30
|
|
|
points: rescaledPoints,
|
2021-01-16 18:49:13 +00:00
|
|
|
};
|
2020-12-06 22:39:31 +00:00
|
|
|
|
2021-10-21 22:05:48 +02:00
|
|
|
if ("scale" in element && "scale" in stateAtResizeStart) {
|
|
|
|
mutateElement(element, {
|
|
|
|
scale: [
|
|
|
|
// defaulting because scaleX/Y can be 0/-0
|
2023-01-08 17:19:13 +01:00
|
|
|
(Math.sign(newBoundsX2 - stateAtResizeStart.x) ||
|
|
|
|
stateAtResizeStart.scale[0]) * stateAtResizeStart.scale[0],
|
|
|
|
(Math.sign(newBoundsY2 - stateAtResizeStart.y) ||
|
|
|
|
stateAtResizeStart.scale[1]) * stateAtResizeStart.scale[1],
|
2021-10-21 22:05:48 +02:00
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
2022-02-18 18:20:55 +05:30
|
|
|
|
2023-08-02 15:04:21 +05:00
|
|
|
if (
|
|
|
|
isArrowElement(element) &&
|
|
|
|
boundTextElement &&
|
|
|
|
shouldMaintainAspectRatio
|
|
|
|
) {
|
|
|
|
const fontSize =
|
|
|
|
(resizedElement.width / element.width) * boundTextElement.fontSize;
|
|
|
|
if (fontSize < MIN_FONT_SIZE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
boundTextFont.fontSize = fontSize;
|
|
|
|
}
|
|
|
|
|
2020-05-05 00:25:40 +09:00
|
|
|
if (
|
2022-02-18 18:20:55 +05:30
|
|
|
resizedElement.width !== 0 &&
|
2021-01-16 18:49:13 +00:00
|
|
|
resizedElement.height !== 0 &&
|
|
|
|
Number.isFinite(resizedElement.x) &&
|
|
|
|
Number.isFinite(resizedElement.y)
|
2020-05-05 00:25:40 +09:00
|
|
|
) {
|
2023-08-02 15:04:21 +05:00
|
|
|
mutateElement(element, resizedElement);
|
|
|
|
|
2021-01-16 18:49:13 +00:00
|
|
|
updateBoundElements(element, {
|
|
|
|
newSize: { width: resizedElement.width, height: resizedElement.height },
|
2020-05-05 00:25:40 +09:00
|
|
|
});
|
2022-12-05 21:03:13 +05:30
|
|
|
|
2023-04-10 18:52:46 +05:30
|
|
|
if (boundTextElement && boundTextFont != null) {
|
2023-03-22 11:32:38 +05:30
|
|
|
mutateElement(boundTextElement, {
|
2023-04-10 18:52:46 +05:30
|
|
|
fontSize: boundTextFont.fontSize,
|
|
|
|
baseline: boundTextFont.baseline,
|
2023-03-22 11:32:38 +05:30
|
|
|
});
|
2022-02-22 18:45:59 +05:30
|
|
|
}
|
2023-08-02 15:04:21 +05:00
|
|
|
handleBindTextResize(
|
|
|
|
element,
|
|
|
|
transformHandleDirection,
|
|
|
|
shouldMaintainAspectRatio,
|
|
|
|
);
|
2020-05-05 00:25:40 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-05-25 19:27:41 +05:00
|
|
|
export const resizeMultipleElements = (
|
2022-08-13 22:53:10 +05:00
|
|
|
pointerDownState: PointerDownState,
|
|
|
|
selectedElements: readonly NonDeletedExcalidrawElement[],
|
2020-08-10 14:16:39 +02:00
|
|
|
transformHandleType: "nw" | "ne" | "sw" | "se",
|
2022-08-13 22:53:10 +05:00
|
|
|
shouldResizeFromCenter: boolean,
|
2020-05-09 17:57:00 +09:00
|
|
|
pointerX: number,
|
|
|
|
pointerY: number,
|
2020-05-05 00:25:40 +09:00
|
|
|
) => {
|
2022-08-13 22:53:10 +05:00
|
|
|
// map selected elements to the original elements. While it never should
|
|
|
|
// happen that pointerDownState.originalElements won't contain the selected
|
|
|
|
// elements during resize, this coupling isn't guaranteed, so to ensure
|
|
|
|
// type safety we need to transform only those elements we filter.
|
|
|
|
const targetElements = selectedElements.reduce(
|
|
|
|
(
|
|
|
|
acc: {
|
|
|
|
/** element at resize start */
|
|
|
|
orig: NonDeletedExcalidrawElement;
|
|
|
|
/** latest element */
|
|
|
|
latest: NonDeletedExcalidrawElement;
|
|
|
|
}[],
|
|
|
|
element,
|
|
|
|
) => {
|
|
|
|
const origElement = pointerDownState.originalElements.get(element.id);
|
|
|
|
if (origElement) {
|
|
|
|
acc.push({ orig: origElement, latest: element });
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
|
2023-05-25 19:27:41 +05:00
|
|
|
// getCommonBoundingBox() uses getBoundTextElement() which returns null for
|
|
|
|
// original elements from pointerDownState, so we have to find and add these
|
|
|
|
// bound text elements manually. Additionally, the coordinates of bound text
|
|
|
|
// elements aren't always up to date.
|
|
|
|
const boundTextElements = targetElements.reduce((acc, { orig }) => {
|
|
|
|
if (!isLinearElement(orig)) {
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
const textId = getBoundTextElementId(orig);
|
|
|
|
if (!textId) {
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
const text = pointerDownState.originalElements.get(textId) ?? null;
|
|
|
|
if (!isBoundToContainer(text)) {
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
const xy = LinearElementEditor.getBoundTextElementPosition(orig, text);
|
|
|
|
return [...acc, { ...text, ...xy }];
|
|
|
|
}, [] as ExcalidrawTextElementWithContainer[]);
|
|
|
|
|
2022-08-13 22:53:10 +05:00
|
|
|
const { minX, minY, maxX, maxY, midX, midY } = getCommonBoundingBox(
|
2023-05-25 19:27:41 +05:00
|
|
|
targetElements.map(({ orig }) => orig).concat(boundTextElements),
|
2022-08-13 22:53:10 +05:00
|
|
|
);
|
|
|
|
const direction = transformHandleType;
|
|
|
|
|
|
|
|
const mapDirectionsToAnchors: Record<typeof direction, Point> = {
|
|
|
|
ne: [minX, maxY],
|
|
|
|
se: [minX, minY],
|
|
|
|
sw: [maxX, minY],
|
|
|
|
nw: [maxX, maxY],
|
|
|
|
};
|
|
|
|
|
|
|
|
// anchor point must be on the opposite side of the dragged selection handle
|
2023-05-25 19:27:41 +05:00
|
|
|
// or be the center of the selection if shouldResizeFromCenter
|
2022-08-13 22:53:10 +05:00
|
|
|
const [anchorX, anchorY]: Point = shouldResizeFromCenter
|
|
|
|
? [midX, midY]
|
|
|
|
: mapDirectionsToAnchors[direction];
|
|
|
|
|
2023-05-25 19:27:41 +05:00
|
|
|
const scale =
|
|
|
|
Math.max(
|
|
|
|
Math.abs(pointerX - anchorX) / (maxX - minX) || 0,
|
|
|
|
Math.abs(pointerY - anchorY) / (maxY - minY) || 0,
|
|
|
|
) * (shouldResizeFromCenter ? 2 : 1);
|
|
|
|
|
|
|
|
if (scale === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapDirectionsToPointerPositions: Record<
|
2022-08-13 22:53:10 +05:00
|
|
|
typeof direction,
|
|
|
|
[x: boolean, y: boolean]
|
|
|
|
> = {
|
|
|
|
ne: [pointerX >= anchorX, pointerY <= anchorY],
|
|
|
|
se: [pointerX >= anchorX, pointerY >= anchorY],
|
|
|
|
sw: [pointerX <= anchorX, pointerY >= anchorY],
|
|
|
|
nw: [pointerX <= anchorX, pointerY <= anchorY],
|
|
|
|
};
|
|
|
|
|
2023-05-25 19:27:41 +05:00
|
|
|
/**
|
|
|
|
* to flip an element:
|
|
|
|
* 1. determine over which axis is the element being flipped
|
|
|
|
* (could be x, y, or both) indicated by `flipFactorX` & `flipFactorY`
|
|
|
|
* 2. shift element's position by the amount of width or height (or both) or
|
|
|
|
* mirror points in the case of linear & freedraw elemenets
|
|
|
|
* 3. adjust element angle
|
|
|
|
*/
|
|
|
|
const [flipFactorX, flipFactorY] = mapDirectionsToPointerPositions[
|
2022-08-13 22:53:10 +05:00
|
|
|
direction
|
|
|
|
].map((condition) => (condition ? 1 : -1));
|
2023-05-25 19:27:41 +05:00
|
|
|
const isFlippedByX = flipFactorX < 0;
|
|
|
|
const isFlippedByY = flipFactorY < 0;
|
|
|
|
|
|
|
|
const elementsAndUpdates: {
|
|
|
|
element: NonDeletedExcalidrawElement;
|
|
|
|
update: Mutable<
|
|
|
|
Pick<ExcalidrawElement, "x" | "y" | "width" | "height" | "angle">
|
|
|
|
> & {
|
|
|
|
points?: ExcalidrawLinearElement["points"];
|
|
|
|
fontSize?: ExcalidrawTextElement["fontSize"];
|
|
|
|
baseline?: ExcalidrawTextElement["baseline"];
|
|
|
|
scale?: ExcalidrawImageElement["scale"];
|
2023-08-02 15:04:21 +05:00
|
|
|
boundTextFontSize?: ExcalidrawTextElement["fontSize"];
|
2023-05-25 19:27:41 +05:00
|
|
|
};
|
|
|
|
}[] = [];
|
|
|
|
|
|
|
|
for (const { orig, latest } of targetElements) {
|
|
|
|
// bounded text elements are updated along with their container elements
|
|
|
|
if (isTextElement(orig) && isBoundToContainer(orig)) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-08-13 22:53:10 +05:00
|
|
|
|
2023-05-25 19:27:41 +05:00
|
|
|
const width = orig.width * scale;
|
|
|
|
const height = orig.height * scale;
|
|
|
|
const angle = normalizeAngle(orig.angle * flipFactorX * flipFactorY);
|
2022-02-21 16:46:39 +05:30
|
|
|
|
2023-05-25 19:27:41 +05:00
|
|
|
const isLinearOrFreeDraw = isLinearElement(orig) || isFreeDrawElement(orig);
|
|
|
|
const offsetX = orig.x - anchorX;
|
|
|
|
const offsetY = orig.y - anchorY;
|
|
|
|
const shiftX = isFlippedByX && !isLinearOrFreeDraw ? width : 0;
|
|
|
|
const shiftY = isFlippedByY && !isLinearOrFreeDraw ? height : 0;
|
|
|
|
const x = anchorX + flipFactorX * (offsetX * scale + shiftX);
|
|
|
|
const y = anchorY + flipFactorY * (offsetY * scale + shiftY);
|
2021-05-09 16:42:10 +01:00
|
|
|
|
2022-08-16 21:51:43 +02:00
|
|
|
const rescaledPoints = rescalePointsInElement(
|
2023-05-25 19:27:41 +05:00
|
|
|
orig,
|
|
|
|
width * flipFactorX,
|
|
|
|
height * flipFactorY,
|
2022-08-16 21:51:43 +02:00
|
|
|
false,
|
|
|
|
);
|
2022-08-13 22:53:10 +05:00
|
|
|
|
2023-05-25 19:27:41 +05:00
|
|
|
const update: typeof elementsAndUpdates[0]["update"] = {
|
2022-08-13 22:53:10 +05:00
|
|
|
x,
|
|
|
|
y,
|
2023-05-25 19:27:41 +05:00
|
|
|
width,
|
|
|
|
height,
|
|
|
|
angle,
|
2022-08-13 22:53:10 +05:00
|
|
|
...rescaledPoints,
|
|
|
|
};
|
2021-05-09 16:42:10 +01:00
|
|
|
|
2023-05-25 19:27:41 +05:00
|
|
|
if (isImageElement(orig) && targetElements.length === 1) {
|
|
|
|
update.scale = [orig.scale[0] * flipFactorX, orig.scale[1] * flipFactorY];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isLinearElement(orig) && (isFlippedByX || isFlippedByY)) {
|
|
|
|
const origBounds = getElementPointsCoords(orig, orig.points);
|
|
|
|
const newBounds = getElementPointsCoords(
|
|
|
|
{ ...orig, x, y },
|
|
|
|
rescaledPoints.points!,
|
|
|
|
);
|
|
|
|
const origXY = [orig.x, orig.y];
|
|
|
|
const newXY = [x, y];
|
|
|
|
|
|
|
|
const linearShift = (axis: "x" | "y") => {
|
|
|
|
const i = axis === "x" ? 0 : 1;
|
|
|
|
return (
|
|
|
|
(newBounds[i + 2] -
|
|
|
|
newXY[i] -
|
|
|
|
(origXY[i] - origBounds[i]) * scale +
|
|
|
|
(origBounds[i + 2] - origXY[i]) * scale -
|
|
|
|
(newXY[i] - newBounds[i])) /
|
|
|
|
2
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (isFlippedByX) {
|
|
|
|
update.x -= linearShift("x");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isFlippedByY) {
|
|
|
|
update.y -= linearShift("y");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 15:04:21 +05:00
|
|
|
if (isTextElement(orig)) {
|
|
|
|
const metrics = measureFontSizeFromWidth(orig, width, height);
|
2023-04-10 18:52:46 +05:30
|
|
|
if (!metrics) {
|
2022-10-29 16:01:38 +05:00
|
|
|
return;
|
|
|
|
}
|
2023-08-02 15:04:21 +05:00
|
|
|
update.fontSize = metrics.size;
|
|
|
|
update.baseline = metrics.baseline;
|
|
|
|
}
|
2022-10-29 16:01:38 +05:00
|
|
|
|
2023-08-02 15:04:21 +05:00
|
|
|
const boundTextElement = pointerDownState.originalElements.get(
|
|
|
|
getBoundTextElementId(orig) ?? "",
|
|
|
|
) as ExcalidrawTextElementWithContainer | undefined;
|
2022-10-29 16:01:38 +05:00
|
|
|
|
2023-08-02 15:04:21 +05:00
|
|
|
if (boundTextElement) {
|
|
|
|
const newFontSize = boundTextElement.fontSize * scale;
|
|
|
|
if (newFontSize < MIN_FONT_SIZE) {
|
|
|
|
return;
|
2022-08-13 22:53:10 +05:00
|
|
|
}
|
2023-08-02 15:04:21 +05:00
|
|
|
update.boundTextFontSize = newFontSize;
|
2020-04-07 17:49:59 +09:00
|
|
|
}
|
2022-08-13 22:53:10 +05:00
|
|
|
|
2023-08-02 15:04:21 +05:00
|
|
|
elementsAndUpdates.push({
|
|
|
|
element: latest,
|
|
|
|
update,
|
|
|
|
});
|
2023-05-25 19:27:41 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
const elementsToUpdate = elementsAndUpdates.map(({ element }) => element);
|
2022-10-29 16:01:38 +05:00
|
|
|
|
2023-08-02 15:04:21 +05:00
|
|
|
for (const {
|
|
|
|
element,
|
|
|
|
update: { boundTextFontSize, ...update },
|
|
|
|
} of elementsAndUpdates) {
|
2023-05-25 19:27:41 +05:00
|
|
|
const { width, height, angle } = update;
|
2022-08-13 22:53:10 +05:00
|
|
|
|
2023-05-25 19:27:41 +05:00
|
|
|
mutateElement(element, update, false);
|
2022-12-05 21:03:13 +05:30
|
|
|
|
2023-05-25 19:27:41 +05:00
|
|
|
updateBoundElements(element, {
|
|
|
|
simultaneouslyUpdated: elementsToUpdate,
|
|
|
|
newSize: { width, height },
|
|
|
|
});
|
|
|
|
|
2023-08-02 15:04:21 +05:00
|
|
|
const boundTextElement = getBoundTextElement(element);
|
|
|
|
if (boundTextElement && boundTextFontSize) {
|
2023-05-25 19:27:41 +05:00
|
|
|
mutateElement(
|
|
|
|
boundTextElement,
|
|
|
|
{
|
2023-08-02 15:04:21 +05:00
|
|
|
fontSize: boundTextFontSize,
|
2023-05-25 19:27:41 +05:00
|
|
|
angle: isLinearElement(element) ? undefined : angle,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
);
|
2023-08-02 15:04:21 +05:00
|
|
|
handleBindTextResize(element, transformHandleType, true);
|
2022-08-13 22:53:10 +05:00
|
|
|
}
|
2023-05-25 19:27:41 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
Scene.getScene(elementsAndUpdates[0].element)?.informMutation();
|
2020-04-09 16:14:32 +01:00
|
|
|
};
|
2020-04-07 17:49:59 +09:00
|
|
|
|
2020-07-26 19:21:38 +09:00
|
|
|
const rotateMultipleElements = (
|
2020-09-11 17:22:40 +02:00
|
|
|
pointerDownState: PointerDownState,
|
2020-07-26 19:21:38 +09:00
|
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
|
|
|
pointerX: number,
|
|
|
|
pointerY: number,
|
2021-10-21 22:05:48 +02:00
|
|
|
shouldRotateWithDiscreteAngle: boolean,
|
2020-07-26 19:21:38 +09:00
|
|
|
centerX: number,
|
|
|
|
centerY: number,
|
|
|
|
) => {
|
|
|
|
let centerAngle =
|
|
|
|
(5 * Math.PI) / 2 + Math.atan2(pointerY - centerY, pointerX - centerX);
|
2021-10-21 22:05:48 +02:00
|
|
|
if (shouldRotateWithDiscreteAngle) {
|
2020-07-26 19:21:38 +09:00
|
|
|
centerAngle += SHIFT_LOCKING_ANGLE / 2;
|
|
|
|
centerAngle -= centerAngle % SHIFT_LOCKING_ANGLE;
|
|
|
|
}
|
2023-06-09 16:22:40 +05:00
|
|
|
|
2023-06-15 00:42:01 +08:00
|
|
|
elements
|
|
|
|
.filter((element) => element.type !== "frame")
|
|
|
|
.forEach((element) => {
|
|
|
|
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element);
|
|
|
|
const cx = (x1 + x2) / 2;
|
|
|
|
const cy = (y1 + y2) / 2;
|
|
|
|
const origAngle =
|
|
|
|
pointerDownState.originalElements.get(element.id)?.angle ??
|
|
|
|
element.angle;
|
|
|
|
const [rotatedCX, rotatedCY] = rotate(
|
|
|
|
cx,
|
|
|
|
cy,
|
|
|
|
centerX,
|
|
|
|
centerY,
|
|
|
|
centerAngle + origAngle - element.angle,
|
|
|
|
);
|
2023-06-09 16:22:40 +05:00
|
|
|
mutateElement(
|
2023-06-15 00:42:01 +08:00
|
|
|
element,
|
2023-06-09 16:22:40 +05:00
|
|
|
{
|
2023-06-15 00:42:01 +08:00
|
|
|
x: element.x + (rotatedCX - cx),
|
|
|
|
y: element.y + (rotatedCY - cy),
|
2022-12-05 21:03:13 +05:30
|
|
|
angle: normalizeAngle(centerAngle + origAngle),
|
2023-06-09 16:22:40 +05:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
);
|
2023-06-15 00:42:01 +08:00
|
|
|
updateBoundElements(element, { simultaneouslyUpdated: elements });
|
|
|
|
|
|
|
|
const boundText = getBoundTextElement(element);
|
|
|
|
if (boundText && !isArrowElement(element)) {
|
|
|
|
mutateElement(
|
|
|
|
boundText,
|
|
|
|
{
|
|
|
|
x: boundText.x + (rotatedCX - cx),
|
|
|
|
y: boundText.y + (rotatedCY - cy),
|
|
|
|
angle: normalizeAngle(centerAngle + origAngle),
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2023-06-09 16:22:40 +05:00
|
|
|
|
|
|
|
Scene.getScene(elements[0])?.informMutation();
|
2020-07-26 19:21:38 +09:00
|
|
|
};
|
|
|
|
|
2020-05-09 17:57:00 +09:00
|
|
|
export const getResizeOffsetXY = (
|
2020-08-10 14:16:39 +02:00
|
|
|
transformHandleType: MaybeTransformHandleType,
|
2020-05-09 17:57:00 +09:00
|
|
|
selectedElements: NonDeletedExcalidrawElement[],
|
|
|
|
x: number,
|
|
|
|
y: number,
|
|
|
|
): [number, number] => {
|
|
|
|
const [x1, y1, x2, y2] =
|
|
|
|
selectedElements.length === 1
|
|
|
|
? getElementAbsoluteCoords(selectedElements[0])
|
|
|
|
: getCommonBounds(selectedElements);
|
|
|
|
const cx = (x1 + x2) / 2;
|
|
|
|
const cy = (y1 + y2) / 2;
|
|
|
|
const angle = selectedElements.length === 1 ? selectedElements[0].angle : 0;
|
|
|
|
[x, y] = rotate(x, y, cx, cy, -angle);
|
2020-08-10 14:16:39 +02:00
|
|
|
switch (transformHandleType) {
|
2020-05-09 17:57:00 +09:00
|
|
|
case "n":
|
|
|
|
return rotate(x - (x1 + x2) / 2, y - y1, 0, 0, angle);
|
|
|
|
case "s":
|
|
|
|
return rotate(x - (x1 + x2) / 2, y - y2, 0, 0, angle);
|
|
|
|
case "w":
|
|
|
|
return rotate(x - x1, y - (y1 + y2) / 2, 0, 0, angle);
|
|
|
|
case "e":
|
|
|
|
return rotate(x - x2, y - (y1 + y2) / 2, 0, 0, angle);
|
|
|
|
case "nw":
|
|
|
|
return rotate(x - x1, y - y1, 0, 0, angle);
|
|
|
|
case "ne":
|
|
|
|
return rotate(x - x2, y - y1, 0, 0, angle);
|
|
|
|
case "sw":
|
|
|
|
return rotate(x - x1, y - y2, 0, 0, angle);
|
|
|
|
case "se":
|
|
|
|
return rotate(x - x2, y - y2, 0, 0, angle);
|
|
|
|
default:
|
|
|
|
return [0, 0];
|
|
|
|
}
|
|
|
|
};
|
2020-05-11 00:41:36 +09:00
|
|
|
|
|
|
|
export const getResizeArrowDirection = (
|
2020-08-10 14:16:39 +02:00
|
|
|
transformHandleType: MaybeTransformHandleType,
|
2020-05-11 00:41:36 +09:00
|
|
|
element: NonDeleted<ExcalidrawLinearElement>,
|
|
|
|
): "origin" | "end" => {
|
|
|
|
const [, [px, py]] = element.points;
|
|
|
|
const isResizeEnd =
|
2020-08-10 14:16:39 +02:00
|
|
|
(transformHandleType === "nw" && (px < 0 || py < 0)) ||
|
|
|
|
(transformHandleType === "ne" && px >= 0) ||
|
|
|
|
(transformHandleType === "sw" && px <= 0) ||
|
|
|
|
(transformHandleType === "se" && (px > 0 || py > 0));
|
2020-05-11 00:41:36 +09:00
|
|
|
return isResizeEnd ? "end" : "origin";
|
|
|
|
};
|