【问题标题】:How to create a curved SVG path between two points?如何在两点之间创建弯曲的 SVG 路径?
【发布时间】:2018-03-14 09:38:20
【问题描述】:

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

<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>

这是我目前写的代码。 元素应替换为弯曲路径

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);
}

【问题讨论】:

    标签: javascript svg bezier curve


    【解决方案1】:

    SVG 二次曲线可能就足够了。要绘制它,您需要端点(您拥有的)和一个控制点来确定曲线。

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

    所以,从两点...

    你可以去

    代码在

    <!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 的符号。

    如果你使用的是 ES6 兼容的浏览器,你可以使用string interpolation 来获得更简洁的代码:

    var curve = `M${p1x} ${p1y} Q${c1x} ${c1y} ${p2x} ${p2y}`;
    

    没有要求显示控制点 - 这只是为了让您可以看到它的位置并说明曲线没有通过它。

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

    【讨论】:

      猜你喜欢
      • 2016-02-01
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多