From 3809fe730ce0586a72ef28e8a6ef205e3eb0f45f Mon Sep 17 00:00:00 2001 From: Mark Tolmacs Date: Tue, 29 Sep 2026 13:58:27 +0000 Subject: [PATCH] fix: Prefer binding distance around rounded rectangle corners when grid binding Signed-off-by: Mark Tolmacs --- packages/element/src/binding.ts | 34 ++++++------- packages/element/tests/binding.test.tsx | 64 ++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 19 deletions(-) diff --git a/packages/element/src/binding.ts b/packages/element/src/binding.ts index 88363061d3..4b19dd101b 100644 --- a/packages/element/src/binding.ts +++ b/packages/element/src/binding.ts @@ -853,6 +853,21 @@ const getBindingStrategyForDraggingBindingElementEndpoints_simple = ( } // Handle normal cases + const orbitPoint = + hit && !pointInElement && !opts?.angleLocked && appState.gridModeEnabled + ? snapBoundPointToGrid( + pointFrom(scenePointerX, scenePointerY), + hit, + elementsMap, + appState.gridSize as NullableGridSize, + arrow, + LinearElementEditor.getPointAtIndexGlobalCoordinates( + arrow, + startDragged ? 1 : -2, + elementsMap, + ), + ) + : globalPoint; const current: BindingStrategy = hit ? pointInElement ? { @@ -866,22 +881,7 @@ const getBindingStrategyForDraggingBindingElementEndpoints_simple = ( focusPoint: projectFixedPointOntoDiagonal( arrow, - opts?.angleLocked - ? globalPoint - : appState.gridModeEnabled - ? snapBoundPointToGrid( - pointFrom(scenePointerX, scenePointerY), - hit, - elementsMap, - appState.gridSize as NullableGridSize, - arrow, - LinearElementEditor.getPointAtIndexGlobalCoordinates( - arrow, - startDragged ? 1 : -2, - elementsMap, - ), - ) - : globalPoint, + orbitPoint, hit, startDragged ? "start" : "end", elementsMap, @@ -889,7 +889,7 @@ const getBindingStrategyForDraggingBindingElementEndpoints_simple = ( appState.isMidpointSnappingEnabled && !opts?.angleLocked && !appState.gridModeEnabled, - ) || globalPoint, + ) || orbitPoint, } : { mode: null }; diff --git a/packages/element/tests/binding.test.tsx b/packages/element/tests/binding.test.tsx index 048d61519c..663495cece 100644 --- a/packages/element/tests/binding.test.tsx +++ b/packages/element/tests/binding.test.tsx @@ -8,9 +8,8 @@ import { import { pointFrom, pointRotateRads } from "@excalidraw/math"; -import { getDefaultAppState } from "@excalidraw/excalidraw/appState"; - import { actionWrapTextInContainer } from "@excalidraw/excalidraw/actions/actionBoundText"; +import { getDefaultAppState } from "@excalidraw/excalidraw/appState"; import { Excalidraw, isLinearElement } from "@excalidraw/excalidraw"; @@ -1777,3 +1776,64 @@ describe("updateBoundElements with stale records", () => { expect(arrow.endBinding?.elementId).toBe("b"); }); }); + +describe("grid mode binding near a rectangle corner", () => { + it("keeps the grid snap when the diagonal projection misses", () => { + const a = { + ...newElement({ type: "rectangle", x: 0, y: 0, width: 100, height: 100 }), + id: "a", + } as NonDeleted; + const b = { + ...newElement({ + type: "rectangle", + x: 300, + y: 0, + width: 100, + height: 100, + }), + id: "b", + } as NonDeleted; + // bound to a, dragged to just above b's top side near its left corner: + // the line from a's focus point crosses b's diagonal inside the part + // trimmed off near the corner, so the diagonal projection misses + const points = [ + pointFrom(0, 0), + pointFrom(237, -53), + ]; + const arrow = { + ...newArrowElement({ type: "arrow", x: 106, y: 50, points }), + ...getSizeFromPoints(points), + id: "x", + startBinding: { + elementId: "a", + mode: "orbit", + fixedPoint: [1.06, 0.5001], + }, + } as NonDeleted; + const elements = [a, b, arrow]; + + const { end } = getBindingStrategyForDraggingBindingElementEndpoints( + arrow, + new Map([[1, { point: points[1] }]]), + 343, + -3, + arrayToMap(elements) as NonDeletedSceneElementsMap, + elements as Ordered>[], + { + ...getDefaultAppState(), + width: 1000, + height: 1000, + offsetTop: 0, + offsetLeft: 0, + gridModeEnabled: true, + gridSize: 20 as NullableGridSize, + } as AppState, + ); + + // snapped to the grid line x = 340, one binding gap above the top side, + // instead of falling back to the raw pointer at (343, -3) + expect(end).toMatchObject({ mode: "orbit", element: b }); + expect(end.focusPoint![0]).toBeCloseTo(340); + expect(end.focusPoint![1]).toBeCloseTo(-getBindingGap(b, arrow)); + }); +});