基于JavaScript實(shí)現(xiàn)動(dòng)態(tài)雨滴特效
演示
技術(shù)棧
介紹一下畫(huà)布吧:
HTML5 標(biāo)簽用于繪制圖像(通過(guò)腳本,通常是 JavaScript)。
不過(guò), 元素本身并沒(méi)有繪制能力(它僅僅是圖形的容器) - 您必須使用腳本來(lái)完成實(shí)際的繪圖任務(wù)。
getContext() 方法可返回一個(gè)對(duì)象,該對(duì)象提供了用于在畫(huà)布上繪圖的方法和屬性
它的方法挺多的大家可以去搜一下我就說(shuō)幾個(gè)常用的:
源碼
設(shè)置畫(huà)布
<canvas id="canvas" style="position: absolute; height: 100%; width:100%;"></canvas>
js部分
跟隨鼠標(biāo)移動(dòng)
window.onmousemove=function (e) { mousePos[0]=e.clientX; mousePos[1]=e.clientY; maxspeedx=(e.clientX-canvasEl.clientWidth/2)/(canvasEl.clientWidth/2); }
創(chuàng)建雨線(xiàn)
function createLine(e) { var temp= 0.25*( 50+Math.random()*100); var myline={ speed:5.5*(Math.random()*6+3), die:false, posx:e, posy:-200, h:temp, color:getRgb(Math.floor(temp*255/75),Math.floor(temp*255/75),Math.floor(temp*255/75)) }; linelist.push(myline); }
雨點(diǎn)的刷新
function update() { if(dropList.length>0) { dropList.forEach(function (e) { e.vx=e.vx+(speedx)/2; e.posx=e.posx+e.vx; e.vy=e.vy+gravity; e.posy=e.posy+e.vy; if(e.posy>canvasEl.clientHeight) { e.die=true; } }); } for(var i=dropList.length-1;i>=0;i--) { //delite die if(dropList[i].die){ dropList.splice(i,1); } } speedx=speedx+(maxspeedx-speedx)/50; if(Math.random()>0) { createLine(Math.random()*2*canvasEl.width-(0.5*canvasEl.width)); createLine(Math.random()*2*canvasEl.width-(0.5*canvasEl.width)); createLine(Math.random()*2*canvasEl.width-(0.5*canvasEl.width)); } var mydeadline=canvasEl.clientHeight- Math.random()*canvasEl.clientHeight/5; linelist.forEach(function (e) { 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])); if(dis<35) { madedrops(e.posx+speedx*e.h,e.posy+e.h); e.die=true; } if((e.posy+e.h)>mydeadline) { if(Math.random()>0.85) { madedrops(e.posx+speedx*e.h,e.posy+e.h); e.die=true; } } if(e.posy>=canvasEl.clientHeight) { e.die=true; }else{ e.posy=e.posy+e.speed; e.posx=e.posx+(e.speed*speedx); } }); for(var i=linelist.length-1;i>=0;i--) { if(linelist[i].die){ linelist.splice(i,1); } } render(); window.requestAnimationFrame(update); }
雨點(diǎn)的渲染
function render() { ctx.fillStyle = backgroundColor; ctx.fillRect(0, 0, canvasEl.width, canvasEl.height); linelist.forEach( function (line) { ctx.strokeStyle =line.color; ctx.lineWidth=4.5; ctx.beginPath(); ctx.moveTo(line.posx,line.posy); ctx.lineTo(line.posx+speedx*line.h,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(); }); }
以上就是基于JavaScript實(shí)現(xiàn)動(dòng)態(tài)雨滴特效的詳細(xì)內(nèi)容,更多關(guān)于JavaScript動(dòng)態(tài)雨滴特效的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
完美實(shí)現(xiàn)js選項(xiàng)卡切換效果(二)
這篇文章主要為大家詳細(xì)介紹如何完美實(shí)現(xiàn)js選項(xiàng)卡切換效果,通過(guò)設(shè)置定時(shí)器實(shí)現(xiàn)延時(shí)0.5s切換選項(xiàng)卡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03JS實(shí)現(xiàn)定時(shí)自動(dòng)關(guān)閉DIV層提示框的方法
這篇文章主要介紹了JS實(shí)現(xiàn)定時(shí)自動(dòng)關(guān)閉DIV層提示框的方法,可實(shí)現(xiàn)加載時(shí)載入js代碼控制div層提示框自動(dòng)關(guān)閉的效果,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-05-05js實(shí)現(xiàn)可鍵盤(pán)控制的簡(jiǎn)單抽獎(jiǎng)程序
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)可鍵盤(pán)控制的簡(jiǎn)單抽獎(jiǎng)程序,代碼簡(jiǎn)潔,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07在IE下獲取object(ActiveX)的Param的代碼
在IE下,獲取Param的時(shí)候有個(gè)詭異現(xiàn)象(不知道算不算bug)。2009-09-09iframe窗口高度自適應(yīng)的又一個(gè)巧妙實(shí)現(xiàn)思路
這篇文章主要介紹了實(shí)現(xiàn)iframe窗口高度自適應(yīng)的又一個(gè)巧妙思路,需要的朋友可以參考下2014-04-04bootstrap3使用bootstrap datetimepicker日期插件
這篇文章主要為大家詳細(xì)介紹了bootstrap3中使用bootstrap datetimepicker日期插件的用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05微信小程序獲取手機(jī)系統(tǒng)信息的方法【附源碼下載】
這篇文章主要介紹了微信小程序獲取手機(jī)系統(tǒng)信息的方法,涉及微信小程序wx.getSystemInfo函數(shù)的簡(jiǎn)單使用技巧,并附帶源碼供讀者下載參考,需要的朋友可以參考下2017-12-12