clean code
This commit is contained in:
@@ -21,60 +21,42 @@ interface GetControlWithCurvatureParams {
|
|||||||
y1: number;
|
y1: number;
|
||||||
x2: number;
|
x2: number;
|
||||||
y2: number;
|
y2: number;
|
||||||
cX: number;
|
|
||||||
cY: number;
|
|
||||||
c: number;
|
c: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
function calculateControlOffset(distance: number, curvature: number): number {
|
function calculateControlOffset(distance: number, curvature: number): number {
|
||||||
return curvature * 25 * Math.sqrt(distance);
|
if (distance >= 0) {
|
||||||
|
return 0.5 * distance;
|
||||||
|
} else {
|
||||||
|
return curvature * 25 * Math.sqrt(-distance);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getControlWithCurvature({ pos, x1, y1, x2, y2, cX, cY, c }: GetControlWithCurvatureParams): [number, number] {
|
function getControlWithCurvature({ pos, x1, y1, x2, y2, c }: GetControlWithCurvatureParams): [number, number] {
|
||||||
let ctX: number, ctY: number;
|
let ctX: number, ctY: number;
|
||||||
switch (pos) {
|
switch (pos) {
|
||||||
case Position.Left:
|
case Position.Left:
|
||||||
{
|
{
|
||||||
const d = x2 - x1;
|
ctX = x1 - calculateControlOffset(x1 - x2, c);
|
||||||
ctY = y1;
|
ctY = y1;
|
||||||
if (d <= 0) {
|
|
||||||
ctX = cX;
|
|
||||||
} else {
|
|
||||||
ctX = x1 - calculateControlOffset(d, c);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Position.Right:
|
case Position.Right:
|
||||||
{
|
{
|
||||||
const d = x1 - x2;
|
ctX = x1 + calculateControlOffset(x2 - x1, c);
|
||||||
ctY = y1;
|
ctY = y1;
|
||||||
if (d <= 0) {
|
|
||||||
ctX = cX;
|
|
||||||
} else {
|
|
||||||
ctX = x1 + calculateControlOffset(d, c);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Position.Top:
|
case Position.Top:
|
||||||
{
|
{
|
||||||
const d = y2 - y1;
|
|
||||||
ctX = x1;
|
ctX = x1;
|
||||||
if (d <= 0) {
|
ctY = y1 - calculateControlOffset(y1 - y2, c);
|
||||||
ctY = cY;
|
|
||||||
} else {
|
|
||||||
ctY = y1 - calculateControlOffset(d, c);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Position.Bottom:
|
case Position.Bottom:
|
||||||
{
|
{
|
||||||
const d = y1 - y2;
|
|
||||||
ctX = x1;
|
ctX = x1;
|
||||||
if (d <= 0) {
|
ctY = y1 + calculateControlOffset(y2 - y1, c);
|
||||||
ctY = cY;
|
|
||||||
} else {
|
|
||||||
ctY = y1 + calculateControlOffset(d, c);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -89,20 +71,13 @@ export function getBezierPath({
|
|||||||
targetY,
|
targetY,
|
||||||
targetPosition = Position.Top,
|
targetPosition = Position.Top,
|
||||||
curvature = 0.25,
|
curvature = 0.25,
|
||||||
centerX,
|
|
||||||
centerY,
|
|
||||||
}: GetBezierPathParams): string {
|
}: GetBezierPathParams): string {
|
||||||
const [_centerX, _centerY] = getCenter({ sourceX, sourceY, targetX, targetY });
|
|
||||||
centerX = centerX ?? _centerX;
|
|
||||||
centerY = centerY ?? _centerY;
|
|
||||||
const [sourceControlX, sourceControlY] = getControlWithCurvature({
|
const [sourceControlX, sourceControlY] = getControlWithCurvature({
|
||||||
pos: sourcePosition,
|
pos: sourcePosition,
|
||||||
x1: sourceX,
|
x1: sourceX,
|
||||||
y1: sourceY,
|
y1: sourceY,
|
||||||
x2: targetX,
|
x2: targetX,
|
||||||
y2: targetY,
|
y2: targetY,
|
||||||
cX: centerX,
|
|
||||||
cY: centerY,
|
|
||||||
c: curvature,
|
c: curvature,
|
||||||
});
|
});
|
||||||
const [targetControlX, targetControlY] = getControlWithCurvature({
|
const [targetControlX, targetControlY] = getControlWithCurvature({
|
||||||
@@ -111,8 +86,6 @@ export function getBezierPath({
|
|||||||
y1: targetY,
|
y1: targetY,
|
||||||
x2: sourceX,
|
x2: sourceX,
|
||||||
y2: sourceY,
|
y2: sourceY,
|
||||||
cX: centerX,
|
|
||||||
cY: centerY,
|
|
||||||
c: curvature,
|
c: curvature,
|
||||||
});
|
});
|
||||||
return `M${sourceX},${sourceY} C${sourceControlX},${sourceControlY} ${targetControlX},${targetControlY} ${targetX},${targetY}`;
|
return `M${sourceX},${sourceY} C${sourceControlX},${sourceControlY} ${targetControlX},${targetControlY} ${targetX},${targetY}`;
|
||||||
|
|||||||
Reference in New Issue
Block a user