HTML使用canvas画布实现涟漪效果

js面向对象思想与canvas画布结合实现涟漪效果

先看效果图
在这里插入图片描述

相关代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        body{
            background-color: black;
        }
        #c1{
            background: white;
        }
        .wrap{
            width: 80vw;
            height: 80vh;
            text-align: center;
            margin: 20px auto;
        }
        button:hover,button:active{
            border: none;
            list-style: none;
        }
        button{
            border: none;
            width: 100px;
            height: 60px;
            border-radius: 12px;
            font-weight: bold;
            font-size: 20px;
            background-color: pink;
        }
        
    </style>
</head>
<body>
    <div class="wrap">
        <canvas id="c1" width="800" height="800"></canvas>
        <br>
        <button>开始</button>
        <button>结束</button>
    </div>

    <script>
        let oC = document.getElementById("c1");
        let oCG = oC.getContext("2d");
        let aBtn = document.querySelectorAll("button");
        class Rain{
            constructor(){
                this.x = Math.round(Math.random()*oC.offsetWidth);
                this.y = Math.round(Math.random()*oC.offsetHeight);
                this.opacity = 1;
                this.colors = [Math.round(Math.random()*255),Math.round(Math.random()*255),Math.round(Math.random()*255)];
                this.color = `rgba(${this.colors[0]},${this.colors[1]},${this.colors[2]},${this.opacity})`;
                this.r = 5;
            }
            draw(){
                oCG.beginPath();
                oCG.save();
                oCG.fillStyle = this.color;
                oCG.arc(this.x,this.y,this.r,0,360*Math.PI/180,false);
                oCG.fill();
                oCG.restore();
                this.opacity -= 1/40;
                this.color = `rgba(${this.colors[0]},${this.colors[1]},${this.colors[2]},${this.opacity})`;
                // console.log(this.color);
                this.r += 45/40;
            }
        }
        let arr = [];
        let timer = null;
        aBtn[0].onclick = function (){
            timer = setInterval(function (){
                let obj1 = new Rain();
                arr.push(obj1);
                oCG.clearRect(0,0,oC.offsetWidth,oC.offsetHeight);
                for(let i=0;i<arr.length;i++){
                    arr[i].draw();
                }
                arr = arr.filter((item,index)=>{
                    return item.opacity>0;
                })
            },30)
        }
        aBtn[1].onclick = function (){
            oCG.clearRect(0,0,oC.offsetWidth,oC.offsetHeight);
            arr = [];
            clearInterval(timer);
        }
    </script>
</body>
</html>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独立寒秋-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值