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