【问题标题】:Hard svg animation硬 svg 动画
【发布时间】:2022-02-01 13:32:29
【问题描述】:

在实现 svg 动画方面我需要您的帮助。 https://youtu.be/lrWjkARl8Zg (抱歉视频质量差)

我有这样的svg结构

我需要箭头 (class= "arrow") 本身沿着长线移动并绘制它。

 <svg class="vector" width="1193" height="329" viewBox="0 0 1193 329" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path class="long_line" d="M1436 327.98L84.7148 327.98C73.8082 328.219 62.9639 326.275 52.818 322.262C42.6722 318.249 33.4293 312.249 25.6315 304.613C17.8337 296.977 11.6384 287.858 7.40866 277.793C3.17891 267.728 0.999997 256.919 0.999996 246C0.999996 235.081 3.17891 224.272 7.40866 214.207C11.6384 204.142 17.8337 195.023 25.6315 187.387C33.4292 179.751 42.6722 173.75 52.818 169.738C62.9638 165.725 73.8082 163.781 84.7147 164.02L589.173 164.02" stroke="black" stroke-width="2" stroke-miterlimit="10"/>
            <path class="arrow" d="M544 204L589 164L548 124" stroke="black" stroke-width="2"/>
            <path class="arrow" d="M504 1L459 41L500 81" stroke="black" stroke-width="2"/>
            <path class="short_line" d="M1308 41L459 41" stroke="black" stroke-width="2" stroke-miterlimit="10"/>
        </svg>

建议我可以使用哪个库

【问题讨论】:

    标签: animation svg css-animations


    【解决方案1】:

    您可以使用 svg smil 动画
    A Guide to SVG Animations (SMIL)

    您的箭头动画可以通过以下方式实现 &lt;animateMotion&gt; 用于移动箭头元素和 为stroke-dashoffset 属性设置动画。

    动画示例

    svg {
      border: 1px solid #ccc;
      width: 33%;
      overflow: visible;
      display: block;
    }
    
    path {
      stroke-width: 10;
      stroke: #000;
    }
    <p>Click on animation for replay</p>
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1400 500">
      <defs>
        <path id="arrow" fill="none" d="M-45,40L0,0l-41-40" />
      </defs>
      <use id="arrow1" href="#arrow" />
      <use id="arrow2" href="#arrow" />
    
      <g id="graphics">
        <rect id="graphics" fill="#fff" fill-opacity="0" x="0" y="0" width="100%" height="100%" />
        <path id="mpath-long" fill="none" pathLength="100"  stroke-width="2" stroke-miterlimit="10" stroke-dashoffset="100" stroke-dasharray="100" d="M1571,413H219.7
        c-10.9,0.2-21.8-1.7-31.9-5.7c-10.1-4-19.4-10-27.2-17.6c-7.8-7.6-14-16.8-18.2-26.8S136,341.9,136,331s2.2-21.7,6.4-31.8
        s10.4-19.2,18.2-26.8c7.8-7.6,17-13.6,27.2-17.6c10.1-4,21-6,31.9-5.7h504.5" />
        <path id="mpath-short" fill="none" pathLength="100" stroke-width="2" stroke-miterlimit="10" stroke-dashoffset="100" stroke-dasharray="100" d="M1443,126H594" />
      </g>
    
      <animateMotion href="#arrow1" dur="2" rotate="auto" repeatCount="1" begin="0;graphics.click" fill="freeze">
        <mpath href="#mpath-long" />
      </animateMotion>
    
      <animate attributeType="XML" href="#mpath-long" id="strokeAni" attributeName="stroke-dashoffset" from="100" to="0" dur="2s" repeatCount="1" begin="0;graphics.click" fill="freeze" />
    
      <animateMotion href="#arrow2" dur="2s" rotate="auto" repeatCount="1" begin="0;graphics.click" fill="freeze">
        <mpath href="#mpath-short" />
      </animateMotion>
    
      <animate attributeType="XML" href="#mpath-short" attributeName="stroke-dashoffset" from="100" to="0" dur="2s" repeatCount="1" fill="freeze" begin="0;graphics.click" />
    
    </svg>

    很有可能,您将不得不调整您的 svg viewBox 以获得所需的结果。
    一个常见的技巧是将应该沿着运动路径移动到 x/y = 0 的元素定位。

    静态示例

    <style>
    svg{
      border: 1px solid #ccc;
      width: 33%;
      overflow:visible;
      display:block;
    }
    
    path{
      stroke-width:10;
      stroke: #000;
    }
    </style>
    
    <svg xmlns="http://www.w3.org/2000/svg"  viewBox="0 0 1400 500">
      <defs>
        <path id="arrow" fill="none" d="M-45,40L0,0l-41-40" />
      </defs>
      <use id="arrow1" href="#arrow" />
      <use id="arrow2" href="#arrow" />
      <g id="graphics">
        <rect id="graphics" fill="#fff" fill-opacity="0" x="0" y="0" width="100%" height="100%" />
        <path id="mpath-long" fill="none" pathLength="100"  stroke-width="2" stroke-miterlimit="10" stroke-dashoffset="0" stroke-dasharray="100" d="M1571,413H219.7
        c-10.9,0.2-21.8-1.7-31.9-5.7c-10.1-4-19.4-10-27.2-17.6c-7.8-7.6-14-16.8-18.2-26.8S136,341.9,136,331s2.2-21.7,6.4-31.8
        s10.4-19.2,18.2-26.8c7.8-7.6,17-13.6,27.2-17.6c10.1-4,21-6,31.9-5.7h504.5" />
        <path id="mpath-short" fill="none" pathLength="100" stroke-width="2" stroke-miterlimit="10" stroke-dashoffset="0" stroke-dasharray="100" d="M1443,126H594" />
      </g>
    </svg>

    【讨论】:

      猜你喜欢
      • 2018-09-10
      • 2019-01-29
      • 2018-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-29
      • 2012-03-28
      • 2015-12-05
      相关资源
      最近更新 更多