怎么在JavaScript中利用canvas实现一个雨滴效果-创新互联

怎么在JavaScript中利用canvas实现一个雨滴效果?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联建站是一家专业提供渌口企业网站建设,专注与网站制作、成都网站制作H5高端网站建设、小程序制作等业务。10年已为渌口众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。

一、雨滴特效需求


雨滴从窗口顶部随机下落到达底部将呈现波纹逐渐散开变淡直到消失,雨滴特效随窗口变化自适应

二、雨滴实现思路

1. 用面向对象的思维 首先创建canvas画布 ,绘制一个雨滴的初始化形状
2. 在给雨滴添加运动的方法
3. 通过定时器让雨滴运动起来


三、具体分析

1.雨滴初始化需要的属性有哪些?
坐标x,y 宽高w,h 。
2.雨滴下落是逐渐加速下落不是匀速需要给一个加速度的属性,也就是y轴坐标不断加上加速度的值
3.雨滴下落到底部某一个区域后开始呈现波纹逐渐散开,也就是到达底部某个范围内开始画圆,圆逐渐变大并且变淡加上透明度
4.雨滴下落拖尾效果需要绘制一层阴影覆盖之前运动的雨滴


四、代码

<!DOCTYPE html>
<html lang="en">

<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>canvas</title>
 <style>
  * {
   margin: 0;
   padding: 0;
  }

  canvas {
   vertical-align: middle;
   background: #000;
  }
 </style>
</head>

<body>
 <canvas id="myCanvas"></canvas>
 <script>
  // 创建画布
  let myCanvas = document.getElementById('myCanvas')
  let ctx = myCanvas.getContext('2d')
  // 自适应窗口
  let width = myCanvas.width = window.innerWidth
  let height = myCanvas.height = window.innerHeight
  window.addEventListener('resize', () => {
   width = myCanvas.width = window.innerWidth
   height = myCanvas.height = window.innerHeight
  })
  // 绘制雨滴
  let raindropArr = []
  function Raindrop(x, y, w, h, l, r, dr, maxR, a, va) {
   this.x = rand(0, window.innerWidth) // 雨滴的x轴
   this.y = y || 0 // 雨滴的y轴
   this.dy = rand(2, 4) // 雨滴的加速度
   this.w = w || 2 // 雨滴的宽度
   this.h = h || 10 // 雨滴的高度
   this.l = rand(0.8 * height, 0.9 * height) // 雨滴的下落高度
   this.r = r || 1 // 波纹半径
   this.dr = dr || 1 // 波纹增加半径
   this.maxR = maxR || 50 // 波纹较大半径
   this.a = a || 1 // 波纹透明度
   this.va = 0.96 // 波纹透明度系数
  }
  Raindrop.prototype = {
   draw: function (index) { // 绘制雨滴
    if (this.y > this.l) {
     ctx.beginPath()
     ctx.arc(this.x, this.y, this.r, 0, Math.PI * 2)
     ctx.strokeStyle = `rgba(0,191,255,${this.a})`
     ctx.stroke()
    } else {
     ctx.fillStyle = 'skyBlue'
     ctx.fillRect(this.x, this.y, this.w, this.h)
    }
    this.update(index)
   },
   update: function (index) { // 更新雨滴坐标 运动起来
    if (this.y > this.l) {
     if (this.a > 0.03) {
      this.r += this.dr
      if (this.r > this.maxR) {
       this.a *= this.va
      }
     } else {
      this.a = 0
      raindropArr.splice(index, 1)
     }
    } else {
     this.y += this.dy
    }
   }
  }
  function rand(min, max) {
   return Math.random() * (max - min) + min
  }
  setInterval(() => {
   let raindrop = new Raindrop()
   raindropArr.push(raindrop)
  }, 100)
  setInterval(() => {
   ctx.fillStyle = 'rgba(0, 0, 0, 0.1)'
   ctx.fillRect(0, 0, myCanvas.width, myCanvas.height)
   for (let i = 0; i < raindropArr.length; i++) {
    raindropArr[i].draw(i)
   }
  }, 30)
 </script>
</body>

</html>

关于怎么在JavaScript中利用canvas实现一个雨滴效果问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。

名称栏目:怎么在JavaScript中利用canvas实现一个雨滴效果-创新互联
标题路径:https://www.cdcxhl.com/article14/ccisde.html

成都网站建设公司_创新互联,为您提供网站维护网站建设商城网站网站改版云服务器域名注册

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站建设网站维护公司