gpt4 book ai didi

javascript - 如何用svg连接不同位置的点?

转载 作者:行者123 更新时间:2023-11-29 10:32:01 25 4
gpt4 key购买 nike

如您所见,我是 svg 的新手,我在连接点时遇到了问题。

我想做的是将这 2 个点用一条线连接起来,无论它们的位置如何。

正如您在下图中看到的,当我将 cx="50"更改为 cx="510"时,该线仍处于同一位置,但我希望该线“跟随”第二个圆圈,无论位置如何。

恢复:

在我的例子中,我有一条静态线,当我更改坐标(cx 或 cy)时,我希望它有一条与另一个点相连的动态线。

enter image description here

    <!DOCTYPE html>
<html>
<head>
<title></title>


</head>
<body>


<svg width="1000" height="1000">

<circle id="circle0" cx="50" cy="50" r="20" stroke="black" stroke-width="3" fill="grey" onClick="dotClick(0)"></circle>
<circle id="circle1" cx="50" cy="110" r="20" stroke="black" stroke-width="3" fill="grey" onClick="dotClick(1)"></circle>


<line id="line0" x1="50" y1="50" x2="50" y2="100" />

</svg>



<script>

var buttons = {};

function dotClick(width) {

(width === 0) ? buttons.one = !buttons.one : buttons.two = !buttons.two;


document.getElementById("line0").setAttribute("stroke", (buttons.one && buttons.two) ? "red" : "");
document.getElementById("circle0").setAttribute("fill", (buttons.one ? "red" : "grey"));

document.getElementById("circle1").setAttribute("fill", (buttons.two ? "red" : "grey"));


}
</script>

</body>
</html>

最佳答案

同时实现预期的变化线x2

<line id="line0" x1="50" y1="50" x2="510" y2="100" />

Codepen- https://codepen.io/nagasai/pen/NjabQJ

更新了动态行的codepen

https://codepen.io/nagasai/pen/OmxWLa?editors=1111

关于javascript - 如何用svg连接不同位置的点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43794703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com