本篇文章為大家展示了使用canvas怎么實(shí)現(xiàn)一個(gè)下雨效果的示例,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
成都創(chuàng)新互聯(lián)是專業(yè)的隰縣網(wǎng)站建設(shè)公司,隰縣接單;提供做網(wǎng)站、成都網(wǎng)站設(shè)計(jì),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行隰縣網(wǎng)站開發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <style> * { margin: 0; padding: 0; } </style> </head> <body> <canvas id="canvas" style="position: absolute; height: 100%; width:100%;"></canvas> <script> window.onload = main; function main() { // 獲取canvas元素 var canvasEl = document.getElementById('canvas'); var ctx = canvasEl.getContext('2d'); // canvas畫布的 背景顏色 var backgroundColor = '#000'; // canvas畫布的寬 等于 可視區(qū)域的寬 canvasEl.width = canvasEl.clientWidth; // canvas畫布的高 等于 可視區(qū)域的高 canvasEl.height = canvasEl.clientHeight; // 保存小水珠的數(shù)組 // 雨滴下落后散成小水珠,小水珠就是一些圓弧 var dropList = []; // 重力 // 雨滴下落后散成小水珠,小水珠會(huì)先上升后下降,主要是因?yàn)?nbsp;gravity 這個(gè)變量的緣故 var gravity = 0.5; // 保存雨滴的數(shù)組 // 每個(gè)雨滴 都是 畫的一條線 var linelist = []; // 保存鼠標(biāo)的坐標(biāo) // mousePos[0] 代表x軸的值,mousePos[1] 代表y軸的值 var mousePos = [0, 0]; // 跟隨鼠標(biāo), mouseDis 大小區(qū)域內(nèi)的雨滴會(huì)消失,形成散落效果 // 以mousePos為圓心,mouseDis為半徑,這個(gè)范圍內(nèi)的雨滴 都會(huì)散開,形成許多小水珠 var mouseDis = 35; // 更新一次動(dòng)畫,畫lineNum 條雨滴,lineNum 值越大,下雨就越密集 var lineNum = 3; // 跟隨鼠標(biāo)方向 變化下雨方向的 速度 // 鼠標(biāo)移動(dòng)后,下雨的方向 會(huì)慢慢改變,主要靠speedx 這個(gè)變量 var speedx = 0; // maxspeedx 為 speedx 可以取的最大值 // 當(dāng) speedx = maxspeedx時(shí),下雨方向 會(huì) 隨鼠標(biāo)移動(dòng)方向立即改變 var maxspeedx = 0; // 頁(yè)面大小發(fā)生變化時(shí),重置canvas畫布大小 window.onresize = function () { canvasEl.width = canvasEl.clientWidth; canvasEl.height = canvasEl.clientHeight; } //移動(dòng)鼠標(biāo)觸發(fā)事件 window.onmousemove = function (e) { // 設(shè)置mousePos 等于 鼠標(biāo)坐標(biāo) // e.clientX 為距離 瀏覽器窗口可視區(qū)域 左邊的距離 // e.clientY 為距離 瀏覽器窗口可視區(qū)域 上邊的距離 mousePos[0] = e.clientX; mousePos[1] = e.clientY; // 通過鼠標(biāo)位置,設(shè)置 maxspeedx的值,取值范圍是 -1 到 1 // maxspeedx的值,關(guān)系到 // 1、雨滴的方向 // 2、雨滴下落的方向 // 3、雨滴下落方向 跟隨 鼠標(biāo)移動(dòng)方向變化的速度 // 4、小水珠的移動(dòng)方向 // 值越接近1,表示方向越向右 // 值越接近-1,表示方向越向左 maxspeedx = (e.clientX - canvasEl.clientWidth / 2) / (canvasEl.clientWidth / 2); } // 根據(jù)參數(shù),返回一個(gè)rgb顏色,用于給雨滴設(shè)置顏色 function getRgb(r, g, b) { return "rgb(" + r + "," + g + "," + b + ")"; } // 畫 一滴雨(一條線) function createLine(e) { // 隨機(jī)生成 雨滴的長(zhǎng)度 var temp = 0.25 * (50 + Math.random() * 100); // 一個(gè) line 對(duì)象,代表一個(gè)雨滴 var line = { // 雨滴下落速度 speed: 5.5 * (Math.random() * 6 + 3), // 判斷是否刪除,值為true就刪除 die: false, // 雨滴x坐標(biāo) posx: e, // 雨滴y坐標(biāo) posy: -50, // 雨滴的長(zhǎng)度 h: temp, // 雨滴的顏色 color: getRgb(Math.floor(temp * 255 / 75), Math.floor(temp * 255 / 75), Math.floor(temp * 255 / 75)) }; // 把創(chuàng)建好的line(雨滴)對(duì)象,添加到保存雨滴的數(shù)組 linelist.push(line); } // 畫一個(gè)小水珠(雨滴散開后的小水珠就是一個(gè)個(gè)的圓弧) function createDrop(x, y) { // 一個(gè) drop 對(duì)象,代表一個(gè)圓弧 var drop = { // 判斷是否刪除,值為true就刪除 die: false, // 圓弧圓心的x坐標(biāo) posx: x, // 圓弧圓心的y坐標(biāo) posy: y, // vx 表示 x軸的值 變化的速度 vx: (Math.random() - 0.5) * 8, // vy 表示 y軸的值 變化的速度 取值范圍:-3 到 -9 vy: Math.random() * (-6) - 3, // 圓弧的半徑 radius: Math.random() * 1.5 + 1 }; return drop; } // 畫一定數(shù)量的小水珠 function madedrops(x, y) { // 隨機(jī)生成一個(gè)數(shù) maxi // maxi 代表要畫小水珠的數(shù)量 var maxi = Math.floor(Math.random() * 5 + 5); for (var i = 0; i < maxi; i++) { dropList.push(createDrop(x, y)); } } // 開始調(diào)用update函數(shù),更新動(dòng)畫 window.requestAnimationFrame(update); // 更新動(dòng)畫 function update() { // 如果保存小水珠的數(shù)組有內(nèi)容 if (dropList.length > 0) { // 遍歷保存小水珠的數(shù)組 dropList.forEach(function (e) { //設(shè)置e.vx,vx表示x坐標(biāo)變化的速度 // (speedx)/2 是為了,讓小水珠 在x軸的移動(dòng)距離短一點(diǎn),看上去更真實(shí)點(diǎn) // 也使 小水珠的移動(dòng)方向 和 雨滴方向,雨滴下落方向,鼠標(biāo)移動(dòng)方向相同 e.vx = e.vx + (speedx / 2); e.posx = e.posx + e.vx; //設(shè)置e.vy,vy表示y坐標(biāo)變化的速度 // e.vy的范圍是-3 到 -9,而這時(shí)e.posy(y坐標(biāo))一定是正值,所以 e.posy的值會(huì)先減小后增大 // 也就是實(shí)現(xiàn) 雨滴散成小水珠,小水珠會(huì)先上升后下降的效果 e.vy = e.vy + gravity; e.posy = e.posy + e.vy; // 如果 小水珠y坐標(biāo) 大于 可視區(qū)域的高度,設(shè)置die屬性為true // 小水珠如果超出可視區(qū)域就刪除掉 if (e.posy > canvasEl.clientHeight) { e.die = true; } }); } // 刪除 die屬性為ture 的數(shù)組成員 // 可視區(qū)域外的小水珠刪除掉 for (var i = dropList.length - 1; i >= 0; i--) { if (dropList[i].die) { dropList.splice(i, 1); } } // 設(shè)置下雨方向變換的速度,取值范圍: -1 到 1 // 當(dāng) speedx = maxspeedx時(shí),下雨方向 會(huì) 隨鼠標(biāo)移動(dòng)方向立即改變 speedx = speedx + (maxspeedx - speedx) / 50; // 根據(jù)lineNum的值,畫一定數(shù)量雨滴 for (var i = 0; i < lineNum; i++) { // 調(diào)用createLine 函數(shù),參數(shù)是雨滴x坐標(biāo) createLine(Math.random() * 2 * canvasEl.width - (0.5 * canvasEl.width)); } // 設(shè)置結(jié)束線,也就是雨滴散開 形成許多小水珠的位置 var endLine = canvasEl.clientHeight - Math.random() * canvasEl.clientHeight / 5; // 遍歷保存雨滴的數(shù)組 linelist.forEach(function (e) { // 利用勾股定理 確定一個(gè)范圍,在這個(gè)范圍內(nèi)雨滴會(huì)散開形成小水珠 // e.posx + speedx * e.h 是雨滴x坐標(biāo) // e.posy + e.h 是雨滴y坐標(biāo) var dis = Math.sqrt(((e.posx + speedx * e.h) - mousePos[0]) * ((e.posx + speedx * e.h) - mousePos[0]) + (e.posy + e.h - mousePos[1]) * (e.posy + e.h - mousePos[1])); // 如果在mouseDis區(qū)域內(nèi),就刪除雨滴,畫一些小水珠(圓?。? // 實(shí)現(xiàn)鼠標(biāo)碰到雨滴,雨滴散成小水珠的效果 if (dis < mouseDis) { // 刪除 雨滴 e.die = true; // 畫一些小水珠(圓?。? madedrops(e.posx + speedx * e.h, e.posy + e.h); } // 如果雨滴超過 結(jié)束線,刪除雨滴,畫一些小水珠(圓?。? if ((e.posy + e.h) > endLine) { e.die = true; madedrops(e.posx + speedx * e.h, e.posy + e.h); } // 如果 雨滴 y坐標(biāo) 大于 可視區(qū)域的高度,設(shè)置die屬性為true // 如果 雨滴 超出可視區(qū)域就刪除掉 if (e.posy >= canvasEl.clientHeight) { e.die = true; } else { // 逐漸增加 雨滴 y坐標(biāo)的值 e.posy = e.posy + e.speed; // 變化雨滴 x坐標(biāo) // * speedx 用來(lái)控制雨滴 下落 方向 // 使 雨滴下落方向 和 鼠標(biāo)移動(dòng)方向相同 e.posx = e.posx + e.speed * speedx; } }); // 刪除 die屬性為ture 的數(shù)組成員 // 鼠標(biāo)區(qū)域內(nèi)的,超過結(jié)束線的,可視區(qū)域外的雨滴刪除掉 for (var i = linelist.length - 1; i >= 0; i--) { if (linelist[i].die) { linelist.splice(i, 1); } } // 渲染 render(); // 遞歸調(diào)用 update,實(shí)現(xiàn)動(dòng)畫效果 window.requestAnimationFrame(update); } // 渲染 function render() { // 畫一個(gè)和可視區(qū)域一樣大的矩形 ctx.fillStyle = backgroundColor; ctx.fillRect(0, 0, canvasEl.width, canvasEl.height); // 畫雨滴效果 ctx.lineWidth = 5; linelist.forEach(function (line) { ctx.strokeStyle = line.color; ctx.beginPath(); ctx.moveTo(line.posx, line.posy); // * speedx 用來(lái)控制雨滴方向 // 使 雨滴方向 和 鼠標(biāo)移動(dòng)方向相同 ctx.lineTo(line.posx + line.h * speedx, line.posy + line.h); ctx.stroke(); }); // 畫雨滴散開形成小水珠效果 ctx.lineWidth = 1; ctx.strokeStyle = "#fff"; dropList.forEach(function (e) { ctx.beginPath(); ctx.arc(e.posx, e.posy, e.radius, Math.random() * Math.PI * 2, 1 * Math.PI); ctx.stroke(); }); // 解開注釋,可看見鼠標(biāo)范圍 /* ctx.beginPath(); ctx.arc(mousePos[0], mousePos[1], mouseDis, 0, 2 * Math.PI); ctx.stroke(); */ } } </script> </body> </html>
上述內(nèi)容就是使用canvas怎么實(shí)現(xiàn)一個(gè)下雨效果的示例,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享文章:使用canvas怎么實(shí)現(xiàn)一個(gè)下雨效果的示例
文章來(lái)源:http://jinyejixie.com/article44/iiceee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、服務(wù)器托管、品牌網(wǎng)站設(shè)計(jì)、做網(wǎng)站、軟件開發(fā)、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)