javascript - svg animateMotion 结束事件监听器
问题描述
我在我的 html 中有 svg ,其中有一个路径和一个animateMotion
沿路径移动的圆圈(带有 )。如何检测何时animationMotion
结束以便我可以执行任务?还有我如何重置animationMotion
- 即把移动的圆圈带回线的起点。下面是我的 svg html 代码:
<svg id="cc-line" x="0px" y="0px" viewBox="0 -23 150 55" width="150px" height="55px" xml:space="preserve">
<path fill="#2471A3" d="M0 0 l150 0" stroke="#2471A3" stroke-width="5"/>
<circle id="cc-line-circle" cx="0" cy="0" r="8" fill="#2471A3">
<animateMotion id="cc-line-motion" path="M0 0 l150 0"
begin= "0s" dur="6.0s" repeatCount="indefinite" rotate="auto" fill="freeze">
</circle>
</svg>
解决方案
将移动的圆圈带回线的起点
您可以通过将关键帧添加到您的<animateMotion>
:
keyPoints="0;1;0" keyTimes="0;0.5;1" calcMode="linear"
中的三个值keyPoints
表示每个关键帧沿线的分数。“0”表示开始,“1”表示结束。
中的三个值keyTimes
表示每个关键帧所在的持续时间的分数。“0”是开始,“0.5”是一半,“1”是结束。
如何检测 animationMotion 何时结束以便我可以执行任务?
最好的方法是使用 Dacre 建议的“onrepeat”事件。该事件称为“repeatEvent”
var anim = document.getElementById("cc-line-motion");
var i=0;
anim.addEventListener("repeatEvent", function(evt) {
console.log("repeat "+(++i));
});
<svg id="cc-line" x="0px" y="0px" viewBox="0 -23 150 55" width="150px" height="55px" xml:space="preserve">
<path fill="#2471A3" d="M0 0 l150 0" stroke="#2471A3" stroke-width="5"/>
<circle id="cc-line-circle" cx="0" cy="0" r="8" fill="#2471A3">
<animateMotion id="cc-line-motion" path="M0 0 l150 0"
begin="0s" dur="6.0s" repeatCount="indefinite" rotate="auto"
keyPoints="0;1;0" keyTimes="0;0.5;1" calcMode="linear"/>
</circle>
</svg>
推荐阅读
- python - 使用 Github API 通过 Python 获取所有成员
- julia - 在 Julia 中绘制数组时应用颜色图
- python - LookupAccountSid,帐户名称和安全 ID 之间没有映射
- vb.net - 如何在 Visual Basic 中以“反向”(从代码获取表单)工作?
- r - R将时间转换为一天中的时间
- java - Acivity1 Fragment2 -> Activity2(单击按钮后)-> Activity1 Fragment2 从活动导航到特定片段
- hyperledger-fabric - 什么是链码的交易计数
- apache-spark - 从 Hive 表读取并写入 Cassandra 表
- java - 如何根据我输入的输入加减来获得我想要的数字?
- javascript - 如何将多个变量添加到“罐子中的 Blob”动画?