canvas简单的烟花效果

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>ball</title>
    <style>
        body,html{
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
        }
        #canvas { border: 1px solid #aaaaaa; display: block; margin: 50px auto; background: #000;}
    </style>
</head>
<body>
<div id="canvas-warp">
    <canvas id="canvas" width="" height="">
        你的浏览器居然不支持Canvas?!赶快换一个吧!!
    </canvas>
</div>

<script>
    var docW = document.body.clientWidth;
    var dovH = document.body.clientHeight;
    var canvas = document.getElementById('canvas');
    canvas.width=docW;
    canvas.height=dovH;
    var context = canvas.getContext('2d');
    var cw = docW;
    var ch = dovH;
    var arrBig = [];
    var arrSmall=[];
    var fn = {
        getX:function(){
            return 100+Math.floor(Math.random()*(cw-200));
        },
        getSpeed:function(){
            return Math.floor(Math.random()*5+10);
        },
        getClolr:function(){
            var r = Math.floor(Math.random()*255);
            var g = Math.floor(Math.random()*255);
            var b = Math.floor(Math.random()*255);
            var c = "rgba("+r+","+g+","+b+",1)";
            var dx = Math.floor(Math.random()*10)-5;
            var dy = Math.floor(Math.random()*10)-5;
            return {
                dx,dy,c
            }
        } 
    }
    setInterval(() => {
        context.clearRect(0, 0, docW, dovH);
        if(arrBig.length<30){
            arrBig.push(new Big(fn.getX(),ch-10,cw,ch,fn.getSpeed()))
        }
        for(var i=0;i<arrBig.length;i++){
            arrBig[i].update();
            if(arrBig[i]){
                arrBig[i].draw();
            }
        }
        for(var i=0;i<arrSmall.length;i++){
            arrSmall[i].update();
            if(arrSmall[i]){
                arrSmall[i].draw();
            }
        }
    }, 30);
    function Big(x,y,w,h,s){
        this.w = w;
        this.h = h;
        this.x = x;
        this.y = y;
        this.s= s;
        arrBig.push(this);
    }
    Big.prototype={
        init:function(){

        },
        draw:function(){
            context.fillStyle='#fff';
            context.fillRect(this.x,this.y,2,5);
        },
        update:function(){
            // var s = 250-this.s*30
            if(this.y<150){
                this.remove();
                for(var i=0;i<30;i++){
                    new Small(this.x,this.y,fn.getClolr().c,fn.getClolr().dx,fn.getClolr().dy).draw();
                }
            }else{
                this.y-=this.s;
            }
        },
        remove:function(){
            for(var i=0;i<arrBig.length;i++){
                if(arrBig[i] == this){
                    arrBig.splice(i,1);
                }
            }
        }
    }
    for(var i=0;i<30;i++){
        new Big(fn.getX(),ch-10,cw,ch,fn.getSpeed())
    }

    //烟花
    function Small(x,y,c,dx,dy){
        this.x=x;
        this.y=y;
        this.c=c;
        this.dx = dx;
        this.dy = dy;
        arrSmall.push(this);
    }

    Small.prototype={
        draw:function(){
            context.beginPath();
            context.arc(this.x,this.y,2,Math.PI*2,false);
            context.fillStyle=this.c;
            context.fill();
        },
        update:function(){
            this.x+=this.dx;
            this.y+=this.dy;
            if(Math.abs(this.dy)>10 || Math.abs(this.dx)>8){
                this.remove();
            }else{
                this.dy+=0.2;
            }
        },
        remove:function(){
            for(var i=0;i<arrSmall.length;i++){
                if(arrSmall[i] == this){
                    arrSmall.splice(i,1);
                }
            }
        }
    }
    // 清空
    

    // 更新


    // 渲染


</script>
</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用HTML5的Canvas标签来实现烟花效果。以下是一个简单的实现,你可以根据自己的需求进行调整: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>烟花效果</title> <style type="text/css"> canvas { background-color: #000; display: block; margin: 0 auto; } </style> </head> <body> <canvas id="fireworks" width="800" height="600"></canvas> <script type="text/javascript"> var canvas = document.getElementById('fireworks'); var ctx = canvas.getContext('2d'); var fireworks = []; function Firework() { this.init = function() { this.x = Math.random() * canvas.width; this.y = canvas.height; this.color = '#' + Math.floor(Math.random() * 16777215).toString(16); this.radius = 2; this.velocity = { x: Math.random() * 6 - 3, y: Math.random() * 3 + 1 }; this.gravity = 0.05; this.opacity = 1; this.time = 0; this.maxTime = Math.random() * 50 + 100; }; this.draw = function() { ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2); ctx.fillStyle = this.color; ctx.globalAlpha = this.opacity; ctx.fill(); }; this.explode = function() { for (var i = 0; i < 50; i++) { var particle = new Particle(); particle.x = this.x; particle.y = this.y; particle.color = this.color; var angle = Math.random() * Math.PI * 2; var speed = Math.random() * 6; particle.velocity.x = Math.cos(angle) * speed; particle.velocity.y = Math.sin(angle) * speed; fireworks.push(particle); } }; this.update = function() { this.draw(); this.x += this.velocity.x; this.y -= this.velocity.y; this.velocity.y -= this.gravity; this.opacity -= 0.01; this.time ++; if (this.time >= this.maxTime) { this.explode(); fireworks.splice(fireworks.indexOf(this), 1); } }; } function Particle() { this.x = 0; this.y = 0; this.color = '#FFF'; this.radius = 2; this.velocity = { x: 0, y: 0 }; this.gravity = 0.05; this.opacity = 1; this.draw = function() { ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2); ctx.fillStyle = this.color; ctx.globalAlpha = this.opacity; ctx.fill(); }; this.update = function() { this.draw(); this.x += this.velocity.x; this.y += this.velocity.y; this.velocity.y += this.gravity; this.opacity -= 0.03; }; } function animate() { requestAnimationFrame(animate); ctx.clearRect(0, 0, canvas.width, canvas.height); if (Math.random() < 0.03) { var firework = new Firework(); firework.init(); fireworks.push(firework); } for (var i = 0; i < fireworks.length; i++) { fireworks[i].update(); } for (var i = 0; i < fireworks.length; i++) { if (fireworks[i].opacity <= 0) { fireworks.splice(i, 1); } } } animate(); </script> </body> </html> ``` 这个例子中,我们首先创建了一个Canvas元素,并且定义了一些CSS样式来设置它的背景色和居中。我们使用了JavaScript来实现烟花效果,通过创建Firework和Particle类来表示烟花烟花粒子,然后使用Canvas上下文中的方法来绘制它们。 在animate函数中,我们使用requestAnimationFrame方法来循环调用该函数,不断更新Canvas中的状态。每隔一定的时间间隔,我们创建一个新的烟花对象并将其加入到fireworks数组中。然后,我们遍历该数组,更新每个烟花烟花粒子的状态,并将它们绘制到Canvas中。最后,我们从数组中删除已经不可见的烟花烟花粒子,以免占用过多的内存。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值