Search code examples
javascriptcsssvgsvg-animate

SVG: Add animate element dynamically


I try to add an animate element dynamically to my path element, which works correct. My html:

<g id="3" class="cluster loop-node">
<path fill="#edf1f2" stroke="#028d35" d="M92,-16C92,-16 408,-16 408,-16 414,-16 420,-22 420,-28 420,-28 420,-88 420,-88 420,-94 414,-100 408,-100 408,-100 92,-100 92,-100 86,-100 80,-94 80,-88 80,-88 80,-28 80,-28 80,-22 86,-16 92,-16"></path>
<text text-anchor="start" x="213.5547" y="-79.4" font-family="Times,serif" font-size="14.00" fill="#000000">1:11</text>
<text text-anchor="start" x="243.8131" y="-79.4" font-family="Times,serif" font-size="14.00" fill="#000000"> (Loop)</text>
</g>

I add my animate element ('<animate attributeType="XML" attributeName="stroke-width" values="6;1;6;1" dur="2s" repeatCount="3"></animate>') by javascript:

$("#3").find("path").append('<animate attributeType="XML" attributeName="stroke-width" values="6;1;6;1" dur="2s" repeatCount="indefinite"></animate>')

After that the animate tag is added correctly:

<g id="3" class="cluster loop-node">
<path fill="#edf1f2" stroke="#028d35" d="M92,-16C92,-16 408,-16 408,-16 414,-16 420,-22 420,-28 420,-28 420,-88 420,-88 420,-94 414,-100 408,-100 408,-100 92,-100 92,-100 86,-100 80,-94 80,-88 80,-88 80,-28 80,-28 80,-22 86,-16 92,-16"><animate attributetype="XML" attributename="stroke-width" values="6;1;6;1" dur="2s" repeatcount="indefinite"></animate></path>
<text text-anchor="start" x="213.5547" y="-79.4" font-family="Times,serif" font-size="14.00" fill="#000000">1:11</text>
<text text-anchor="start" x="243.8131" y="-79.4" font-family="Times,serif" font-size="14.00" fill="#000000"> (Loop)</text>
</g>

But the problem is that the animation is not shown when I insert it that way (programmatically). If I insert the same html from the beginning, then it works, but not when I want to insert them programmatically. I thought maybe I should reload the div, but the animation didn't run. What is the problem with my code?


Solution

  • you have to start your animation with .beginElement() which is not a jquery method therefore you can't use it with a jquery object. Use the Dom object instead

    var myanim=document.createElementNS("http://www.w3.org/2000/svg", 'animate');
    myanim.setAttribute("id","myAnimation"); 
    myanim.setAttribute("attributeType","XML"); 
    myanim.setAttribute("attributeName","stroke-width"); 
    myanim.setAttribute("values","6;1;6;1"); 
    myanim.setAttribute("dur","2s"); 
    myanim.setAttribute("repeatCount","3"); 
    var myPath = document.getElementById("path1");
    
    myPath.appendChild(myanim);
    
    function myFunction() {
    console.log('hello')
    //$("#myAnimation").beginElement();
    document.getElementById("myAnimation").beginElement();
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <button onclick="myFunction()">Click me</button>
    
    <svg width="320" height="320" viewBox="0 0 320 320">
    <path
                id="path1" fill="#edf1f2" stroke="#ff8d35"
                d="M160,100 Q200,100,200,300 Q100,300,100,200 Z">
    
    
                    </path>
    
     </svg>