css 给定路径的Svg填充动画

iswrvxsc  于 2023-01-18  发布在  其他
关注(0)|答案(4)|浏览(242)

我试图从左到右的箭头动画。我的箭头的路径代码如下所示:

<svg id="svg_circle" width="100%" height="100%" viewBox = '0 0 450 400'> 
    <g transform = "translate(0,0)"> 
      <path class="path" stroke="#F0F0F0" fill="#fff" stroke-width="1" opacity="1" d="m34.97813,21.70979l-33.55223,0.47088l-0.0394,-13.57138l34.2665,-0.47295l-0.0208,-7.14282l14.50618,14.42226l-14.95643,15.04345l-0.20382,-8.74944z" 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>

以上是我的箭头的svg路径内容。
请任何人帮助我如何填补从左到右的路径。等待快速React

yacmzcpb

yacmzcpb1#

您可以通过在<linear gradient>中设置<stop>的动画来完成此操作。

<svg id="svg_circle" width="100%" height="100%" viewBox = '0 0 450 400'> 

  <defs>
    <linearGradient id="left-to-right">
      <stop offset="0" stop-color="#4DAF4C">
        <animate dur="2s" attributeName="offset" fill="freeze" from="0" to="1" />
      </stop>
      <stop offset="0" stop-color="#fff">
        <animate dur="2s" attributeName="offset" fill="freeze" from="0" to="1" />
      </stop>
      
    </linearGradient>
  </defs>

  <path class="path" stroke="#F0F0F0" fill="url(#left-to-right)" stroke-width="1" opacity="1" d="m34.97813,21.70979l-33.55223,0.47088l-0.0394,-13.57138l34.2665,-0.47295l-0.0208,-7.14282l14.50618,14.42226l-14.95643,15.04345l-0.20382,-8.74944z" id="svg_1" />
</svg>

它的工作原理是,我们有一个线性梯度来表示从绿色到白色的突变,<animation>元素将突变的位置从箭头的左边(offset=0)移动到右边(offset=“1”)。
注意SVG <animate>元素在IE中不起作用,如果你需要支持IE,你需要使用FakeSmile库或者使用其他方法(比如JS动画库)。

w46czmvw

w46czmvw2#

andreas ' answer的基础上构建。你可以用一个动画形状来覆盖你的箭头。

<svg id="svg_circle" width="450" height="400" viewBox='0 0 450 400'> 
  <path class="path" stroke="#F0F0F0" fill="#fff"
    stroke-width="1" opacity="1" id="svg_1"
    d="m34.97813,21.70979l-33.55223,0.47088l-0.0394,
       -13.57138l34.2665,-0.47295l-0.0208,-7.14282
       l14.50618,14.42226l-14.95643,15.04345l-0.20382,
       -8.74944z"> 
   <animate id="project_anim1" attributeName="fill"
      from="#fff" to="#4DAF4C" begin="0s" dur="3s"
      fill="freeze" repeatCount="indefinite" />
  </path>
  <rect x="0" y="0" width="53" height="34" fill="#fff">
    <animate attributeType="XML" attributeName="x"
      from="0" to="53" begin="0s" dur="3s"
      repeatCount="indefinite" />
    <animate attributeType="XML" attributeName="width"
      from="53" to="0" begin="0s" dur="3s"
      repeatCount="indefinite" />
  </rect>
</svg>
jchrr9hc

jchrr9hc3#

我不认为fill属性可以实现这一点。但是,您可以将SVG路径反转为一个带有类似三角形的孔的矩形。现在,您只需要在该路径后面添加第二个元素,您可以在其中简单地在x方向上动画缩放,以从左到右填充孔。
下面是展示该技术的图像:

下面是一个工作示例:

<svg width="100%" height="100%" viewBox='0 0 450 400'>
  <rect x="0" y="0" width="1" height="22" style="fill: black;" >
    <animateTransform attributeName="transform" type="scale" from="1 1" to="50 1" begin="0s" dur="2s" repeatCount="indefinite" />
  </rect>
  <path fill="#ffffff" d="M0,0v29.8h86V0H0z M6.5,25V5.5L48.8,25H6.5z"/>
</svg>

注:答案从三角形更新为箭头,我不会更新我的答案,因为技术是相同的每一个形状。

vqlkdk9b

vqlkdk9b4#

<svg id="svg_circle" width="100%" height="100%" viewBox = '0 0 450 400'> 

  <defs>
    <linearGradient id="left-to-right">
      <stop offset="0" stop-color="#4DAF4C">
        <animate dur="2s" attributeName="offset" fill="freeze" from="0" to="1" />
      </stop>
      <stop offset="0" stop-color="#fff">
        <animate dur="2s" attributeName="offset" fill="freeze" from="0" to="1" />
      </stop>
      
    </linearGradient>
  </defs>

  <path class="path" stroke="#F0F0F0" fill="url(#left-to-right)" stroke-width="1" opacity="1" d="m34.97813,21.70979l-33.55223,0.47088l-0.0394,-13.57138l34.2665,-0.47295l-0.0208,-7.14282l14.50618,14.42226l-14.95643,15.04345l-0.20382,-8.74944z" id="svg_1" />
</svg>

相关问题