❤ ❤WEB前端--canvas绚丽烟花特效❤ ❤

一、实现效果

在这里插入图片描述

二、代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    
    <style>
        *{
            padding:0;
            margin:0;
        }
        
        body{
            background-color: #000;
        }
    </style>
</head>
<body>
    <canvas id="can" width="100%" height="1000"></canvas>
    <!-- <div id="box" style="left: 0;"></div> -->
    <script>
        var can=document.getElementById("can");
        var c=can.getContext("2d");
        var w=can.width=window.innerWidth;
        var h=can.Height=window.innerHeight;
        c.beginPath();
        c.fillStyle = "red";
        c.fillRect(0,0,w,h);
        c.closePath();
        window.onresize=function(){
            w=can.width=window.innerWidth;
            h=can.Height=window.innerHeight;
            c.beginPath();
            c.fillStyle="black";
            c.fillRect(0,0,w,h);
            c.closePath();
        }
        function Fireworks(){}
        Fireworks.prototype={
            init:function(){
                this.x=ranNum(0,w);
                this.y=h-30;
                this.ys=4;
                this.r=1;
                this.rs=2;
                this.a=1;
                this.as=0.02;
                this.l=ranNum(0.1*h,0.3*h);
                this.color="rgba("+ranNum(0,255)+","+ranNum(0,255)+","+ranNum(0,255)+","+this.a+")";
            },
            draw:function(){
                if(this.y>this.l){
                    c.fillStyle=this.color;
                    c.fillRect(this.x,this.y,2,10);
                }else{
                    c.beginPath();
                    c.arc(this.x,this.y,this.r,0,Math.PI*2,true);
                    c.strokeStyle=this.color;
                    c.stroke();
                    c.closePath();
                }
                
                
                this.update();
            },
            update:function(){
                if(this.y>this.l){
                    this.y-=this.ys;
                }else{
                    if(this.r<100){
                        this.r+=this.rs;
                        this.a*=this.as;
                    }else{
                        this.init();
                    }
                }
                // this.y-=this.ys;
            }
        }
        // 存对象
        var fire=[];
        // 实例化对象
        setTimeout(function(){
            for(var i=0;i<30;i++){
            
                var firework=new Fireworks;
                firework.init();
                fire.push(firework);
           
           
        }
        },5000);
       
        // 出现以及移动,调用画方法
        function move(){
            c.beginPath();
            c.fillStyle="rgba(0,0,0,0.1)";
            c.fillRect(0,0,w,h);
            c.beginPath();
            c.closePath();
            for(var i=0;i<fire.length;i++){
                fire[i].draw();
            }
            requestAnimationFrame(move);
        }
        move();
        function ranNum(min,max){
            return Math.random()*(max-min)+min;

        }
        function Circle(){

        }
        
    
    </script>
</body>
</html>
  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值