Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何设置JS生成的SVG对象的动画?_Javascript_Animation_Svg - Fatal编程技术网

Javascript 如何设置JS生成的SVG对象的动画?

Javascript 如何设置JS生成的SVG对象的动画?,javascript,animation,svg,Javascript,Animation,Svg,这个Javascript片段创建了一个圆形对象,但没有设置圆形的动画。在Chrome中,我可以使用开发人员工具编辑HTML(添加一些空白;无任何内容),这似乎说服Chrome制作动画 如何强制动画开始 function f(x, y) { var svg = document.getElementById('svg'); var circle = document.createElementNS(svg.namespaceURI, 'circle'); circle.setAttr

这个Javascript片段创建了一个圆形对象,但没有设置圆形的动画。在Chrome中,我可以使用开发人员工具编辑HTML(添加一些空白;无任何内容),这似乎说服Chrome制作动画

如何强制动画开始

function f(x, y) {
  var svg = document.getElementById('svg');

  var circle = document.createElementNS(svg.namespaceURI, 'circle');
  circle.setAttribute('cx', x);
  circle.setAttribute('cy', y);
  circle.setAttribute('r', '50');

  var animation = document.createElementNS(svg.namespaceURI, 'animatemotion');
  var path = 'm 0,0 l ' + (x + 500) + ',0'
  animation.setAttribute('path', path);
  animation.setAttribute('dur', '30s');
  animation.setAttribute('fill', 'freeze');
  circle.appendChild(animation);

  svg.appendChild(circle);
}

f(50, 50);

元素创建似乎区分大小写

工作小提琴

看这条线

 var animation = document.createElementNS(svg.namespaceURI, 'animateMotion');
将animatemotion替换为animatemotion

代码

最初,我认为这是一个bug,在检查了一些我尝试使用区分大小写的示例后,我意识到它是有效的

function f(x, y) {
  var svg = document.getElementById('svg');

  var circle = document.createElementNS(svg.namespaceURI, 'circle');
  circle.setAttribute('cx', x);
  circle.setAttribute('cy', y);
  circle.setAttribute('r', '50');

  var animation = document.createElementNS(svg.namespaceURI, 'animateMotion');
  var path = 'm 0,0 l ' + (x + 500) + ',0'
  animation.setAttribute('path', path);
  animation.setAttribute('dur', '30s');
  animation.setAttribute('fill', 'freeze');
  circle.appendChild(animation);

  svg.appendChild(circle);
}

f(50, 50);