基于JS實(shí)現(xiàn)漫畫中大雨滂沱的效果
演示

技術(shù)棧
最近用canvas有點(diǎn)上癮,接著上次的雨滴和流星雨的思路我們?cè)诟倪M(jìn)一下
<canvas id='canvas' width='1280' height='720'></canvas>
源碼
css部分
body {
margin:0;
padding:0;
background: #000;
overflow:hidden;
}
canvas {
background:url('../img/maxresdefault.jpg');
background-size: cover;
background-repeat: no-repeat;
background-position: center center;
}background-size屬性指定背景圖片大小。
默認(rèn)值: auto 繼承: no 版本: CSS3 JavaScript 語(yǔ)法: object object.style.backgroundSize="60px 80px"
語(yǔ)法
background-size: length|percentage|cover|contain;
length 設(shè)置背景圖片高度和寬度。第一個(gè)值設(shè)置寬度,第二個(gè)值設(shè)置的高度。如果只給出一個(gè)值,第二個(gè)是設(shè)置為 auto(自動(dòng))
percentage 將計(jì)算相對(duì)于背景定位區(qū)域的百分比。第一個(gè)值設(shè)置寬度,第二個(gè)值設(shè)置的高度。如果只給出一個(gè)值,第二個(gè)是設(shè)置為"auto(自動(dòng))"
cover 此時(shí)會(huì)保持圖像的縱橫比并將圖像縮放成將完全覆蓋背景定位區(qū)域的最小大小。
contain 此時(shí)會(huì)保持圖像的縱橫比并將圖像縮放成將適合背景定位區(qū)域的最大大小。
大雨滂沱
function _pexresize() {
var cw = window.innerWidth;
var ch = window.innerHeight;
if (cw<=ch*stgw/stgh) {
loffset = 0;
toffset = Math.floor(ch-(cw*stgh/stgw))/2;
canvas.style.width = cw + "px";
canvas.style.height = Math.floor(cw*stgh/stgw) + "px";
} else {
loffset = Math.floor(cw-(ch*stgw/stgh))/2;
toffset = 0;
canvas.style.height = ch + "px";
canvas.style.width = Math.floor(ch*stgw/stgh) + "px";
}
canvas.style.marginLeft = loffset +"px";
canvas.style.marginTop = toffset +"px";
}隨機(jī)雨點(diǎn)和持續(xù)降雨
for (var l=0;l<lcount;l++) {
ctx.clearRect(0,0,stgw,stgh);
for (var i=0;i<count*(lcount-l)/1.5;i++) {
var myx = Math.floor(Math.random()*stgw);
var myy = Math.floor(Math.random()*stgh);
var myh = l*6+8;
var myw = myh/10;
ctx.beginPath();
ctx.moveTo(myx,myy);
ctx.lineTo(myx+myw,myy+myh);
ctx.arc(myx, myy+myh, myw, 0, 1 * Math.PI);
ctx.lineTo(myx-myw,myy+myh);
ctx.closePath();
ctx.fill();
}
layer[l] = new Image();
layer[l].src = canvas.toDataURL("image/png");
layery[l] = 0;
}
var stt = 0;
var str = Date.now()+Math.random()*4000;
var stact = false;
function animate() {
ctx.clearRect(0,0,stgw,stgh);
for (var l=0;l<lcount;l++) {
layery[l] += (l+1.5)*5;
if (layery[l]>stgh) {
layery[l] =layery[l]-stgh;
}
ctx.drawImage(layer[l],0,layery[l]);
ctx.drawImage(layer[l],0,layery[l]-stgh);
}
if (Date.now()>str) {
stact = true;
}
if (stact) {
stt++;
if (stt<5+Math.random()*10) {
var ex = stt/30;
} else {
var ex = (stt-10)/30;
}
if (stt>20) {
stt = 0;
stact = false;
str = Date.now()+Math.random()*8000+2000;
}
ctx.fillStyle = "rgba(255,255,255,"+ex+")";
ctx.fillRect(0,0,stgw,stgh);
}
window.requestAnimationFrame(animate);
}到此這篇關(guān)于基于JS實(shí)現(xiàn)漫畫中大雨滂沱的效果的文章就介紹到這了,更多相關(guān)JS大雨滂沱效果內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- js+css實(shí)現(xiàn)紅包雨效果
- HTML+JS實(shí)現(xiàn)“代碼雨”效果源碼(黑客帝國(guó)文字下落效果)
- canvas+gif.js打造自己的數(shù)字雨頭像的示例代碼
- JS+CSS+HTML實(shí)現(xiàn)“代碼雨”類似黑客帝國(guó)文字下落效果
- JS實(shí)現(xiàn)代碼雨特效
- JavaScript canvas實(shí)現(xiàn)代碼雨效果
- js+canvas實(shí)現(xiàn)代碼雨效果
- JavaScript實(shí)現(xiàn)代碼雨效果
- 利用JS打造黑客代碼雨效果
- JavaScript實(shí)現(xiàn)字符雨效果
相關(guān)文章
JS面試題---關(guān)于算法臺(tái)階的問(wèn)題
下面小編就為大家?guī)?lái)一篇JS面試題---關(guān)于算法臺(tái)階的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07
layui 阻止圖片上傳的實(shí)例(before方法)
今天小編就為大家分享一篇layui 阻止圖片上傳的實(shí)例(before方法),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
Echarts折線圖如何根據(jù)容器寬度自適應(yīng)展示
通過(guò)js來(lái)制作復(fù)選框的全選和不選效果
js實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的五種方法推薦

