【问题标题】:Continuous wave like animation from wave line svg来自波浪线 svg 的连续波浪状动画
【发布时间】:2017-11-10 18:43:05
【问题描述】:

您好,我有一个 svg,它是用 adobe illustrator 构建的波浪线,我正在尝试向其添加类似动画的连续波浪。

我已经尝试了 vivus.js,并且能够让绘图动画正常工作,但不是连续的。

有没有人知道如何开始这样的事情?我不希望有人为我解决问题,而是为我指出正确的方向,尽管如果有人能快速回答,我不反对。

任何答案都可以使用纯 css、js 或两者兼而有之。

这是svg

<svg id="wave" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     viewBox="0 0 456.7 39.9" style="enable-background:new 0 0 456.7 39.9;" xml:space="preserve">
<style type="text/css">
    .st69{fill:none;stroke:#000000;stroke-width:12;stroke-miterlimit:10;}
</style>
    <path class="st69" d="M4.2,33.2c0.1-0.1,7-6.9,15.9-13.8C27.7,13.7,38.7,6,47.5,6c7.5,0,14,6.6,20.3,12.9l0.4,0.4
    c6.8,6.9,14.6,14.6,24.6,14.6c9.9,0,17.7-7.8,24.5-14.6l0.5-0.5C124,12.5,130.5,6,137.9,6c7.5,0,13.9,6.5,20.2,12.9l0.4,0.4
    c6.8,6.9,14.6,14.6,24.5,14.6c10,0,17.8-7.8,24.6-14.6l0.5-0.5C214.4,12.5,220.9,6,228.4,6c7.5,0,14,6.5,20.2,12.9l0.4,0.4
    c6.8,6.9,14.5,14.6,24.5,14.6c9.9,0,17.7-7.8,24.5-14.6l0.3-0.3c6.3-6.4,12.9-13,20.5-13c7.5,0,14.1,6.6,20.4,13l0.3,0.3
    c6.8,6.9,14.6,14.6,24.5,14.6c9.9,0,17.6-7.8,24.5-14.6l0.2-0.2C395.1,12.6,401.6,6,409.2,6c8.7,0,19.8,7.7,27.3,13.4
    c8.9,6.8,15.9,13.7,16,13.8"/>
</svg>

和 jsfiddle 链接

here

谢谢!!

【问题讨论】:

    标签: javascript css svg css-animations


    【解决方案1】:

    我希望这会对你有所帮助。稍微调整一下这些值或添加 SVG 波形作为背景,重复此操作并更改 background-position

    @keyframes wave {
      0% { 
        left: -80px; 
      }
      100% { 
        left: 0; 
      }
    }
    
    .container {
      width: 100px;
      overflow: hidden;
    }
    .container svg {
      position: relative;
      left: -50px;
      width: 200px;
      animation: wave 2s linear infinite;
    }
    <div class="container">
    <svg id="wave" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
         viewBox="0 0 456.7 39.9" style="enable-background:new 0 0 456.7 39.9;" xml:space="preserve">
    <style type="text/css">
        .st69{fill:none;stroke:#000000;stroke-width:12;stroke-miterlimit:10;}
    </style>
        <path class="st69" d="M4.2,33.2c0.1-0.1,7-6.9,15.9-13.8C27.7,13.7,38.7,6,47.5,6c7.5,0,14,6.6,20.3,12.9l0.4,0.4
    	c6.8,6.9,14.6,14.6,24.6,14.6c9.9,0,17.7-7.8,24.5-14.6l0.5-0.5C124,12.5,130.5,6,137.9,6c7.5,0,13.9,6.5,20.2,12.9l0.4,0.4
    	c6.8,6.9,14.6,14.6,24.5,14.6c10,0,17.8-7.8,24.6-14.6l0.5-0.5C214.4,12.5,220.9,6,228.4,6c7.5,0,14,6.5,20.2,12.9l0.4,0.4
    	c6.8,6.9,14.5,14.6,24.5,14.6c9.9,0,17.7-7.8,24.5-14.6l0.3-0.3c6.3-6.4,12.9-13,20.5-13c7.5,0,14.1,6.6,20.4,13l0.3,0.3
        c6.8,6.9,14.6,14.6,24.5,14.6c9.9,0,17.6-7.8,24.5-14.6l0.2-0.2C395.1,12.6,401.6,6,409.2,6c8.7,0,19.8,7.7,27.3,13.4
    	c8.9,6.8,15.9,13.7,16,13.8"/>
    </svg>
    </div>

    【讨论】:

    • 这正是我想要的
    【解决方案2】:

    除了 css 技巧之外,您还可以使用带区间的 Math.sin,将其应用于 Bezier 曲线(循环并加入属性字符串),然后就完成了。

    function anim(){
    	document.querySelectorAll('svg circle').forEach((c,i)=>c.setAttribute('cy',50+Math.sin(performance.now()/1000+i)*25))
    }
    setInterval(anim,20)
    <svg id="wave" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
         viewBox="0 0 456 100" style="enable-background:new 0 0 456 100;" xml:space="preserve">
    <style type="text/css">
        .st69{fill:none;stroke:#000000;stroke-width:12;stroke-miterlimit:10;}
    </style>
        <circle cx="50" cy="50" r="4"  fill="red" />
        <circle cx="100" cy="50" r="4"  fill="red" />
        <circle cx="150" cy="50" r="4"  fill="red" />
        <circle cx="200" cy="50" r="4" fill="red" />
        <circle cx="250" cy="50" r="4"  fill="red" />
        <circle cx="300" cy="50" r="4"  fill="red" />
        <circle cx="350" cy="50" r="4"  fill="red" />
        <circle cx="400" cy="50" r="4" fill="red" />
    </svg>

    【讨论】:

    • 谢谢您,先生,这有帮助。我已经接受了另一个答案,但我给了你一个赞成票
    猜你喜欢
    • 2019-11-14
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 2020-08-11
    • 1970-01-01
    • 2011-01-09
    相关资源
    最近更新 更多