Skip to content

Commit cba335d

Browse files
committed
fix(step-edge): drop points on straight segments
getPoints() can return points that sit on a straight segment: the gapped source/target, and the center points when the step lines up with them. getBend() caps each radius at half the distance to the neighbouring point, so such a point shrinks the bend next to it. When the step lands a short distance past the handle offset, the radius drops to a fraction of a pixel. Drop a point when the path runs straight through it. Duplicates are the zero-length case of the same check, which replaces the equality filter from #5730. Points where the path turns back are kept.
1 parent 3d35b57 commit cba335d

2 files changed

Lines changed: 20 additions & 10 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@xyflow/system": patch
3+
---
4+
5+
Fix `getSmoothStepPath` bends that render sharper than `borderRadius` when nodes are close to each other

‎packages/system/src/utils/edges/smoothstep-edge.ts‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ const getDirection = ({
5858

5959
const distance = (a: XYPosition, b: XYPosition) => Math.sqrt(Math.pow(b.x - a.x, 2) + Math.pow(b.y - a.y, 2));
6060

61+
// checks if b lies on the straight segment from a to c, including b === c
62+
const isBetween = (a: XYPosition, b: XYPosition, c: XYPosition) =>
63+
(a.x === b.x && b.x === c.x && (b.y - a.y) * (c.y - b.y) >= 0) ||
64+
(a.y === b.y && b.y === c.y && (b.x - a.x) * (c.x - b.x) >= 0);
65+
6166
/*
6267
* With this function we try to mimic an orthogonal edge routing behaviour
6368
* It's not as good as a real orthogonal edge routing, but it's faster and good enough as a default for step and smooth step edges
@@ -197,16 +202,16 @@ function getPoints({
197202
const gappedSource = { x: sourceGapped.x + sourceGapOffset.x, y: sourceGapped.y + sourceGapOffset.y };
198203
const gappedTarget = { x: targetGapped.x + targetGapOffset.x, y: targetGapped.y + targetGapOffset.y };
199204

200-
const pathPoints = [
201-
source,
202-
// we only want to add the gapped source/target if they are different from the first/last point to avoid duplicates which can cause issues with the bends
203-
...(gappedSource.x !== points[0].x || gappedSource.y !== points[0].y ? [gappedSource] : []),
204-
...points,
205-
...(gappedTarget.x !== points[points.length - 1].x || gappedTarget.y !== points[points.length - 1].y
206-
? [gappedTarget]
207-
: []),
208-
target,
209-
];
205+
const pathPoints: XYPosition[] = [];
206+
207+
for (const point of [source, gappedSource, ...points, gappedTarget, target]) {
208+
const [a, b] = pathPoints.slice(-2);
209+
// drop b if the path runs straight through it, otherwise it would shrink the bend of the next corner
210+
if (b && isBetween(a, b, point)) {
211+
pathPoints.pop();
212+
}
213+
pathPoints.push(point);
214+
}
210215

211216
return [pathPoints, centerX, centerY, defaultOffsetX, defaultOffsetY];
212217
}

0 commit comments

Comments
 (0)