gpt4 book ai didi

javascript - 如何使用 JavaScript 创建 SVG 动画标签

转载 作者:行者123 更新时间:2023-11-28 03:42:10 25 4
gpt4 key购买 nike

我有这个带有动​​画的 SVG 圆圈。

 <circle cx="30" cy="50" r="15" fill="blue" stroke="black" stroke-width="1">
<animateMotion path="M 0 0 H 300 Z" dur="8s" repeatCount="indefinite" />
</circle>

它工作正常。我想使用 javascript 制作完全相同的东西,所以如果我单击一个按钮,它会创建具有相同动画的相同圆圈。

这是我的尝试:

    var animateMotion = document.createElementNS("http://www.w3.org/2000/svg", "animateMotion");
animateMotion.setAttribute("dur","8s");
animateMotion.setAttribute("d", "M 0 0 H 300 Z");
animateMotion.setAttribute("repeatCount","indefinite");
//assuming that I already created my circle
myCircle.appendChild(animateMotion);

最佳答案

    var animateMotion = document.createElementNS('http://www.w3.org/2000/svg','animateMotion');
animateMotion.setAttribute("dur","3s");
animateMotion.setAttribute("repeatCount","1");
var mpath = document.createElementNS('http://www.w3.org/2000/svg','mpath');
mpath.setAttributeNS("http://www.w3.org/1999/xlink", "href", "#YourPath");
animateMotion.appendChild(mpath);

关于javascript - 如何使用 JavaScript 创建 SVG 动画标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9991919/

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