我有一个我正在制作动画的对象animateMotion。单击动画停止按钮后,stop对象应该返回到之前没有相同动画时会渲染的位置,在这种情况下x& y= 0,但是当动画停止时,对象会保留它的位置,这实际上不该是。也许我试图错误地关闭动画,我正在更改xlink:href接受沿路径移动的对象的属性 ( ),即下例中的正方形。也许有一些内置函数SVG,我非常怀疑,我想听听关于这个的易懂的答案。
最小的例子:
document.querySelector('button').onclick = () => {
document.querySelector('#AM').setAttribute('xlink:href', ' ');
};
<div class="wrapper">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="border: 0.0625rem solid">
<g class="main-area">
<path d="M0 75L300 75" fill="none" stroke="red" id="AMP"/>
<rect x="0" y="0" width="50" height="50" id="TrO" transform="translate(0 -25)"/>
<animateMotion id="AM" xlink:href="#TrO" begin="0s" dur="3s" repeatCount="indefinite">
<mpath xlink:href="#AMP"/>
</animateMotion>
</g>
</svg>
<button>Stop</button>
</div>
当然,这不是最好的选择,但它确实发挥了作用。
还有一种特殊的方法
endElement()可以停止动画。纯 SVG
您可以尝试另一种动画,其中“x”属性,即矩形左上角的坐标,将进行动画处理。
Play可以根据任务移除按钮。带动画的选项
animateMotion更新 9.12.2018
以 CSS 规则样式格式化 SVG 代码,以便于感知