一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服務(wù)器之家 - 編程語言 - JavaScript - js教程 - JavaScript canvas實(shí)現(xiàn)雨滴特效

JavaScript canvas實(shí)現(xiàn)雨滴特效

2021-12-29 16:02huangdong1931 js教程

這篇文章主要為大家詳細(xì)介紹了JavaScript canvas實(shí)現(xiàn)雨滴特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了canvas實(shí)現(xiàn)雨滴特效的具體代碼,供大家參考,具體內(nèi)容如下

一、雨滴特效需求

雨滴從窗口頂部隨機(jī)下落到達(dá)底部將呈現(xiàn)波紋逐漸散開變淡直到消失,雨滴特效隨窗口變化自適應(yīng)

二、雨滴實(shí)現(xiàn)思路

1. 用面向?qū)ο蟮乃季S 首先創(chuàng)建canvas畫布 ,繪制一個(gè)雨滴的初始化形狀
2. 在給雨滴添加運(yùn)動的方法
3. 通過定時(shí)器讓雨滴運(yùn)動起來

三、具體分析

1.雨滴初始化需要的屬性有哪些?
坐標(biāo)x,y 寬高w,h 。
2.雨滴下落是逐漸加速下落不是勻速需要給一個(gè)加速度的屬性,也就是y軸坐標(biāo)不斷加上加速度的值
3.雨滴下落到底部某一個(gè)區(qū)域后開始呈現(xiàn)波紋逐漸散開,也就是到達(dá)底部某個(gè)范圍內(nèi)開始畫圓,圓逐漸變大并且變淡加上透明度
4.雨滴下落拖尾效果需要繪制一層陰影覆蓋之前運(yùn)動的雨滴

四、代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!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>
  // 創(chuàng)建畫布
  let myCanvas = document.getElementById('myCanvas')
  let ctx = myCanvas.getContext('2d')
  // 自適應(yīng)窗口
  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 // 波紋透明度系數(shù)
  }
  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) { // 更新雨滴坐標(biāo) 運(yùn)動起來
    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>

五、總結(jié)

canvas基本上任何運(yùn)動,特效,都是通過js定時(shí)器改變坐標(biāo)的方式實(shí)現(xiàn)。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/dzhi1931/article/details/112362649

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 图片专区亚洲欧美另类 | 国产成人在线播放视频 | 国产免费一区二区 | 欧美一级欧美一级高清 | 性free非洲老妇 | 日本高清有码视频 | 人人澡 人人澡碰人人看软件 | 91精品国产91久久久久久 | caoporn超碰最新地址进入 | 为什么丈夫插我我却喜欢被打着插 | 日本免费的一级绿象 | 丝瓜视频黄色在线观看 | 91寡妇天天综合久久影院 | 日岳母小说 | 日本免费精品视频 | 俄罗斯freeⅹ性欧美 | 国产午夜精品福利久久 | 亚洲欧美优优色在线影院 | 歪歪视频在线播放无遮挡 | 欧美一级艳片视频免费观看 | 日本红色高清免费观看 | 亚洲va韩国va欧美va天堂 | 日本精工厂网址 | 国产日日操 | 美女扒开腿让男人桶爽免费gif | 青青青国产精品国产精品美女 | 亚洲va久久久久 | 国产成人亚洲精品一区二区在线看 | 视频在线观看入口一二三2021 | 激情综合色啪啪小说 | 国产免费久久精品44 | k逼| 羞羞漫画免费漫画页面在线看漫画秋蝉 | 日韩一品在线播放视频一品免费 | ck7788免费视频 | 污到你怀疑人生 | 亚洲国产在线视频中文字 | 日本在线你懂的 | 调教车文 | 91tv破解版不限次数 | 久久久高清国产999尤物 |