js实现雨滴特效

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>canvas雨滴特效</title>
</head>
<style>
    body{
        margin:0;
    }
    canvas{
        diaplay:block;
        background-color:#090909;
    }
</style>
<body>
<canvas class="rain">
</canvas>
<script>
    var oCanvas=document.querySelector(".rain");
    var aRain=[];
    var w=window.innerWidth;
    var h=window.innerHeight;
    oCanvas.width=w;
    oCanvas.height=h;
    window.onresize =function(){
        w=window.innerWidth;
        h=window.innerHeight;
        oCanvas.width=w;
        oCanvas.height=h;
    }();
    var canCon=oCanvas.getContext("2d");
 
    /*var y=0;
    */
    function random(min,max){
        return Math.random()*(max-min)+min;
    };
    function Rain(){};
    Rain.prototype={
        init:function(){
            this.x=random(0,w);
            this.y=0;
            this.w=2;
            this.h=10;
            this.color="#3ff";
            this.vy=random(2,3);
            this.height=random(0.8*h,0.9*h);
            this.r=2;
            this.maxR=random(80,130);
        },
        draw:function(){
            if(this.y<this.height){
                canCon.fillStyle= this.color;
                canCon.fillRect(this.x,this.y,this.w,this.h);
            }else{
                canCon.beginPath();
                canCon.strokeStyle=this.color;
                canCon.arc(this.x,this.y,this.r,0,Math.PI*2);
                canCon.stroke();
            }
        },
        move:function () {
            if(this.y<this.height){
                this.y+=this.vy;
            }else {
                if(this.r<this.maxR){
                    this.r++;
                }else {
                    this.init();
                }
            }
            this.draw();
        }
    }
 
    function createRain(num,time){
        for(var i=0;i<num;i++){
            setTimeout(function (){
                var rain=new Rain();
                rain.init();
                rain.draw();
                aRain.push(rain);
            },time*i);
        }
    }
    createRain(100,200);
    setInterval( function(){
        canCon.fillStyle="rgba(0,0,0,0.09)";
        canCon.fillRect(0,0,w,h);
       for(var item of aRain){
        item.move();
    }
    },1000/60);
</script>
</body>
</html>

效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值