From a6366f9e42836b4c5732141bf314489ede9f60cb Mon Sep 17 00:00:00 2001 From: Clauderic Demers Date: Tue, 16 Jul 2024 16:25:29 -0400 Subject: [PATCH] Improve shape intersection --- .changeset/improve-shape-intersection.md | 5 +++++ .../collision/src/algorithms/shapeIntersection.ts | 12 ++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 .changeset/improve-shape-intersection.md diff --git a/.changeset/improve-shape-intersection.md b/.changeset/improve-shape-intersection.md new file mode 100644 index 00000000..39d17dc1 --- /dev/null +++ b/.changeset/improve-shape-intersection.md @@ -0,0 +1,5 @@ +--- +'@dnd-kit/collision': patch +--- + +Improve accuracy of `shapeIntersection` when there are multiple intersecting shapes. diff --git a/packages/collision/src/algorithms/shapeIntersection.ts b/packages/collision/src/algorithms/shapeIntersection.ts index ed0b375b..e8a0e0d6 100644 --- a/packages/collision/src/algorithms/shapeIntersection.ts +++ b/packages/collision/src/algorithms/shapeIntersection.ts @@ -10,12 +10,13 @@ export const shapeIntersection: CollisionDetector = ({ dragOperation, droppable, }) => { - if (!droppable.shape) { + const {shape} = dragOperation; + + if (!droppable.shape || !shape?.current) { return null; } - const {shape} = dragOperation; - const intersectionArea = shape?.current.intersectionArea(droppable.shape); + const intersectionArea = shape.current.intersectionArea(droppable.shape); // Check if the droppable is intersecting with the drag operation shape. if (intersectionArea) { @@ -26,8 +27,11 @@ export const shapeIntersection: CollisionDetector = ({ * collisions. */ const distance = Point.distance(droppable.shape.center, position.current); + const intersectionRatio = + intersectionArea / + (shape.current.area + droppable.shape.area - intersectionArea); - const value = 1 / distance; + const value = intersectionRatio / distance; return { id: droppable.id,