javascripthtmlanimationsvgsmil

How to pause and run just one SMIL animation in svg?


I have an SVG that has two SMIL animations. I want to control just the animations for one path (has id="poi_front" for the group) by pausing and running them using buttons, and let the animations for the other SVG path (id="poi_back") run continuously. I managed to pause and run all the four animations (two for "poi_front" and two for "poi_back") in the svg - the code is shown below:

I have tried document.getElementById("svg").getElementById("poi_front").unpauseAnimations(); but it doesn't work.

I read this w3cubDocs about SVGSVGElement.getElementById(), and it talks about the "SVG document fragment" but I am not sure what this means, and how to use it in my script.

document.getElementById("svg").pauseAnimations();

document.getElementById("button").onclick = function() {
    start();
};

function start() {
    document.getElementById("svg").unpauseAnimations();
}   

document.getElementById("button2").onclick = function() {
    stop();
};

function stop() {
    document.getElementById("svg").pauseAnimations();
}
html, body {
    height: 100%;
    margin: 0%;
    padding: 0%;
    overflow: hidden;
}
body {
    height: 100vh;
    display: flex;
    background-color: #2a0000;
    box-sizing: border-box;
    flex-direction: column;
    align-items: center;
}  
#button {
    width: 60px;
    height: 30px; 
    background-color: orange;
    position: relative;
    margin-top: 5px;
    margin-bottom: 5px;
}
#button2 {
    width: 60px;
    height: 30px; 
    background-color: yellow; 
    position: relative;
    margin-top: 5px;
    margin-bottom: 5px;
}
<button id="button">START</button>
<button id="button2">STOP</button>
<svg 
    width="100%" 
    height="100%" 
    version="1.1" 
    viewBox="0 0 300 300" 
    xmlns="http://www.w3.org/2000/svg"
    id="svg"
>
    <!--Begin Bottle -->
    <path
        d="m166.76 57.45c-13.062-0.0623-26.955 0.73204-46.311 0.0859-2.7685-0.0924-5 2.23-5 5v10.709c-2e-5 2.77 2.23 5 5 5h4.7226v6.209a88.388 88.388 0 0 0 -67.176 85.715 88.388 88.388 0 0 0 88.389 88.389 88.388 88.388 0 0 0 88.389 -88.389 88.388 88.388 0 0 0 -67.176 -85.695v-6.2285h4.7227c2.77 0 5-2.23 5-5v-10.709c0-2.77-2.231-4.9242-5-5-1.8457-0.0506-3.6945-0.077-5.5606-0.0859z"
        fill="#1b000c"
    />
    <path
        d="m231.11 170.17c0 46.793-37.933 84.727-84.727 84.727-46.793 0-84.727-37.933-84.727-84.727-1e-6 -40.912 28.997-75.051 67.559-82.986 12.071 2.5421 23.795 1.9463 35.395 0.22531 38.033 8.3382 66.499 42.225 66.499 82.761z"
        fill="#270600"
    />
    <path
        d="m122.57 61.1c16.221 0.59986 32.004 0.30589 47.553 0 1.8278-0.036 3.3 1.4718 3.3 3.3v6.2541c0 1.8282-1.4718 3.3-3.3 3.3h-47.553c-1.8282 0-3.3-1.4718-3.3-3.3v-6.2541c0-1.8282 1.473-3.3676 3.3-3.3z"
        fill="#270600"
    />
    <path
        d="m129.08 77.57v6.0117c9.8102 2.4161 28.289 2.4275 35 0v-6.0117z"
        fill="#270600"
    />
    <!--End Bottle -->
    <!--Begin Poison Back-->
    <path
        fill="#9bc300"
        id="poi_back"
    >
        <animate
            id="poi_back_1"
            attributeName="d"
            values="m 62.692,157.03 c -0.66726,4.2839 -1.0332,8.6677 -1.0332,13.139 2e-6,46.793 37.933,84.727 84.727,84.727 46.793,0 84.727,-37.933 84.727,-84.727 0,-4.4706 -0.36411,-8.8552 -1.0312,-13.139 -14.83662,12.68631 -46.3415,32.04916 -110.09827,6.15172 C 55.89527,130.25689 61.6596,170.1687 62.6926,157.03 Z; m 62.692,157.03 c 0.858416,-8.12931 -0.379575,2.34105 -1.0332,13.139 -2.827316,46.70751 37.933,84.727 84.727,84.727 46.793,0 84.727,-37.933 84.727,-84.727 0,-4.4706 -0.36411,-8.8552 -1.0312,-13.139 -8.28777,2.68817 -30.19905,-24.60832 -80.45959,1.45473 C 92.556926,187.6518 60.557883,163.01304 61.6588,170.169 Z"
            begin="0s;poi_back_2.end"
            dur="1.5s"
        />
        <animate
            id="poi_back_2"
            attributeName="d"
            values="m 62.692,157.03 c 0.858416,-8.12931 -0.379575,2.34105 -1.0332,13.139 -2.827316,46.70751 37.933,84.727 84.727,84.727 46.793,0 84.727,-37.933 84.727,-84.727 0,-4.4706 -0.36411,-8.8552 -1.0312,-13.139 -8.28777,2.68817 -30.19905,-24.60832 -80.45959,1.45473 C 92.556926,187.6518 60.557883,163.01304 61.6588,170.169 Z;m 62.692,157.03 c -0.66726,4.2839 -1.0332,8.6677 -1.0332,13.139 2e-6,46.793 37.933,84.727 84.727,84.727 46.793,0 84.727,-37.933 84.727,-84.727 0,-4.4706 -0.36411,-8.8552 -1.0312,-13.139 -14.83662,12.68631 -46.3415,32.04916 -110.09827,6.15172 C 55.89527,130.25689 61.6596,170.1687 62.6926,157.03 Z"
            begin="poi_back_1.end"
            dur="1.5s"
        />
    </path>
    <!--End Poison Back-->
    <!--Begin Poison Front-->
    <g id="poi_front">
        <path fill="#739a00">
            <animate
                id="poi_front_1"
                attributeName="d"
                values="m62.692 157.03c-0.66726 4.2839-1.0332 8.6677-1.0332 13.139 2e-6 46.793 37.933 84.727 84.727 84.727 46.793 0 84.727-37.933 84.727-84.727 0-4.4706-0.36411-8.8552-1.0312-13.139-7.1302-16.488-52.947-12.538-86.979 5.0508-31.611 16.066-81.443 8.0879-80.41-5.0508z; m 62.692,157.03 c -0.66726,4.2839 -1.0332,8.6677 -1.0332,13.139 2e-6,46.793 37.933,84.727 84.727,84.727 46.793,0 84.727,-37.933 84.727,-84.727 0,-4.4706 -0.36411,-8.8552 -1.0312,-13.139 -14.83662,12.68631 -46.3415,32.04916 -110.09827,6.15172 C 55.89527,130.25689 61.6596,170.1687 62.6926,157.03 Z"
                begin="0s;poi_front_2.end"
                dur="1.5s"
            />
            <animate
                id="poi_front_2"
                attributeName="d"
                values="m 62.692,157.03 c -0.66726,4.2839 -1.0332,8.6677 -1.0332,13.139 2e-6,46.793 37.933,84.727 84.727,84.727 46.793,0 84.727,-37.933 84.727,-84.727 0,-4.4706 -0.36411,-8.8552 -1.0312,-13.139 -14.83662,12.68631 -46.3415,32.04916 -110.09827,6.15172 C 55.89527,130.25689 61.6596,170.1687 62.6926,157.03 Z; m62.692 157.03c-0.66726 4.2839-1.0332 8.6677-1.0332 13.139 2e-6 46.793 37.933 84.727 84.727 84.727 46.793 0 84.727-37.933 84.727-84.727 0-4.4706-0.36411-8.8552-1.0312-13.139-7.1302-16.488-52.947-12.538-86.979 5.0508-31.611 16.066-81.443 8.0879-80.41-5.0508z"
                begin="poi_front_1.end"
                dur="1.5s"
            /> 
        </path>
    </g>
    <!--End Poison Front-->
</svg>


Solution

  • What you ask in your question and what you do with your code differs in multiple ways. But either of them won't work, for various reasons.

    1. If an <animate> element contains a values attribute, it must contain a corresponding keyTimes attribute, with the same number of semicolon-seperated values (going from 0 to 1 in ascending order). You may only leave out the keyTimes attribute if you use the from and to attributes instead.

    Only the first point in neccessary to get your animation to pause/unpause:

    document.getElementById("svg").pauseAnimations(); 
    document.getElementById("button").onclick = start;
    function start(){
        document.getElementById("svg").unpauseAnimations();
    }   
    
    document.getElementById("button2").onclick = stop;
    function stop(){
        document.getElementById("svg").pauseAnimations();
    }
    html, body{
       height: 100%;
       margin: 0%;
       padding: 0%;
       overflow: hidden;
    }
    body{
      height: 100vh;
      display: flex;
      background-color: #2a0000;
      box-sizing: border-box;
      flex-direction: column;
      align-items: center;
    }  
    #button{
     width: 60px;
     height: 30px; 
     background-color: orange;
     position: relative;
    margin-top: 5px;
    margin-bottom:  5px;
    }
    #button2{
     width: 60px;
     height: 30px; 
     background-color: yellow; 
     position: relative;
    margin-top: 5px;
    margin-bottom:  5px;
    }
    <button id="button">START</button>
    <button id="button2">STOP</button>
    <svg 
        width="100%" 
        height="100%" 
        version="1.1" 
        viewBox="0 0 300 300" 
        xmlns="http://www.w3.org/2000/svg"
        id="svg">
    <!--Begin Bottle -->
     <path 
        d="m166.76 57.45c-13.062-0.0623-26.955 0.73204-46.311 0.0859-2.7685-0.0924-5 2.23-5 5v10.709c-2e-5 2.77 2.23 5 5 5h4.7226v6.209a88.388 88.388 0 0 0 -67.176 85.715 88.388 88.388 0 0 0 88.389 88.389 88.388 88.388 0 0 0 88.389 -88.389 88.388 88.388 0 0 0 -67.176 -85.695v-6.2285h4.7227c2.77 0 5-2.23 5-5v-10.709c0-2.77-2.231-4.9242-5-5-1.8457-0.0506-3.6945-0.077-5.5606-0.0859z" 
        fill="#1b000c"/>
     <path 
        d="m231.11 170.17c0 46.793-37.933 84.727-84.727 84.727-46.793 0-84.727-37.933-84.727-84.727-1e-6 -40.912 28.997-75.051 67.559-82.986 12.071 2.5421 23.795 1.9463 35.395 0.22531 38.033 8.3382 66.499 42.225 66.499 82.761z" 
        fill="#270600"/>
     <path 
        d="m122.57 61.1c16.221 0.59986 32.004 0.30589 47.553 0 1.8278-0.036 3.3 1.4718 3.3 3.3v6.2541c0 1.8282-1.4718 3.3-3.3 3.3h-47.553c-1.8282 0-3.3-1.4718-3.3-3.3v-6.2541c0-1.8282 1.473-3.3676 3.3-3.3z" 
        fill="#270600"/>
     <path 
        d="m129.08 77.57v6.0117c9.8102 2.4161 28.289 2.4275 35 0v-6.0117z"
        fill="#270600"/>
     <!--End Bottle -->
      <!--Begin Poison Back-->
     <path 
        fill="#9bc300"
        id="poi_back">
        <animate 
         id="poi_back_1"
         attributeName="d"
             values="m 62.692,157.03 c -0.66726,4.2839 -1.0332,8.6677 -1.0332,13.139 2e-6,46.793 37.933,84.727 84.727,84.727 46.793,0 84.727,-37.933 84.727,-84.727 0,-4.4706 -0.36411,-8.8552 -1.0312,-13.139 -14.83662,12.68631 -46.3415,32.04916 -110.09827,6.15172 C 55.89527,130.25689 61.6596,170.1687 62.6926,157.03 Z; m 62.692,157.03 c 0.858416,-8.12931 -0.379575,2.34105 -1.0332,13.139 -2.827316,46.70751 37.933,84.727 84.727,84.727 46.793,0 84.727,-37.933 84.727,-84.727 0,-4.4706 -0.36411,-8.8552 -1.0312,-13.139 -8.28777,2.68817 -30.19905,-24.60832 -80.45959,1.45473 C 92.556926,187.6518 60.557883,163.01304 61.6588,170.169 Z"
             keyTimes="0;1"
          begin="0s;poi_back_2.end"
          dur="1.5s"
        />
            <animate 
         id="poi_back_2"
         attributeName="d"
             values="m 62.692,157.03 c 0.858416,-8.12931 -0.379575,2.34105 -1.0332,13.139 -2.827316,46.70751 37.933,84.727 84.727,84.727 46.793,0 84.727,-37.933 84.727,-84.727 0,-4.4706 -0.36411,-8.8552 -1.0312,-13.139 -8.28777,2.68817 -30.19905,-24.60832 -80.45959,1.45473 C 92.556926,187.6518 60.557883,163.01304 61.6588,170.169 Z;m 62.692,157.03 c -0.66726,4.2839 -1.0332,8.6677 -1.0332,13.139 2e-6,46.793 37.933,84.727 84.727,84.727 46.793,0 84.727,-37.933 84.727,-84.727 0,-4.4706 -0.36411,-8.8552 -1.0312,-13.139 -14.83662,12.68631 -46.3415,32.04916 -110.09827,6.15172 C 55.89527,130.25689 61.6596,170.1687 62.6926,157.03 Z"
             keyTimes="0;1"
          begin="poi_back_1.end"
          dur="1.5s"
        />
     </path>    
     <!--End Poison Back-->
     <!--Begin Poison Front-->
     <g id="poi_front">
     <path 
        fill="#739a00"
        >
          <animate
             id="poi_front_1"
             attributeName="d"
             values="m62.692 157.03c-0.66726 4.2839-1.0332 8.6677-1.0332 13.139 2e-6 46.793 37.933 84.727 84.727 84.727 46.793 0 84.727-37.933 84.727-84.727 0-4.4706-0.36411-8.8552-1.0312-13.139-7.1302-16.488-52.947-12.538-86.979 5.0508-31.611 16.066-81.443 8.0879-80.41-5.0508z; m 62.692,157.03 c -0.66726,4.2839 -1.0332,8.6677 -1.0332,13.139 2e-6,46.793 37.933,84.727 84.727,84.727 46.793,0 84.727,-37.933 84.727,-84.727 0,-4.4706 -0.36411,-8.8552 -1.0312,-13.139 -14.83662,12.68631 -46.3415,32.04916 -110.09827,6.15172 C 55.89527,130.25689 61.6596,170.1687 62.6926,157.03 Z"
             keyTimes="0;1"
              begin="0s;poi_front_2.end"
              dur="1.5s"
              />
              <animate
             id="poi_front_2"
             attributeName="d"
             values="m 62.692,157.03 c -0.66726,4.2839 -1.0332,8.6677 -1.0332,13.139 2e-6,46.793 37.933,84.727 84.727,84.727 46.793,0 84.727,-37.933 84.727,-84.727 0,-4.4706 -0.36411,-8.8552 -1.0312,-13.139 -14.83662,12.68631 -46.3415,32.04916 -110.09827,6.15172 C 55.89527,130.25689 61.6596,170.1687 62.6926,157.03 Z; m62.692 157.03c-0.66726 4.2839-1.0332 8.6677-1.0332 13.139 2e-6 46.793 37.933 84.727 84.727 84.727 46.793 0 84.727-37.933 84.727-84.727 0-4.4706-0.36411-8.8552-1.0312-13.139-7.1302-16.488-52.947-12.538-86.979 5.0508-31.611 16.066-81.443 8.0879-80.41-5.0508z"
             keyTimes="0;1"
              begin="poi_front_1.end "
              dur="1.5s"
              />      
    </path> 
    </g>    
     <!--End Poison Front-->
    </svg>

    1. .pauseAnimations() or .unpauseAnimations() can only be called on <svg> elements, in your case document.getElementById("svg"). They will pause and unpause all animation child elements at once. There is no way to select single animations.

    2. If you want to target single animations, the relevant Interface is ElementTimeControl with methods .beginElement() and .endElement(), to be called on the <animate> element itself, as in document.getElementById("poi_front_1"). Pausing and unpausing the animation in an intermediate state is not possible.

    3. Your technique to start one animation at the end of another makes things pretty complex. Whether the animation simply ends or is stopped programmatically, the endEvent is always raised and triggers a restart. Unfortunately, these events are not cancelable, so a call to Event.preventDefault() does not work.

    It's much more convenient to fold the two alternating animations into one. The values list gets three values: "...state1...;...state2...;...state1...", keyTimes is set to "0;0.5;1", dur is doubled. Additionally, set repeatCount="indefinite" for the animation to run on. Then you can either set begin="0s" to run it automatically or begin="indefinite" to control it programmatically.

    Bonus Tip: if you want the path to stay on screen after the animation is stopped, set fill="freeze".

    var poiFront1 = document.getElementById("poi_front_1")
    var poiFront2 = document.getElementById("poi_front_2")
    
    document.getElementById("button").onclick = function (){
        poiFront1.beginElement();
    }   
    
    document.getElementById("button2").onclick = function (){
        poiFront1.endElement();
    }
    html, body{
       height: 100%;
       margin: 0%;
       padding: 0%;
       overflow: hidden;
    }
    body{
      height: 100vh;
      display: flex;
      background-color: #2a0000;
      box-sizing: border-box;
      flex-direction: column;
      align-items: center;
    }  
    #button{
     width: 60px;
     height: 30px; 
     background-color: orange;
     position: relative;
    margin-top: 5px;
    margin-bottom:  5px;
    }
    #button2{
     width: 60px;
     height: 30px; 
     background-color: yellow; 
     position: relative;
    margin-top: 5px;
    margin-bottom:  5px;
    }
    <button id="button">START</button>
    <button id="button2">STOP</button>
    <svg 
        width="100%" 
        height="100%" 
        version="1.1" 
        viewBox="0 0 300 300" 
        xmlns="http://www.w3.org/2000/svg"
        id="svg">
    <!--Begin Bottle -->
     <path 
        d="m166.76 57.45c-13.062-0.0623-26.955 0.73204-46.311 0.0859-2.7685-0.0924-5 2.23-5 5v10.709c-2e-5 2.77 2.23 5 5 5h4.7226v6.209a88.388 88.388 0 0 0 -67.176 85.715 88.388 88.388 0 0 0 88.389 88.389 88.388 88.388 0 0 0 88.389 -88.389 88.388 88.388 0 0 0 -67.176 -85.695v-6.2285h4.7227c2.77 0 5-2.23 5-5v-10.709c0-2.77-2.231-4.9242-5-5-1.8457-0.0506-3.6945-0.077-5.5606-0.0859z" 
        fill="#1b000c"/>
     <path 
        d="m231.11 170.17c0 46.793-37.933 84.727-84.727 84.727-46.793 0-84.727-37.933-84.727-84.727-1e-6 -40.912 28.997-75.051 67.559-82.986 12.071 2.5421 23.795 1.9463 35.395 0.22531 38.033 8.3382 66.499 42.225 66.499 82.761z" 
        fill="#270600"/>
     <path 
        d="m122.57 61.1c16.221 0.59986 32.004 0.30589 47.553 0 1.8278-0.036 3.3 1.4718 3.3 3.3v6.2541c0 1.8282-1.4718 3.3-3.3 3.3h-47.553c-1.8282 0-3.3-1.4718-3.3-3.3v-6.2541c0-1.8282 1.473-3.3676 3.3-3.3z" 
        fill="#270600"/>
     <path 
        d="m129.08 77.57v6.0117c9.8102 2.4161 28.289 2.4275 35 0v-6.0117z"
        fill="#270600"/>
     <!--End Bottle -->
      <!--Begin Poison Back-->
     <path 
        fill="#9bc300"
        id="poi_back">
        <animate 
         id="poi_back_1"
         attributeName="d"
             values="m 62.692,157.03 c -0.66726,4.2839 -1.0332,8.6677 -1.0332,13.139 2e-6,46.793 37.933,84.727 84.727,84.727 46.793,0 84.727,-37.933 84.727,-84.727 0,-4.4706 -0.36411,-8.8552 -1.0312,-13.139 -14.83662,12.68631 -46.3415,32.04916 -110.09827,6.15172 C 55.89527,130.25689 61.6596,170.1687 62.6926,157.03 Z; m 62.692,157.03 c 0.858416,-8.12931 -0.379575,2.34105 -1.0332,13.139 -2.827316,46.70751 37.933,84.727 84.727,84.727 46.793,0 84.727,-37.933 84.727,-84.727 0,-4.4706 -0.36411,-8.8552 -1.0312,-13.139 -8.28777,2.68817 -30.19905,-24.60832 -80.45959,1.45473 C 92.556926,187.6518 60.557883,163.01304 61.6588,170.169 Z;m 62.692,157.03 c -0.66726,4.2839 -1.0332,8.6677 -1.0332,13.139 2e-6,46.793 37.933,84.727 84.727,84.727 46.793,0 84.727,-37.933 84.727,-84.727 0,-4.4706 -0.36411,-8.8552 -1.0312,-13.139 -14.83662,12.68631 -46.3415,32.04916 -110.09827,6.15172 C 55.89527,130.25689 61.6596,170.1687 62.6926,157.03 Z"
             keyTimes="0;0.5;1"
          begin="0s"
          dur="3s"
          repeatCount="indefinite"
        />
     </path>    
     <!--End Poison Back-->
     <!--Begin Poison Front-->
     <g id="poi_front">
     <path 
        fill="#739a00"
        >
          <animate
             id="poi_front_1"
             attributeName="d"
             values="m62.692 157.03c-0.66726 4.2839-1.0332 8.6677-1.0332 13.139 2e-6 46.793 37.933 84.727 84.727 84.727 46.793 0 84.727-37.933 84.727-84.727 0-4.4706-0.36411-8.8552-1.0312-13.139-7.1302-16.488-52.947-12.538-86.979 5.0508-31.611 16.066-81.443 8.0879-80.41-5.0508z; m 62.692,157.03 c -0.66726,4.2839 -1.0332,8.6677 -1.0332,13.139 2e-6,46.793 37.933,84.727 84.727,84.727 46.793,0 84.727,-37.933 84.727,-84.727 0,-4.4706 -0.36411,-8.8552 -1.0312,-13.139 -14.83662,12.68631 -46.3415,32.04916 -110.09827,6.15172 C 55.89527,130.25689 61.6596,170.1687 62.6926,157.03 Z; m62.692 157.03c-0.66726 4.2839-1.0332 8.6677-1.0332 13.139 2e-6 46.793 37.933 84.727 84.727 84.727 46.793 0 84.727-37.933 84.727-84.727 0-4.4706-0.36411-8.8552-1.0312-13.139-7.1302-16.488-52.947-12.538-86.979 5.0508-31.611 16.066-81.443 8.0879-80.41-5.0508z"
             keyTimes="0;0.5;1"
              begin="indefinite"
              dur="3s"
              repeatCount="indefinite"
              />      
    </path> 
    </g>    
     <!--End Poison Front-->
    </svg>