从lineto到cubic-bezier曲线命令的SVG路径动画

问题描述 投票:0回答:1

我试图动画svg <path>元素。最初的<path>元素有L(lineto)命令,而我试图动画它的<path>C(cubic-bezier curve)命令。我尝试过动画,但它并没有真正在两条路径之间转换。这样的事情可能吗?

<svg>
  <path id="path" d="M0,50 L25,40 L50,60 L75,40 L100,60 L125,40 L150,50" fill="none" stroke-width="2" stroke="#000" />
  <animate xlink:href="#path"
           attributeName="d"
           dur="3s"
           from="M 0 50 L 25 40 L 50 60 L 75 40 L 100 60 L 125 40 L 150 50"
           to="M 0 80 C 25 55 50 55 75 80 C 100 105 125 105 150 80"
           repeatCount="indefinite" />
</svg>
animation svg svg-animate
1个回答
3
投票

正如您所发现的,路径数据动画需要列出完全相同的命令序列和点才能工作。也就是说,您可以根据需要将路径划分为多个线段,如果控制点位于直线上,则立方贝塞尔曲线可以描述直线。为了你的例子,你需要

  • 将每个L片段重写为C片段
  • 将每个C段划分为三个C段,使曲线不变

这两项任务最好使用具有适当工具的Inkscape等图形编辑器执行。

<svg>
  <path id="path" d="M0,50 L25,40 L50,60 L75,40 L100,60 L125,40 L150,50" fill="none" stroke-width="2" stroke="#000" />
  <animate xlink:href="#path"
           attributeName="d"
           dur="3s"
           from="M 0,50 
                 C 12.5,45 12.5,45 25,40
                 37.5,50 37.5,50 50,60
                 62.5,50 62.5,50 75,40
                 87.5,50 87.5,50 100,60
                 112.5,50 112.5,50 125,40
                 137.5,45 137.5,45 150,50"
           to="M 0,80
               C 8.33333,71.6667 16.6667,66.1111 25,63.3333
               33.3333,60.5556 41.6667,60.5556 50,63.3333
               58.3333,66.1111 66.6667,71.6667 75,80
               83.3333,88.3333 91.6667,93.8889 100,96.6667
               108.333,99.4444 116.667,99.4444 125,96.6667
               133.333,93.8889 141.667,88.3333 150,80"
           repeatCount="indefinite" />
</svg>
© www.soinside.com 2019 - 2024. All rights reserved.