网页js模拟烟花绽放效果

烟花又称花炮、烟火、焰火、炮仗,根据《中国烟花爆竹标准—安全与质量》对烟花爆竹的定义为:以烟火药为原料,用于产生声光色的娱乐用品。中国劳动人民较早发明,常用于盛大的典礼或表演中。
烟花其实和爆竹的结构类似,其结构都包含黑火药和药引。为了达到好的表演效果,焰火和礼花弹中填充了大量用于发射以及爆炸的火药,例如,一个直径为20厘米的礼花弹在发射后,要上升到大概200米的高空才会爆炸,而这些星星点点覆盖的半径大约可以有80米左右。

在网页端模拟烟花会不会更棒,已经有小朋友做出了非常好的效果,让我们来一起看一下,有图有真相:

在线demo地址

index.html

<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.16/p5.js"></script>
    <script src = "https://p5js.org/assets/js/p5.dom.min.js"></script>
    <script src="./logic.js"></script>
    <script src="./particle.js"></script>
    <script src="./Firework.js"></script>
  </head>
  <body>
  </body>
</html>

particle.js



function Particle(x,y,hu,firework){
  this.firework = firework;
  this.lifespan = 255;
  this.hu = hu;

  this.pos = createVector(x,y);
  if (this.firework)
  this.vel = createVector(0,random(-10,-18));
  else{
    this.vel = p5.Vector.random2D();
    this.vel.mult(random(4,12));
  }
  this.acc = createVector(0,0);


  this.applyForce = function(force){
    this.acc.add(force);
  }

  this.done = function(){
    if (this.lifespan<=0)
    return true;
    else return false;
  }

  this.update = function(){
    if(!this.firework){
      this.vel.mult(0.9);
      this.lifespan -=4;
    }
    this.vel.add(this.acc);
    this.pos.add(this.vel);
    this.acc.mult(0);
  }

  this.show = function(){
    colorMode(HSB);
    if(!this.firework){
      strokeWeight(2);
      stroke(hu,255,255,this.lifespan);
    }
    else {
    stroke(hu,255,255);
    }

    point(this.pos.x,this.pos.y);
  }

}

logic.js


var fireworks = [];

function setup(){
  createCanvas(window.innerWidth,window.innerHeight);
  colorMode(HSB);
  stroke(255);
  strokeWeight(4);
  gravity = createVector(0,0.2);
  background(0);

}



function draw(){
  colorMode(RGB);
  background(0,0,0,25);
  if (random(1)<0.05){
    fireworks.push(new Firework());
  }
  for (var i = fireworks.length-1; i >= 0; i--) {
    fireworks[i].update();
    fireworks[i].show();
    if (fireworks[i].done()){
      fireworks.splice(i,1);
    }
  }
console.log(fireworks.length);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值