vue + svg 绘制水波纹、波浪动画效果

1 篇文章 0 订阅

效果:

实现可配置图像大小水面高度波浪高度波浪速度

<template>
  <div class="wave-svg" :style="`width:${width}px;height:${height}px;`">
    <div style="width:100%;height:100%;">
      <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" 
        :width="width" 
        :height="height" 
        :viewbox="`0 0 ${width} ${height}`">
      <defs>
        <linearGradient id="line1" x1="0%" y1="0%" x2="0%" y2="100%">
          <stop offset="0%" style="stop-color:#3DCDEA;stop-opacity:1"/>
          <stop offset="100%" style="stop-color:#0070EA; stop-opacity:1"/>
        </linearGradient>
      </defs>
      <defs>
        <linearGradient id="line2" x1="0%" y1="0%" x2="0%" y2="100%">
          <stop offset="0%" style="stop-color:#4AB5FF;stop-opacity:1"/>
          <stop offset="100%" style="stop-color:#3686FF; stop-opacity:1"/>
        </linearGradient>
      </defs>
      <defs>
        <linearGradient id="line3" x1="0%" y1="0%" x2="0%" y2="100%">
          <stop offset="0%" style="stop-color:#49A1FF;stop-opacity:1"/>
          <stop offset="100%" style="stop-color:#3686FF;stop-opacity:1"/>
        </linearGradient>
      </defs>
      <path v-if="topLinePath" :d="topLinePath" stroke="transparent" fill="url(#line3)">
        <animateTransform attributeType="XML" attributeName="transform" begin="0s" :dur="`${speedTop}s`" type="translate" from="0 0" :to="`${bulgeDistance*4} 0`" repeatCount="indefinite" />
      </path>
      <path v-if="centerLinePath" :d="centerLinePath" stroke="transparent" fill="url(#line2)">
        <animateTransform attributeType="XML" attributeName="transform" begin="0s" :dur="`${speedCenter}s`" type="translate" from="0 0" :to="`${bulgeDistance*4} 0`" repeatCount="indefinite" />
      </path>
      <path v-if="bottomLinePath" :d="bottomLinePath" stroke="transparent" fill="url(#line1)">
        <animateTransform attributeType="XML" attributeName="transform" begin="0s" :dur="`${speedBottom}s`" type="translate" from="0 0" :to="`${bulgeDistance*4} 0`" repeatCount="indefinite" />
      </path>
    </svg>
    </div>
  </div>
</template>
<script>
export default {
  props:{
    width:{
      type:Number,      //图像宽度
      default:450
    },
    height:{
      type:Number,      //图像高度
      default:400
    },
    waterHeight:{
      type:Number,      //水面高度
      default:360
    },
    bulgeHeight:{
      type:Number,      //波浪起伏高度
      default:15
    },
    bulgeDistance:{
      type:Number,      //波浪起伏间隔
      default:30
    },
    lineDistance:{
      type:Number,      //三条线的距离
      default:20
    },
    speedTop:{
      type:Number,      //第一根线速度
      default:1
    },
    speedCenter:{
      type:Number,      //第一根线速度
      default:1
    },
    speedBottom:{
      type:Number,      //第一根线速度
      default:1
    },
  },
  data(){
    return{
      topLinePath:null,
      centerLinePath:null,
      bottomLinePath:null,
    }
  },
  mounted(){
    this.init()
  },
  methods:{
    init(){
      let num = Math.floor(this.width/this.bulgeDistance)
      let basicPoint = this.height - this.waterHeight
      let basicPointCenter = this.height - this.waterHeight + this.lineDistance
      let basicPointBottom = this.height - this.waterHeight + this.lineDistance*2
      let topLinePath = `M ${-4*this.bulgeDistance} ${basicPoint} Q ${-3*this.bulgeDistance} ${basicPoint - this.bulgeHeight} ${-2*this.bulgeDistance} ${basicPoint} T 0 ${basicPoint}`
      for(var i=1;i<num;i++){
        topLinePath += ` T ${this.bulgeDistance*i*2} ${basicPoint} `
      }
      topLinePath += `v ${this.height} h -${3*num*this.bulgeDistance} z`
      let centerLinePath = `M ${-5*this.bulgeDistance} ${basicPointCenter} Q ${-4*this.bulgeDistance} ${basicPointCenter - this.bulgeHeight} ${-3*this.bulgeDistance} ${basicPointCenter} T ${-this.bulgeDistance} ${basicPointCenter}`
      for(var i=1;i<num;i++){
        centerLinePath += ` T ${-this.bulgeDistance + this.bulgeDistance*i*2} ${basicPointCenter} `
      }
      centerLinePath += `v ${this.height} h -${3*num*this.bulgeDistance} z`
      let bottomLinePath = `M ${-4*this.bulgeDistance} ${basicPointBottom} Q ${-3*this.bulgeDistance} ${basicPointBottom - this.bulgeHeight} ${-2*this.bulgeDistance} ${basicPointBottom} T 0 ${basicPointBottom}`
      for(var i=1;i<num;i++){
        bottomLinePath += ` T ${this.bulgeDistance*i*2} ${basicPointBottom} `
      }
      bottomLinePath += `v ${this.height} h -${3*num*this.bulgeDistance} z`
      this.topLinePath = topLinePath
      this.centerLinePath = centerLinePath
      this.bottomLinePath = bottomLinePath
    }
  },
  watch:{
    waterHeight(){
      this.init()
    }
  }
}
</script>
<style lang="scss">
// .wave-svg{
//   width: 100%;
//   height: 100%;
// }
</style>

 

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的 Vue 水波纹循环动画实现: 1. 首先,我们需要在 Vue 的 `template` 中添加一个 `div` 元素,作为容器来显示水波纹动画: ```html <template> <div class="ripple-container"> <div class="ripple"></div> </div> </template> ``` 2. 然后,在 `style` 标签中添加样式,使容器和水波纹的位置、大小、颜色等属性符合我们的需求: ```css <style> .ripple-container { position: relative; width: 200px; height: 200px; border-radius: 50%; overflow: hidden; background-color: #ccc; } .ripple { position: absolute; width: 0; height: 0; margin: auto; left: 0; right: 0; top: 0; bottom: 0; background-color: rgba(255, 255, 255, 0.7); border-radius: 50%; opacity: 0.7; animation: ripple 2s linear infinite; } @keyframes ripple { from { width: 0; height: 0; opacity: 0.7; } to { width: 400px; height: 400px; opacity: 0; } } </style> ``` 3. 最后,在 Vue 的 `script` 中添加逻辑代码,使水波纹动画循环播放: ```js <script> export default { name: 'RippleAnimation', created() { setInterval(() => { const ripple = document.querySelector('.ripple'); ripple.classList.remove('ripple'); void ripple.offsetWidth; ripple.classList.add('ripple'); }, 2000); }, }; </script> ``` 以上就是一个简单的 Vue 水波纹循环动画实现。需要注意的是,为了实现动画循环播放,我们在 `created()` 生命周期中使用 `setInterval()` 定时器来不断地重复添加、移除类名,以触发动画效果

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值