如何在两点之间创建弯曲的SVG路径?

时间:2018-03-14 09:38:20

标签: javascript svg bezier curve

我需要在两个圆圈的中心之间画一条对称曲线

<svg>
    <circle class="spot" id="au" cx="1680" cy="700" r="0"></circle>
    <circle class="spot" id="sl" cx="1425" cy="525" r="0"></circle>

    <line id="line1" stroke-width="2" stroke="red"/>
</svg>

这是我到目前为止编写的代码。的&LT; line&gt;元素应该用弯曲的路径替换

function drawNow() {
    let point1X = document.getElementById("au").getAttribute("cx");
    let point1Y = document.getElementById("au").getAttribute("cy");
    let point2X = document.getElementById("sl").getAttribute("cx");
    let point2Y = document.getElementById("sl").getAttribute("cy");

    let line1 = document.getElementById("line1");
    line1.setAttribute("x1", point1X);
    line1.setAttribute("y1", point1Y);
    line1.setAttribute("x2", point2X);
    line1.setAttribute("y2", point2Y);
}

1 个答案:

答案 0 :(得分:10)

SVG二次曲线可能就足够了。要绘制它,你需要有终点(你有)和一个决定曲线的控制点。

为了形成对称曲线,控制点需要位于端点之间的直线的垂直平分线上。一点点数学就能找到它。

所以,从两点来看......

Two points to join

你可以到达

Points joined with curve

中的代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        svg { background-color: bisque; }
        .spot { fill: blue; }
        .spot2 { fill: red; }
    </style>
    <script>
        function x() {
            var p1x = parseFloat(document.getElementById("au").getAttribute("cx"));
            var p1y = parseFloat(document.getElementById("au").getAttribute("cy"));
            var p2x = parseFloat(document.getElementById("sl").getAttribute("cx"));
            var p2y = parseFloat(document.getElementById("sl").getAttribute("cy"));

            // mid-point of line:
            var mpx = (p2x + p1x) * 0.5;
            var mpy = (p2y + p1y) * 0.5;

            // angle of perpendicular to line:
            var theta = Math.atan2(p2y - p1y, p2x - p1x) - Math.PI / 2;

            // distance of control point from mid-point of line:
            var offset = 30;

            // location of control point:
            var c1x = mpx + offset * Math.cos(theta);
            var c1y = mpy + offset * Math.sin(theta);

            // show where the control point is:
            var c1 = document.getElementById("cp");
            c1.setAttribute("cx", c1x);
            c1.setAttribute("cy", c1y);

            // construct the command to draw a quadratic curve
            var curve = "M" + p1x + " " + p1y + " Q " + c1x + " " + c1y + " " + p2x + " " + p2y;
            var curveElement = document.getElementById("curve");
            curveElement.setAttribute("d", curve);
        }
    </script>
</head>
<body>
    <svg width="240" height="160">
        <circle id="au" class="spot" cx="200" cy="50" r="4"></circle>
        <circle id="sl" class="spot" cx="100" cy="100" r="4"></circle>
        <circle id="cp" class="spot2" cx="0" cy="0" r="4"></circle>
        <path id="curve" d="M0 0" stroke="green" stroke-width="4" stroke-linecap="round" fill="transparent"></path>
    </svg>
    <button type="button" onclick="x();">Click</button>
</body>
</html>

如果您希望曲线走另一条路,请更改offset

的符号

不需要显示控制点 - 这样就可以看到它在哪里,并说明曲线没有通过它。

注意:使用atan2的另一种方法是计算点之间线条的渐变的负倒数,但对于渐变为零的情况,这是非常繁琐的,并且当渐变时可能会产生非常不准确的结果接近零。