From 0a529bd2ed9f32a6662609df73e8afd00d8f0b7e Mon Sep 17 00:00:00 2001 From: Ryan Di Date: Tue, 28 May 2024 19:57:34 +0800 Subject: [PATCH] change a rotated element's width and height --- packages/excalidraw/components/DragInput.tsx | 64 +++++++++++++------- packages/excalidraw/components/Stats.tsx | 8 --- 2 files changed, 41 insertions(+), 31 deletions(-) diff --git a/packages/excalidraw/components/DragInput.tsx b/packages/excalidraw/components/DragInput.tsx index ec3283384e..8efd70512a 100644 --- a/packages/excalidraw/components/DragInput.tsx +++ b/packages/excalidraw/components/DragInput.tsx @@ -6,10 +6,9 @@ import { mutateElement } from "../element/mutateElement"; import { resizeSingleElement } from "../element/resizeElements"; import type { ElementsMap, ExcalidrawElement } from "../element/types"; import { KEYS } from "../keys"; -import { degreeToRadian, radianToDegree, rotatePoint } from "../math"; -import Scene from "../scene/Scene"; +import { degreeToRadian, radianToDegree } from "../math"; import type { AppState, Point } from "../types"; -import { arrayToMap } from "../utils"; +import { deepCopyElement } from "../element/newElement"; const shouldKeepAspectRatio = (element: ExcalidrawElement) => { return element.type === "image"; @@ -35,10 +34,8 @@ const DragInput = ({ const inputRef = useRef(null); const labelRef = useRef(null); - const originalElementsMap = useMemo( - () => arrayToMap(Scene.getScene(element)?.getNonDeletedElements() ?? []), - [element], - ); + const originalElement = useRef(); + const accumulatedDimensionChange = useRef(0); const handleChange = useMemo( () => @@ -48,7 +45,7 @@ const DragInput = ({ source: "pointerMove" | "keyDown", pointerOffset?: number, ) => { - if (inputRef.current) { + if (inputRef.current && originalElement.current) { const keepAspectRatio = shouldKeepAspectRatio(element); if ( @@ -57,10 +54,11 @@ const DragInput = ({ pointerOffset ) { const handles = getTransformHandles( - element, + originalElement.current, zoom, elementsMap, "mouse", + {}, ); let referencePoint: Point | undefined; @@ -78,26 +76,28 @@ const DragInput = ({ } if (referencePoint !== undefined && handleDirection !== undefined) { - const pointerRotated = rotatePoint( - [ - referencePoint[0] + - (property === "width" ? pointerOffset : 0), - referencePoint[1] + - (property === "height" ? pointerOffset : 0), - ], - referencePoint, - element.angle, - ); + accumulatedDimensionChange.current += pointerOffset; + + const pointer: Point = [ + referencePoint[0] + + (property === "width" + ? accumulatedDimensionChange.current + : 0), + referencePoint[1] + + (property === "height" + ? accumulatedDimensionChange.current + : 0), + ]; resizeSingleElement( - originalElementsMap, + elementsMap, keepAspectRatio, element, elementsMap, handleDirection, false, - pointerRotated[0], - pointerRotated[1], + pointer[0], + pointer[1], ); } } else if ( @@ -130,10 +130,11 @@ const DragInput = ({ mutateElement(element, { [property]: newVal, }); + originalElement.current = deepCopyElement(element); } } }, - [element, property, zoom, elementsMap, originalElementsMap], + [element, property, zoom, elementsMap], ); const hangleChangeThrottled = useMemo(() => { @@ -157,6 +158,11 @@ const DragInput = ({ hangleChangeThrottled.cancel(); }); + useEffect(() => { + accumulatedDimensionChange.current = 0; + originalElement.current = undefined; + }, [element.id]); + return (