Search code examples
htmlcsssvgsvg-animate

Svg path fill with animation


I have a path which is similar to circle. The task is to fill the path with a color with animation. The fill animation should be in circle manner and not from top to bottom or bottom to top.

my svg code is :

<svg id="svg_circle" width="100%" height="100%" xmlns="http://www.w3.org/2000/svg" viewBox = '0 0 450 400'>
    <g class="svg_circle" transform = "translate(0,0)">
        <path class="path" stroke="#F0F0F0" fill="#fff" stroke-width="1" opacity="1" d="m-0.25,238.11061l88.59461,-82.21665l87.56445,86.40604l-33.99561,0.52367c2.72107,17.03346 46.55824,67.96739 105.37633,63.99055c70.95792,-4.79765 101.17847,-64.19902 103.74816,-97.50561c7.13262,-92.44812 -81.66575,-121.29229 -115.80064,-115.90062c-119.13463,18.8176 -96.38311,112.29843 -96.38311,112.29843l-50.54082,-49.76652l-46.48973,43.1249c-12.30406,-104.7234 83.23188,-194.53124 191.25985,-198.17803c97.87838,-3.30416 202.62703,53.17701 213.76024,178.57248c16.06879,180.98587 -165.14043,220.64431 -208.6094,218.37164c-143.15297,-7.48456 -189.38275,-115.91408 -199.33787,-158.14925l-39.14646,-1.57102z" id="svg_1">
            <animate id="project_anim1" attributeName="fill" from="#fff" to="#4DAF4C" begin="1s" dur="1s" fill="freeze" repeatCount="1"></animate>
        </path>
    </g>
</svg>


Solution

  • The common "dasharray" line drawing technique would work for you, in combination with using the arrow shape as a mask.

    This technique is described in this SO question

    However, since the head of your arrow shape overlaps the tail, to get a "perfect" result, you may have to divide the sweep into two parts. You would draw the tail part with a tail mask, and then the second half of the shape (including the arrow head) with a separate mask.

    Here's a rough imperfect version showing the technique.

    <svg id="svg_circle" width="100%" height="100%" xmlns="http://www.w3.org/2000/svg" viewBox = '0 0 450 400'>
      <defs>
        <path id="arrow" stroke="#F0F0F0" fill="none" stroke-width="1" opacity="1" d="m-0.25,238.11061l88.59461,-82.21665l87.56445,86.40604l-33.99561,0.52367c2.72107,17.03346 46.55824,67.96739 105.37633,63.99055c70.95792,-4.79765 101.17847,-64.19902 103.74816,-97.50561c7.13262,-92.44812 -81.66575,-121.29229 -115.80064,-115.90062c-119.13463,18.8176 -96.38311,112.29843 -96.38311,112.29843l-50.54082,-49.76652l-46.48973,43.1249c-12.30406,-104.7234 83.23188,-194.53124 191.25985,-198.17803c97.87838,-3.30416 202.62703,53.17701 213.76024,178.57248c16.06879,180.98587 -165.14043,220.64431 -208.6094,218.37164c-143.15297,-7.48456 -189.38275,-115.91408 -199.33787,-158.14925l-39.14646,-1.57102z" id="svg_1"/>
        <clipPath id="arrow-clip" clipPathUnits="userSpaceOnUse">
          <use xlink:href="#arrow"/>
        </clipPath>
      </defs>
    
      <g clip-path="url(#arrow-clip)">
        <circle cx="244" cy="200" r="158" transform="rotate(-164,244,200)"
                fill="none" stroke="#4DAF4C" stroke-width="175"
                stroke-dasharray="993 993">
          <animate attributeName="stroke-dashoffset" from="993" to="0" begin="1s" dur="1s" fill="freeze" repeatCount="1"/>
        </circle>
      </g>
      <use xlink:href="#arrow"/>
    </svg>

    Here we are animating a really thick green line, but using your shape as a clipping path so it conforms to the shape you want.

    But as I mentioned, the section where the head overlaps the end of the tail is not perfect, so you may need to divide the animation into two parts as described above.