SVG animate with dynamically added elements
Asked Answered
M

4

12

Based on this code, I'm trying to animate a dynamically-generated SVG element:

var svgnode = document.createElementNS('http://www.w3.org/2000/svg','svg');
var circle = document.createElementNS('http://www.w3.org/2000/svg','circle');
circle.setAttribute("cx", "10");
circle.setAttribute("cy", "10");
circle.setAttribute("r", "10");
circle.setAttribute("fill", "blue");
var ani = document.createElementNS("http://www.w3.org/2000/svg","animateTransform");
ani.setAttribute("attributeName", "transform");
ani.setAttribute("attributeType", "xml");
ani.setAttribute("type", "translate" );
ani.setAttribute("from", "10");
ani.setAttribute("to", "100");
ani.setAttribute("begin", "0s");
ani.setAttribute("dur", "2s");
ani.setAttribute("fill", "freeze");
circle.appendChild(ani);
svgnode.appendChild(circle);
document.getElementById('mydiv').appendChild(svgnode);

the SVG shows up fine, but the animation doesn't work. If I write the equivalent code in plain HTML/SVG it works. I'm using Chrome.

Mystical answered 10/12, 2011 at 7:50 Comment(1)
It is working, under Chrome v15 jsfiddle.net/SF5nEOpulence
H
6

It would not work if added dynamically later on chrome (would work with FF).

see http://jsfiddle.net/tap0ri/SF5nE/2/

this is seems to be chrome bug.

Hydroplane answered 19/12, 2011 at 4:52 Comment(4)
yes, this is very annoying because it means you can't trigger animations in response to messages from a serverMystical
I tried hack with creating somekind of "svg pool" on document.ready and I would use any svg for dynamically adding elements inside it.Hydroplane
ah so if the SVG element is create on document load then later added animate elements do work, but if the SVG element is later added then it doesn't?Mystical
Yes, I have reported this issue to webkit community, but not sure when they would fix it, until then, we need this hack !!Hydroplane
K
3

Definitely [Still] a bug with Chrome. Running Chrome 19.0.1084.52

This code works: http://jsfiddle.net/t3d28/

This code still does not work: http://jsfiddle.net/tap0ri/SF5nE/2/

Knacker answered 19/7, 2012 at 18:27 Comment(0)
H
0

I have still the same Problem 5 year's ago :) (and with svg-Filter too)

I use follow hack:

1. I set all may attributes wit setAtteribute, for example ani.setAttribute("attributeName", "transform");

2. I read and set my svg-root innerhtml: $svgRoot.html($svgRoot.html());

Hope it helps someone, or someone can tell me a better way :)

Headachy answered 31/10, 2017 at 11:7 Comment(0)
B
-1

Doesn't works on both navigators with viewBox="0 0 0 0".

Works here:

<div id="mydiv">
    <svg id="svgNode" version="1.1" width="100%" height="100%" viewBox="0 0 100% 100%" preserveAspectRatio="xMidYMid meet" id="ext-element-180"></svg>
</div>

see: https://jsfiddle.net/oOroBax/xkb89y4h/

Bannock answered 13/2, 2015 at 18:14 Comment(1)
What does this have to do with the question?Jennifferjennilee

© 2022 - 2024 — McMap. All rights reserved.