[英]How to create SVG animation tag using javascript
I have this SVG circle with an animation. 我有一个带有动画的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>
It works fine. 工作正常。 I want to make the exact same thing using javascript so if I click a button, it would create the same circle with the same animation.
我想使用JavaScript进行完全相同的操作,因此,如果单击按钮,它将创建具有相同动画的相同圆圈。
here is my try: 这是我的尝试:
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);
Looks like animateMotion
elements are bugged in Webkit, when dinamically created (see SVG elements will not Animate when added dynamically ), and unsupported in IE. 动态创建时,
animateMotion
元素似乎在Webkit中出现错误(请参见SVG元素在动态添加时不会动画化 ),并且在IE中不受支持。 I think you're better off using pure javascript animation using a library like d3 . 我认为您最好使用像d3这样的库使用纯JavaScript动画。
Maybe this is your problem: 也许这是您的问题:
In XML: 在XML中:
<animateMotion path= ... />
In JS: 在JS中:
animateMotion.setAttribute("d", ... ); //Should this be "path"?
Hope it helps, 希望能帮助到你,
regards m93a. 关于m93a。
If just the click is important, it can be solved by using the begin
attribute in your animateMotion
: 如果只是单击很重要,则可以通过使用
animateMotion
的begin
属性来解决:
<svg height="500" width="500" id="foo">
<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" begin="foo.click"/>
</circle>
</svg>
Should work with a button as well, if the button has an identifier, eg id="startbutton"
, then it should be begin="startbutton.click
同样应该与按钮一起使用,如果按钮具有标识符,例如
id="startbutton"
,则它应该为begin="startbutton.click
Another alternative with inline javascript can be found here: http://svg-whiz.com/svg/PausePlay.svg 可以在此处找到内联JavaScript的另一种替代方法: http : //svg-whiz.com/svg/PausePlay.svg
cheers 干杯
if you want the animate as soon as <animateMotion>
element append page, set <animateMotion>
'begin' to 'indefinite' and begin the animate call its .beginElement(); 如果要在
<animateMotion>
元素追加页面后立即进行动画处理,请将<animateMotion>
'begin'设置为'indefinite'并开始进行动画处理,调用.beginElement();。
var animateMotion = document.createElementNS(SVG_NS, 'animateMotion');
animateMotion.setAttribute('begin', 'indefinite');
//append animateMotion to the page
animateMotion.beginElement();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.