烟花源代码

【跨年烟花代码】用C语言来写“烟花”,快拿去给心中的那个人看 嗨!这里是狐狸~~ 我之前有发过一篇有关程序员相亲的文章,今天突然翻起,又陷入了沉思,现在不会大家都觉得程序员不懂浪漫吧,真的大错特错,今天就让你们看看什么是程序员的浪漫! 我们今天就来写写《烟花》表白程序,不要惊讶,不要激动,学会了快去拿给心中的那个人看!!! 煽情的话就不要我说了吧,直接上代码 首先当然是我们的老朋友结构体,其中艺术字结构体不用我多说,该写什么内容自己应该知道哈 // 烟花结构 struct FIRE { int r; // 当前爆炸半径 int m. 阅读详情
AI摘要

本文介绍了一个使用HTML5 Canvas实现的烟花动画效果。通过自定义Firework和Particle类来模拟烟花上升和爆炸的过程,配合定时器控制烟花发射频率,实现了流畅且视觉效果丰富的烟花场景。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Canvas烟花</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
<style type="text/css">
    body{
        padding: 0;
        margin: 0;
        background: #000;
    }
</style>
</head>
<body>
    <canvas id="canvas">Canvas is not supported in your browser.</canvas>
    <script type="text/javascript">
        window.requestAnimationFrame = (function(){
            return window.requestAnimationFrame ||
                   window.webkitRequestAnimationFrame ||
                   window.mozRequestAnimationFrame ||
                   function(callback){
                        window.setTimeout(callback,1000/60);
                   }
        })();

        var canvas = document.getElementById("canvas"),
            ctx = canvas.getContext("2d"),
            cw = window.innerWidth,
            ch = window.innerHeight,
            fireworks = [], 
            particles = [], 
            hue = 120, 
            timerTotal = 80, 
            timerTick = 0;

        canvas.width = cw;
        canvas.height = ch;


        function random(min,max){
            return Math.random()*(max-min)+min;
        }

        function caculateDistance(sx,sy,tx,ty){
            var xDistance = sx-tx,
                yDistance = sy-ty;
            return Math.sqrt(Math.pow(xDistance,2)+Math.pow(yDistance,2));
        }

        function Firework(sx,sy,tx,ty){
            this.x = sx; 
            this.y = sy; 
            this.sx = sx; 
            this.sy = sy; 
            this.tx= tx; 
            this.ty = ty; 
            this.distanceToTarget = caculateDistance(sx,sy,tx,ty); 
            this.distanceTraveled = 0; 
            this.coordinates = []; 
            this.coordinatesCount = 3;
            while(this.coordinatesCount--){
                this.coordinates.push([this.x,this.y]);
            }
            this.angle = Math.atan2(ty-sy,tx-sx); 
            this.speed = 2; 
            this.acceleration = 1.05 
            this.brightness = random(50,70); 
            this.targetRadius = 1; 
        }

        Firework.prototype.update = function(index){
            this.coordinates.pop(); 
            this.coordinates.unshift([this.x,this.y]);

            if(this.targetRadius<8) {
                this.targetRadius += 0.3;
            } else {
                this.targetRadius = 1;
            }
            this.speed *= this.acceleration; 
            var vx = Math.cos(this.angle)*this.speed,
                vy = Math.sin(this.angle)*this.speed; 
            this.distanceTraveled = caculateDistance(this.sx,this.sy,this.x+vx,this.y+vy); 
            if(this.distanceTraveled>=this.distanceToTarget){
                createParticles(this.tx,this.ty);
                fireworks.splice(index,1);
            }else{
                this.x += vx;
                this.y += vy;
            }
        }

        Firework.prototype.draw = function(){

            ctx.beginPath();
            ctx.moveTo(this.coordinates[this.coordinates.length-1][0],this.coordinates[this.coordinates.length-1][1]);
            ctx.lineTo(this.x,this.y);
            ctx.strokeStyle = 'hsl('+hue+',100%,'+this.brightness+'%)';
            ctx.stroke();

            ctx.beginPath();
            ctx.arc(this.tx,this.ty,this.targetRadius,0,Math.PI*2);
            ctx.stroke();
        }

        function Particle(x,y){
            this.x = x;
            this.y = y;
            this.coordinates = [];
            this.coordinatesCount = 5;
            while(this.coordinatesCount--){
                this.coordinates.push([this.x,this.y]);
            }
            this.angle = random(0,2*Math.PI); 
            this.speed = random(1,10); 
            this.friction = 0.95 
            this.gravity = 1; 
            this.hue = random(hue-20,hue+20); 
            this.brightness = random(50,80); 
            this.alpha = 1; 
            this.decay = random(0.015,0.03); 
        }

        Particle.prototype.update = function(index){
            this.coordinates.pop(); 
            this.coordinates.unshift([this.x,this.y]);
            this.speed *= this.friction;
            this.x += Math.cos(this.angle) * this.speed;
            this.y += Math.sin(this.angle) * this.speed + this.gravity;
            this.alpha -= this.decay;
            if(this.alpha<=this.decay){
                particles.splice(index,1);
            }
        }

        Particle.prototype.draw = function(){
            ctx.beginPath();
            ctx.moveTo(this.coordinates[this.coordinates.length-1][0],this.coordinates[this.coordinates.length-1][1]);
            ctx.lineTo(this.x,this.y);
            ctx.strokeStyle = 'hsla('+this.hue+',100%,'+this.brightness+'%,'+this.alpha+')';
            ctx.stroke();
        }

        function createParticles(x,y){
            var particleCount = 30;
            while(particleCount--) {
                particles.push(new Particle(x,y));
            }
        }

        function loop(){
            requestAnimationFrame(loop);
            hue += 0.5;
            ctx.globalCompositeOperation = 'destination-out';
            ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
            ctx.fillRect( 0, 0, cw, ch );
            ctx.globalCompositeOperation = 'lighter';
            var i = fireworks.length;
            while(i--) {
                fireworks[i].draw();
                fireworks[i].update(i);
            }
            var i = particles.length;
            while(i--) {
                particles[i].draw();
                particles[i].update(i);
            }
            if(timerTick >= timerTotal) {
                fireworks.push(new Firework(cw/2,ch,random(0,cw),random(0,ch/2)));
                timerTick = 0;
            } else {
                timerTick++;
            }
        }
        window.onload = loop;
    </script>
</body>
</html>

这个效果之前是在runjs上看到的,看了一段时间才看懂,根据自己的理解,较少了一些效果。
window.requestAnimationFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function(callback){
window.setTimeout(callback,1000/60);//每秒60帧
}
})();

var canvas = document.getElementById(“canvas”),
ctx = canvas.getContext(“2d”),
cw = window.innerWidth,
ch = window.innerHeight,
fireworks = [], //烟花数组
particles = [], //烟花爆炸屑数字
hue = 120, //初始色调 0/360 红色 120 绿色 240 蓝色
timerTotal = 80, //每隔80下释放一次烟花
timerTick = 0;
//设置canvas画布的宽高
canvas.width = cw;
canvas.height = ch;

//生成min到max之间的随机数
function random(min,max){
return Math.random()*(max-min)+min;
}

//计算点(sx,sy)到点(tx,ty)之间的距离
function caculateDistance(sx,sy,tx,ty){
var xDistance = sx-tx,
yDistance = sy-ty;
return Math.sqrt(Math.pow(xDistance,2)+Math.pow(yDistance,2));
}

//烟花对象 (sx,sy)初始位置 (tx,ty)目标位置
function Firework(sx,sy,tx,ty){
this.x = sx; //实时运行坐标x
this.y = sy; //实时运行坐标y
this.sx = sx; //初始位置坐标x
this.sy = sy; //初始位置坐标Y
this.tx= tx; //目标位置坐标x
this.ty = ty; //目标位置坐标Y
this.distanceToTarget = caculateDistance(sx,sy,tx,ty); //计算初始位置到目标位置之间的距离
this.distanceTraveled = 0; //已运行距离
this.coordinates = []; //我理解是一个辅助变量 用于生成实时运动轨迹
this.coordinatesCount = 3;
while(this.coordinatesCount–){
this.coordinates.push([this.x,this.y]);
}
this.angle = Math.atan2(ty-sy,tx-sx); //初始位置和目标位置之间的角度
this.speed = 2; //初始速度
this.acceleration = 1.05 //加速度
this.brightness = random(50,70); //明度
this.targetRadius = 1; //目标位置标示圆圈的初始半径
}

//更新烟花的位置
Firework.prototype.update = function(index){
this.coordinates.pop();
this.coordinates.unshift([this.x,this.y]);
//上面是一个技巧吧 先将数组最后一个移除,然后将当前烟花位置插入到第一个,那数组最后一个元素和更新之后的坐标之间就形成了一条轨迹
//让目标标示处圆圈动起来
if(this.targetRadius<8) {
this.targetRadius += 0.3;
} else {
this.targetRadius = 1;
}
this.speed *= this.acceleration; //根据加速度变换速度
var vx = Math.cos(this.angle)*this.speed,//计算水瓶方向速度
vy = Math.sin(this.angle)*this.speed; //计算垂直方向速度
this.distanceTraveled = caculateDistance(this.sx,this.sy,this.x+vx,this.y+vy); //重新计算烟花已运行距离
//如果烟花运行距离大于或等于初始位置到目标位置之间的距离,生成新烟花并移除当前烟花,否则更新烟花位置
if(this.distanceTraveled>=this.distanceToTarget){
createParticles(this.tx,this.ty);
fireworks.splice(index,1);
}else{
this.x += vx;
this.y += vy;
}
}

Firework.prototype.draw = function(){
//画出烟花运行轨迹
ctx.beginPath();
ctx.moveTo(this.coordinates[this.coordinates.length-1][0],this.coordinates[this.coordinates.length-1][1]);
ctx.lineTo(this.x,this.y);
ctx.strokeStyle = ‘hsl(‘+hue+’,100%,’+this.brightness+’%)’;
ctx.stroke();

    //画出目标位置标示小圆圈
    ctx.beginPath();
    ctx.arc(this.tx,this.ty,this.targetRadius,0,Math.PI*2);
    ctx.stroke();

}

//烟花爆炸屑对象
function Particle(x,y){
this.x = x;
this.y = y;
this.coordinates = [];
this.coordinatesCount = 5;
while(this.coordinatesCount–){
this.coordinates.push([this.x,this.y]);
}
this.angle = random(0,2*Math.PI); //生成任意方向的碎屑
this.speed = random(1,10); //随机速度
this.friction = 0.95 //摩擦力
this.gravity = 1; //重力
this.hue = random(hue-20,hue+20); //生成与烟花色彩相近的碎屑
this.brightness = random(50,80); //随机明度
this.alpha = 1; //初始透明度
this.decay = random(0.015,0.03); //碎屑消失时间
}

Particle.prototype.update = function(index){
this.coordinates.pop();
this.coordinates.unshift([this.x,this.y]);
//上面是一个技巧吧 先将数组最后一个移除,然后将当前烟花位置插入到第一个,那数组最后一个元素和更新之后的坐标之间就形成了一条轨迹
this.speed *= this.friction;
this.x += Math.cos(this.angle) * this.speed;
this.y += Math.sin(this.angle) * this.speed + this.gravity;
this.alpha -= this.decay;
if(this.alpha<=this.decay){
particles.splice(index,1);
}
}

Particle.prototype.draw = function(){
ctx.beginPath();
ctx.moveTo(this.coordinates[this.coordinates.length-1][0],this.coordinates[this.coordinates.length-1][1]);
ctx.lineTo(this.x,this.y);
ctx.strokeStyle = ‘hsla(‘+this.hue+’,100%,’+this.brightness+’%,’+this.alpha+’)’;
ctx.stroke();

}

function createParticles(x,y){
//生成30个烟花碎屑
var particleCount = 30;
while(particleCount–) {
particles.push(new Particle(x,y));
}
}

function loop(){
//流畅动画过程
requestAnimationFrame(loop);
hue += 0.5;
ctx.globalCompositeOperation = ‘destination-out’;
ctx.fillStyle = ‘rgba(0, 0, 0, 0.5)’;
ctx.fillRect( 0, 0, cw, ch );
ctx.globalCompositeOperation = ‘lighter’;
var i = fireworks.length;
while(i–) {
fireworks[i].draw();
fireworks[i].update(i);
}
var i = particles.length;
while(i–) {
particles[i].draw();
particles[i].update(i);
}
if(timerTick >= timerTotal) {
fireworks.push(new Firework(cw/2,ch,random(0,cw),random(0,ch/2)));
timerTick = 0;
} else {
timerTick++;
}
}

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlusR">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
  <style>
        body{margin:0;padding:0;overflow: hidden;}
        .city{width:100%;position:fixed;bottom: 0px;z-index: 100;}
        .city img{width: 100%;}
    </style>
 </head>
 <body>
    <canvas id='cas' style="background-color:rgba(0,5,24,1)">canvas</canvas>
    <div class="city"><img src="city.png" alt="" /></div>
    <img src="moon.png" alt="" id="moon" style="visibility: hidden;"/>
    <div style="display:none">
        <div class="shape">HAPPY</div>
        <div class="shape">HAPPY</div>
        <div class="shape">HAPPY</div>
        <div class="shape">HAPPY</div>
        <div class="shape">HAPPY</div>
    </div>
    <audio src="boom.mp3" preload="auto"></audio>
    <audio src="boom.mp3" preload="auto"></audio>
    <audio src="boom.mp3" preload="auto"></audio>
    <audio src="boom.mp3" preload="auto"></audio>
    <audio src="boom.mp3" preload="auto"></audio>
    <audio src="boom.mp3" preload="auto"></audio>
    <audio src="shotfire.mp3" preload="auto"></audio>
    <audio src="shotfire.mp3" preload="auto"></audio>
    <audio src="shotfire.mp3" preload="auto"></audio>
 </body>
 <script>
    var cas=document.getElementById('cas'),
        ctx=cas.getContext('2d'),
        ocas=document.createElement('canvas'),
        octx=ocas.getContext('2d'),
        cw=document.compatMode=='BackCompat'?document.body.clientWidth:document.documentElement.clientWidth,
        ch=document.compatMode=='BackCompat'?document.body.clientHeight:document.documentElement.clientHeight,
        last,
        now,
        stars=[],
        booms=[],
        moon=document.getElementById('moon'),
        shape=document.querySelectorAll('.shape'),
        audio=document.getElementsByTagName('audio');
    cas.width=ocas.width=cw;
    cas.height=ocas.height=ch;
    window.onload=function(){
        createStars();
        last=+new Date();
        animate();
    }

    function animate(){
        ctx.save();
        ctx.globalCompositeOperation='destination-out';
        ctx.globalAlpha=0.1;
        ctx.fillRect(0,0,cw,ch);
        ctx.restore();

        stars.forEach(function(){
            this.paint();
        });
        createMoon();

        now=+new Date();
        if(now-last>1000){
            var rand=getRand(0,100)>50?true:false,num=Math.floor(getRand(0,shape.length)),
                x=getRand(cw/3,cw*2/3),y=ch,r=2,color='#fff',tx=getRand(cw/5,cw*4/5),ty=getRand(50,ch-300);
            if(rand){
                booms.push(new Firework(x,y,r,color,tx,ty));
            }else{
                booms.push(new Firework(x,y,r,color,tx,ty,shape[num]));
            }
            for(var i=0;i<audio.length;i++){
                if(audio[i].src.indexOf('shotfire')>=0 && (audio[i].paused || audio[i].ended)){
                    audio[i].play();
                    break;
                }
            }
            last=now;
        }

        booms.forEach(function(){
            var that=this;
            if(!this.dead){
                this.update();
                this.smoke();
            }else{
                this.booms.forEach(function(index){
                    if(!this.dead){
                        this.update();
                    }else{
                        if(index==that.booms.length){
                            booms.splice(booms.indexOf(that),1);
                        }
                    }
                });
            }
        });
        requestAnimationFrame(animate);
    }

    function Particle(x,y,r,color,tx,ty){
        this.x=x;
        this.y=y;
        this.r=r;
        this.color=color;
        this.tx=tx;
        this.ty=ty;
        this.dead=false;
    }
    Particle.prototype={
        paint:function(){
            ctx.beginPath();
            ctx.fillStyle='rgba('+this.color.a+','+this.color.b+','+this.color.c+',1)';
            ctx.fillRect(this.x-this.r,this.y-this.r,2*this.r,2*this.r);
            ctx.closePath();
        },
        update:function(){
            this.ty+=0.3;
            var dx=this.tx-this.x,dy=this.ty-this.y;
            if(Math.abs(dx)<0.1 && Math.abs(dy)<20){
                this.dead=true;
            }else{
                this.x=Math.abs(dx)<0.1?this.tx:this.x+dx*0.1;
                this.y=Math.abs(dy)<0.1?this.ty:this.y+dy*0.1;
                this.paint();
            }
        }
    }

    function Firework(x,y,r,color,tx,ty,shape){
        this.x=x;
        this.y=y;
        this.r=r;
        this.color=color;
        this.tx=tx;
        this.ty=ty;
        this.shape=shape || false;
        this.dead=false;
        this.ba=getRand(30,50);
        this.booms=[];
    }
    Firework.prototype={
        paint:function(){
            ctx.save();
            ctx.beginPath();
            ctx.fillStyle=this.color;
            ctx.arc(this.x,this.y,this.r,0,2*Math.PI);
            ctx.fill();
            ctx.closePath();
            ctx.restore();
        },
        smoke:function(){
            ctx.save();
            ctx.beginPath();
            ctx.fillStyle='rgba(255,228,150,0.3)';
            ctx.arc(this.x,this.y,this.r+Math.random()*3,0,2*Math.PI);
            ctx.fill();
            ctx.closePath();
            ctx.restore();
        },
        update:function(){
            var dx=this.tx-this.x,dy=this.ty-this.y;
            if(Math.abs(dx)<this.ba && Math.abs(dy)<this.ba){
                this.dead=true;
                if(this.shape){
                    this.shapeBoom();
                }else{
                    this.boom();
                }
                for(var i=0;i<audio.length;i++){
                    if(audio[i].src.indexOf('boom')>=0 && (audio[i].paused || audio[i].ended)){
                        audio[i].play();
                        break;
                    }
                }
            }else{
                this.x+=dx*0.03;
                this.y+=dy*0.03;
                this.paint();
            }
        },
        boom:function(){
            var count=getRand(100,300),r,color,tx,ty,angle,distance;
            for(var i=0;i<count;i++){
                r=getRand(1,3);
                angle=getRand(-Math.PI,Math.PI);
                color={
                    a:parseInt(getRand(0,255)),
                    b:parseInt(getRand(0,255)),
                    c:parseInt(getRand(0,255))
                },
                distance=getRand(0,count);
                tx=Math.cos(angle)*distance+this.x;
                ty=Math.sin(angle)*distance+this.y;
                this.booms.push(new Particle(this.x,this.y,r,color,tx,ty));
            }
        },
        shapeBoom:function(){
            var that=this,step=4,dx=cw/2-this.tx,dy=ch/2-this.ty;
            getData(this.shape,step,function(dots){
                dots.forEach(function(){
                    that.booms.push(new Particle(that.x,that.y,1,this.color,this.x-dx,this.y-dy));
                });
            });
        }
    }
    function getData(dom,step,callback){
        var text=dom.innerHTML,imgData=[],dots=[],i;
        octx.clearRect(0,0,cw,ch);
        octx.save();
        octx.font='120px 微?雅黑 bold';
        octx.textAlign='center';
        octx.textBaseline='middle';
        octx.fillStyle='rgba('+parseInt(getRand(168,255))+','+parseInt(getRand(168,255))+','+parseInt(getRand(168,255))+',1)';
        octx.fillText(text,cw/2,ch/2);
        octx.restore();

        imgData=octx.getImageData(0,0,cw,ch);
        for(var x=0;x<imgData.width;x+=step){
            for(var y=0;y<imgData.height;y+=step){
                i=(y*imgData.width+x)*4;
                if(imgData.data[i+3]>167){
                    dots.push({x:x,y:y,color:{
                        a:imgData.data[i],
                        b:imgData.data[i+1],
                        c:imgData.data[i+2]
                    }});
                }
            }
        }
        callback(dots);
    }

    function createMoon(){
        var sx=cw-200,sy=100,r=40,add=2;
        if(moon.complete){
            ctx.drawImage(moon,sx,sy,2*r,2*r);
        }else{
            moon.onload=function(){
                ctx.drawImage(moon,sx,sy,2*r,2*r);
            }
        }

        ctx.save();
        ctx.beginPath();
        ctx.fillStyle='rgba(240,219,120,0.05)';
        for(var i=0;i<5;i++){
            ctx.arc(sx+r,sy+r,r+add,0,2*Math.PI);
            ctx.fill();
            add+=2;
        }
        ctx.closePath();
        ctx.restore();
    }
    function createStars(){
        for(var i=0;i<100;i++){
            stars.push(new Star(getRand(0,cw),getRand(0,ch-200),Math.random(),'#fff'));
        }
    }
    function Star(x,y,r,color){
        this.x=x;
        this.y=y;
        this.r=r;
        this.color=color;
    }
    Star.prototype={
        paint:function(){
            ctx.save();
            ctx.beginPath();
            ctx.fillStyle=this.color;
            ctx.arc(this.x,this.y,this.r,0,2*Math.PI);
            ctx.fill();
            ctx.closePath();
            ctx.restore();
        }
    }

    function getRand(min,max){
        return Math.random()*(max-min)+min;
    }
    Array.prototype.forEach=function(callback){
        for(var i=0;i<this.length;i++){
            if(this[i]!=null){
                callback.call(this[i],i);
            }
        }
    }
 </script>
</html>

鼠标点击实现

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlusR">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>?日快?</title>
  <style>

*{margin:0;padding:0;list-style-type:none;}
a,img{border:0;}
body{font:12px/180% Arial, Helvetica, sans-serif, "新宋体";background:#000 url("pic.jpg") no-repeat center center;background-size: 100% 100%;}

#canvas{cursor:crosshair;display:block;color:black;}


  </style>
 </head>
 <body>
  <canvas id='canvas' width='100%' height='100%'></canvas>
<script type="text/javascript" src="lib/jquery-1.11.3.js"></script>
  <script type="text/javascript">
var canvas=document.getElementById('canvas'),
    ctx=canvas.getContext('2d'),
    cw=window.innerWidth,
    ch=window.innerHeight,
    mousedown=!1,
    mx,
    my,
    last,
    now,
    timerTick=0,
    timerTotal=80,
    timeWait=0,
    timeWaitTotal=10,
    fireworks=[],
    particles=[],
    fl,
    pl,
    hue=120,
    accelerate=1.05,
    img=new Image();
canvas.width=cw;
canvas.height=ch;
img.src='pic.jpg';

document.onmousedown=function(e){
    mousedown=!0;
}
document.onmouseup=function(e){
    mousedown=!1;
}
document.onmousemove=function(e){
    mx=e.pageX-canvas.offsetLeft;
    my=e.pageY-canvas.offsetTop;
}
window.onload=function(){
    last=+new Date();
    animate();
}
function animate(){
    now=+new Date();
    if(now-last>30){
        hue=getRand(50,80);
        ctx.globalCompositeOperation='destination-out';
        ctx.globalAlpha=0.5;
        ctx.fillRect(0,0,cw,ch);
        ctx.fill();
        ctx.globalCompositeOperation='lighter';
        ctx.drawImage(img,0,0,cw,ch);
        /*??和更新*/
        fl=fireworks.length-1;
        while(fl>=0){
            fireworks[fl].paint();
            fireworks[fl].update(fl);
            fl--;
        }
        pl=particles.length-1;
        while(pl>=0){
            particles[pl].paint();
            particles[pl].update(pl);
            pl--;
        }
        /*添加?象*/
        if(timerTick>timerTotal){
            if(!mousedown){
                fireworks.push(new Firework(cw/2,ch,getRand(0,cw),getRand(50,ch/2)));
                timerTick=0;
            }
        }else{
            timerTick++;
        }
        if(timeWait>timeWaitTotal){
            if(mousedown){
                fireworks.push(new Firework(cw/2,ch,mx,my));
                timeWait=0;
            }
        }else{
            timeWait++;
        }

    }
    if('requestAnimationFrame' in window){
        requestAnimationFrame(animate);
    }else if('mozRequestAnimationFrame' in window){
        mozRequestAnimationFrame(animate);
    }
}

function Firework(sx,sy,tx,ty){
    this.x=sx;
    this.y=sy;
    this.tx=tx;
    this.ty=ty;
    this.radius=1;
    this.brightness=getRand(50,70);
    this.angle=Math.atan2(this.ty-this.y,this.tx-this.x);
    this.speed=5;
    this.xadd=Math.cos(this.angle);
    this.yadd=Math.sin(this.angle);
    this.coord=[];
    for(var i=0;i<3;i++){
        this.coord.push({x:this.x,y:this.y});
    }
}
Firework.prototype={
    paint:function(){
        ctx.save();
        ctx.beginPath();
        ctx.moveTo(this.coord[this.coord.length-1].x,this.coord[this.coord.length-1].y);
        ctx.lineTo(this.x,this.y);
        ctx.strokeStyle='hsl('+hue+',100%,'+this.brightness+'%)';
        ctx.lineWidth=1.5;
        ctx.stroke();

        ctx.beginPath();
        ctx.arc(this.tx,this.ty,this.radius,0,2*Math.PI);
        ctx.stroke();
        ctx.closePath();
        ctx.restore();
    },
    update:function(index){
        var dx=this.tx-this.x,dy=this.ty-this.y;
        if(Math.abs(dx)<20 && Math.abs(dy)<20){
            fireworks.splice(index,1);
            createParticles(this.x,this.y);
        }else{
            this.x+=this.speed*this.xadd;
            this.y+=this.speed*this.yadd;
            this.speed*=accelerate;
            this.coord.pop();
            this.coord.unshift({x:this.x,y:this.y});
            if(this.radius>8){
                this.radius=1;
            }else{
                this.radius+=0.3;
            }
        }
    }
}
function createParticles(x,y){
    for(var i=0;i<20;i++){
        particles.push(new Particle(x,y));
    }
}
function Particle(x,y){
    this.x=x;
    this.y=y;
    this.hue=getRand(hue-50,hue+50);
    this.brightness=getRand(50,80);
    this.alpha=1;
    this.decay=getRand(0.015,0.03);
    this.angle=getRand(-Math.PI,Math.PI);
    this.speed=getRand(5,10);
    this.friction=0.95;
    this.gravity=2;
    this.xadd=Math.cos(this.angle);
    this.yadd=Math.sin(this.angle);
    this.coord=[];
    for(var i=0;i<5;i++){
        this.coord.push({x:this.x,y:this.y});
    }
}
Particle.prototype={
    paint:function(){
        ctx.beginPath();
        ctx.moveTo(this.coord[this.coord.length-1].x,this.coord[this.coord.length-1].y);
        ctx.lineTo(this.x,this.y);
        ctx.strokeStyle='hsla('+this.hue+',100%,'+this.brightness+'%,'+this.alpha+')';
        ctx.lineWidth=2;
        ctx.stroke();
        ctx.closePath();
    },
    update:function(index){
        this.alpha-=this.decay;
        if(Math.abs(this.alpha)<=this.decay){
            particles.splice(index,1);
        }else{
            this.x+=this.speed*this.xadd;
            this.y+=this.speed*this.yadd+this.gravity;
            this.speed*=this.friction;
            this.coord.pop();
            this.coord.unshift({x:this.x,y:this.y});
        }
    }
}

function getRand(min,max){
    return Math.random()*(max-min)+min;
}


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

这里写图片描述

这里写图片描述

python炫酷烟花表白源代码,用python画烟花的代码 大家好,小编为大家解答python绘制烟花特定爆炸效果的问题。很多人还不知道python炫酷烟花表白源代码,现在让我们一起来看看吧! 阅读详情

相关推荐

C语言源代码 《烟花》

烟花表白程序,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

python code自留地(1)

简易的代码,大家可以随意修改,望增添更多色彩。 将你的“墩”整活起来。 import turtle turtle.title("BingDwenDwen") # 速度 turtle.speed(100) # 左手 turtle.penup() turtle.goto(177, 112) turtle.pencolor("lightgray") turtle.pensize(3) turtle.fillcolor("white") turtle.begin_fill() turtle.pe.

Arican_的博客 1697

C语言烟花代码C语言烟花代码

C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码C语言烟花代码

3个烟花代码,我已上传,也可复制代码

这烟花好看吗 ?源码如下

分享编程技术,让编程的世界没有孤岛。 11万+

一文读懂GAN, pix2pix, CycleGAN和pix2pixHD

本文翻译、总结自朱俊伟的线上报告,主要讲了如何用机器学习生成图片。 来源:Games2018 Webinar 64期 :Siggraph 2018优秀博士论文报告 人员信息 主讲嘉宾 姓名:朱俊彦(Jun-Yan Zhu) 现状:麻省理工学院博士后(PostDoc at MIT),计算机科学与人工智能实验室(Computer Science and Artificial In...

gdymind的博客 6万+

GraphRAG 踩坑记录

GraphRAG踩坑记录

yufanwenshu的博客 4357

HTML5 Canvas实现的跨年烟花源代码

以下是一份基于HTML5 Canvas实现的跨年烟花源代码: 上述代码创建了一个简单HTML页面,在页面上有一个画布用于展示烟花效果,当用户在画布上点击时,会在点击位置附近生成烟花并绽放,烟花具有随机的颜色和大小等效果。

max500600的博客 2538

龙虾 Skill 技能库|OpenClaw+Hermes 全集成 一键调用所有 AI 技能

龙虾 Skill 技能库、OpenClaw、Hermes Agent、AI 技能集成、本地 AI 自动化、办公工具、数字员工

weixin_42219287的博客 627

《烟花》C语言源代码

《烟花》C语言源代码 去发现同类优质开源项目:https://gitcode.com/ 本仓库提供了《烟花》C语言源代码,这是一个充满浪漫气息的表白程序。通过烟花绽放的动画效果,为您营造浪漫的氛围,适合用于各种表白场合。程序运行后,您将看到绚丽的烟花效果,犹如置身现场。快来体验这份独特的浪漫吧! 去发现同类优质开源项目:https://gitcode.com/ ...

gitblog_06716的博客 400

UFS 3.1协议分析(第五章) -- UFS协议栈

ContentsUFS顶层架构应用层UFS命令集UFS 设备管理器UFS任务管理器传输层互连层 UFS顶层架构     UFS基于SCSI SAM架构模型定义了一个完整的协议栈,从上到下依次为应用层、传输层、数据链路层、物理层。UFS使用MIPI联盟的UniPro作为数据链路层和MIPI的M-PHY作为物理层,两者合起来称之为互连层(UFS InterConnect Layer) 应用层     应用层包括UFS命令集、设备管

Frank_sample的专栏 7380

手把手教你学pcie--性能调优 - 中断机制优化

在PCIe系统开发中,中断机制是主机与设备之间通信的重要组成部分。然而,不当的中断设计可能导致CPU负载过高、延迟增加等问题。本文将详细介绍如何优化PCIe系统的中断机制,提升系统性能和响应速度。传统中断(INTx):通过专用引脚发送中断信号,可能引发冲突且效率较低。消息中断(MSI):通过内存写操作发送中断消息,避免冲突并提高效率。扩展消息中断(MSI-X):基于MSI改进,支持更多独立中断向量,适用于复杂场景。选择合适的中断模式对系统性能至关重要。

MHD0815的博客 306

跨年烟花html源代码

跨年烟花html源代码 去发现同类优质开源项目:https://gitcode.com/ 介绍 本仓库提供一套精彩的跨年烟花html源代码,可以帮助您轻松搭建一个具有烟花特效的网页,非常适合用于节日庆典或特殊活动的网页背景。 特点 烟花效果逼真,色彩丰富 支持多种浏览器 代码开源,可自由修改和二次开发 使用说明 下载资源文件至本地 解压文件,得到index.html文件 使用浏览器打开ind...

gitblog_06750的博客 313

LangGraph 实战教程:搭建一个智能办公多Agent协作平台

只存储必要的信息:不要把所有的原始数据都存储在状态中,比如销售数据可以存储在数据库或向量数据库中,状态中只存储数据的 ID 或摘要。使用类型提示:避免类型错误,提高代码的可维护性。支持增量更新:LangGraph 默认支持状态的增量更新(通过reduce函数),比如messages列表可以通过追加新消息的方式更新,而不是每次都重新创建整个列表。Normal Edge 的定义非常简单,只需要在StateGraph对象(稍后会详细讲解)上调用add_edge# 创建一个 StateGraph 对象。

SuperAGI2025 222

python炫酷烟花表白源代码,html代码烟花特效python

在Python中,如果要定义一个可以接收可变数量参数的函数,可以使用特殊符号 *(单个星号)作为函数定义时参数列表的前缀,例如:```python# 函数体部分```这个 *args 代表参数列表里可以接收 0 个或多。参数个在函数体内,可以像操作一个普通的元组岁并茄(tuple)那样访问这个参数列表里的每一个参数,例如 args[0] 表示访问乎察第一个参数。

number_516的博客 1056

PHP与Node.js的对比及优缺点分析

PHP和Node.js是两种常用的服务器端编程语言,它们在不同的应用场景下各有优势和劣势。本文将全方位比较PHP和Node.js的特点,以及它们在编程中的优缺点,并提供相应的源代码示例供参考。需要注意的是,本文只是对PHP和Node.js的一些优缺点进行了简要介绍,实际应用中还需要考虑其他因素,如团队经验、项目规模和预算等。最终的选择应该基于全面的评估和需求分析。PHP:PHP是一种同步阻塞的编程语言,它的处理方式是一次只处理一个请求。Node.js:Node.js的性能较高,特别适合处理高并发的请求。

ByteSparkX的博客 891

23、工业物流中的多智能体路径规划与碰撞处理策略

本文探讨了工业4.0背景下工业物流中的多智能体路径规划(MAPF)与碰撞处理策略。分析了自动导引车(AGV)等移动资源的应用现状,综述了主流路径规划算法如A*、D*Lite、M*等,以及集中式与分布式碰撞检测方法。重点比较了五种碰撞避免策略:路径交叉、优先级、起始时间调整、无交叉路径和速度调整,提出了基于环境条件的策略选择流程,并通过流程图展示了评估、组合、测试与优化的全过程。文章强调应根据订单不确定性、时间约束、优先级和生产组织等因素综合选择或组合策略,以提升物流效率与系统可靠性。

mac99的博客 131

C GDI+ 实现的烟花效果源代码

C# GDI+ 实现的烟花效果源代码 去发现同类优质开源项目:https://gitcode.com/ 欢迎来到本资源仓库!这里提供了一个使用C# GDI+ 技术纯代码实现的烟花绽放效果源代码。本源代码旨在帮助那些对GDI+绘图感兴趣并希望深入学习的朋友。 资源描述 本资源包含一个C#程序,该程序实现了不依赖动态GIF图片的烟花效果。通过精心设计的代码,程序能够模拟出烟花在新年夜空中绚烂绽放的美丽...

gitblog_06741的博客 348

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

烟花算法(Fireworks Algorithm,FWA)源代码 Matlab

烟花算法官方网站 好不容易找到了,百度搜不到,大家不用再去csdn下载了,各种版本都有,相关研究和源代码,嘿嘿嘿

xumm_mm的博客 6919

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

烟花C语言源代码:打造浪漫表白氛围

烟花C语言源代码:打造浪漫表白氛围 去发现同类优质开源项目:https://gitcode.com/ 项目介绍 在浪漫的氛围中,表白无疑是最动人的时刻。《烟花》C语言源代码正是为此而生,它利用C语言编程,模拟出烟花绽放的动画效果,为您营造一场视觉盛宴,让表白瞬间充满浪漫与惊喜。 项目技术分析 《烟花》C语言源代码采用了经典的C语言编程技术,通过控制台输出字符的方式,实现了烟花效果的动态展示。其主要...

gitblog_06734的博客 469

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

庆祝烟花 - Python源代码,附超详细备注!!!!!

庆祝烟花 - Python源代码,附超详细备注!!!!! 去发现同类优质开源项目:https://gitcode.com/ 资源描述 本仓库提供了一个用Python编写的庆祝烟花效果的源代码,并附有超详细的备注,帮助你轻松理解和使用代码。无论你是Python初学者还是有经验的开发者,都可以通过这份代码快速实现一个炫酷的烟花效果。 代码说明 主文件:main.py 和 fireworks.py。其...

gitblog_09805的博客 385

python炫酷烟花表白源代码

  天天敲代码的朋友,有没有想过代码也可以变得很酷炫又浪漫?今天就教大家用Python模拟出绽放的烟花,工作之余也可以随时让程序为自己放一场烟花秀。python炫酷烟花表白源代码      python炫酷烟花表白源代码   这个有趣的小项目并不复杂,只需一点可视化技巧,100余行Python代码和程序库...

计算机毕业论文源码,学生个人网页制作html源码。贴近用户做网络推广和互联网优化。 1031
上一篇: 爬虫
下一篇: canvas爱心时钟
普通网友
博客等级 码龄11年 117粉丝 32原创
评论 27
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值