如何在两点之间创建弯曲的SVG路径? [英] How to create a curved SVG path between two points?
问题描述
我需要在两个圆的中心之间绘制一条对称弯曲的线.
I need to draw a symmetrically curved line between the centers of two circles.
<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>
这是我到目前为止编写的代码. < line>元素应替换为弯曲路径.
This is the code I wrote so far. < line > element should be replaced with a curved path.
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);
}
推荐答案
SVG二次曲线可能就足够了.要绘制它,需要端点(具有端点)和控制点来确定曲线.
An SVG quadratic curve will probably suffice. To draw it, you need the end points (which you have) and a control point which will determine the curve.
要绘制对称曲线,控制点必须位于端点之间直线的垂直平分线上.进行一些数学运算就可以找到它.
To make a symmetrical curve, the control point needs to be on the perpendicular bisector of the line between the end points. A little maths will find it.
所以,从两点来看...
So, from two points...
您可以进入
其中包含代码
<!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
的符号.
If you want the curve to go the other way, change the sign of offset
.
如果您使用的是ES6兼容的浏览器,则可以使用字符串内插以获取更简洁的代码:
If you are using ES6-compliant browsers, you can use string interpolation for slightly tidier code:
var curve = `M${p1x} ${p1y} Q${c1x} ${c1y} ${p2x} ${p2y}`;
不需要显示控制点-只是为了您可以看到控制点在哪里,并说明曲线没有通过控制点.
There is no requirement for the control point to be shown - that's just so you can see where it is and illustrate that the curve doesn't go through it.
注意:使用atan2的一种替代方法是计算点之间直线的梯度的负倒数,但是对于梯度为零的情况则很奇怪,当梯度为零时可能会产生非常不准确的结果接近零.
这篇关于如何在两点之间创建弯曲的SVG路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!