Skip to content

Commit

Permalink
Improve shape intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
clauderic committed Jul 16, 2024
1 parent 64c2930 commit a6366f9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/improve-shape-intersection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@dnd-kit/collision': patch
---

Improve accuracy of `shapeIntersection` when there are multiple intersecting shapes.
12 changes: 8 additions & 4 deletions packages/collision/src/algorithms/shapeIntersection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down

0 comments on commit a6366f9

Please sign in to comment.