Svg路径填充动画

时间:2017-03-22 11:28:40

标签: html css svg svg-animate

我有一条类似于圆圈的路径。任务是使用带动画的颜色填充路径。填充动画应采用圆形方式,而不是从上到下或从下到上。

我的svg代码是:



<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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

常见的“dasharray”线条绘制技术适用于您,并结合使用箭头形状作为蒙版。

This technique is described in this SO question

但是,由于箭头形状的头部与尾部重叠,为了获得“完美”效果,您可能需要将扫描分为两部分。您将使用尾部蒙版绘制尾部,然后使用单独的蒙版绘制形状的后半部分(包括箭头)。

这是一个粗略的不完美版本,展示了这项技术。

<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>

这里我们为一条非常粗的绿线设置动画,但是将你的形状用作剪切路径,以便它符合你想要的形状。

但正如我所提到的,头部与尾部末端重叠的部分并不完美,因此您可能需要将动画分成两部分,如上所述。

相关问题