Skip to content

Commit e5aaaa6

Browse files
committed
fix(core,edges): correct smoothstep edge pathing
1 parent 55d8a9a commit e5aaaa6

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

packages/core/src/components/Edges/utils/bezier.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ export function getBezierPath({
7676
y2: sourceY,
7777
c: curvature,
7878
})
79-
80-
const [centerX, centerY, offsetX, offsetY] = getBezierEdgeCenter({
79+
const [labelX, labelY, offsetX, offsetY] = getBezierEdgeCenter({
8180
sourceX,
8281
sourceY,
8382
targetX,
@@ -90,8 +89,8 @@ export function getBezierPath({
9089

9190
return [
9291
`M${sourceX},${sourceY} C${sourceControlX},${sourceControlY} ${targetControlX},${targetControlY} ${targetX},${targetY}`,
93-
centerX,
94-
centerY,
92+
labelX,
93+
labelY,
9594
offsetX,
9695
offsetY,
9796
]

packages/core/src/components/Edges/utils/smoothstep.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ function getPoints({
8787
if (sourceDir[dirAccessor] * targetDir[dirAccessor] === -1) {
8888
centerX = center.x || defaultCenterX
8989
centerY = center.y || defaultCenterY
90-
9190
// --->
9291
// |
9392
// >---
@@ -126,9 +125,9 @@ function getPoints({
126125
if (diff <= offset) {
127126
const gapOffset = Math.min(offset - 1, offset - diff)
128127
if (sourceDir[dirAccessor] === currDir) {
129-
sourceGapOffset[dirAccessor] = gapOffset
128+
sourceGapOffset[dirAccessor] = (sourceGapped[dirAccessor] > source[dirAccessor] ? -1 : 1) * gapOffset
130129
} else {
131-
targetGapOffset[dirAccessor] = gapOffset
130+
targetGapOffset[dirAccessor] = (targetGapped[dirAccessor] > target[dirAccessor] ? -1 : 1) * gapOffset
132131
}
133132
}
134133
}
@@ -148,8 +147,8 @@ function getPoints({
148147
}
149148
}
150149

151-
const sourceGapPoint = { x: sourceGapped.x - sourceGapOffset.x, y: sourceGapped.y - sourceGapOffset.y }
152-
const targetGapPoint = { x: targetGapped.x - targetGapOffset.x, y: targetGapped.y - targetGapOffset.y }
150+
const sourceGapPoint = { x: sourceGapped.x + sourceGapOffset.x, y: sourceGapped.y + sourceGapOffset.y }
151+
const targetGapPoint = { x: targetGapped.x + targetGapOffset.x, y: targetGapped.y + targetGapOffset.y }
153152
const maxXDistance = Math.max(Math.abs(sourceGapPoint.x - points[0].x), Math.abs(targetGapPoint.x - points[0].x))
154153
const maxYDistance = Math.max(Math.abs(sourceGapPoint.y - points[0].y), Math.abs(targetGapPoint.y - points[0].y))
155154

@@ -165,16 +164,16 @@ function getPoints({
165164

166165
const pathPoints = [
167166
source,
168-
{ x: sourceGapped.x - sourceGapOffset.x, y: sourceGapped.y - sourceGapOffset.y },
167+
{ x: sourceGapped.x + sourceGapOffset.x, y: sourceGapped.y + sourceGapOffset.y },
169168
...points,
170-
{ x: targetGapped.x - targetGapOffset.x, y: targetGapped.y - targetGapOffset.y },
169+
{ x: targetGapped.x + targetGapOffset.x, y: targetGapped.y + targetGapOffset.y },
171170
target,
172171
]
173172

174173
return [pathPoints, centerX, centerY, defaultOffsetX, defaultOffsetY]
175174
}
176175

177-
function getBend(a: XYPosition, b: XYPosition, c: XYPosition, size: number) {
176+
function getBend(a: XYPosition, b: XYPosition, c: XYPosition, size: number): string {
178177
const bendSize = Math.min(distance(a, b) / 2, distance(b, c) / 2, size)
179178
const { x, y } = b
180179

@@ -195,6 +194,17 @@ function getBend(a: XYPosition, b: XYPosition, c: XYPosition, size: number) {
195194
return `L ${x},${y + bendSize * yDir}Q ${x},${y} ${x + bendSize * xDir},${y}`
196195
}
197196

197+
/**
198+
* Get a smooth step path from source to target handle
199+
* @param params
200+
* @param params.sourceX - The x position of the source handle
201+
* @param params.sourceY - The y position of the source handle
202+
* @param params.sourcePosition - The position of the source handle (default: Position.Bottom)
203+
* @param params.targetX - The x position of the target handle
204+
* @param params.targetY - The y position of the target handle
205+
* @param params.targetPosition - The position of the target handle (default: Position.Top)
206+
* @returns A path string you can use in an SVG, the labelX and labelY position (center of path) and offsetX, offsetY between source handle and label
207+
*/
198208
export function getSmoothStepPath({
199209
sourceX,
200210
sourceY,

0 commit comments

Comments
 (0)