• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

网页js模拟烟花绽放效果

武飞扬头像
空中湖
帮助1

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

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

学新通

在线demo地址

index.html

  1.  
    <html>
  2.  
    <head>
  3.  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.16/p5.js"></script>
  4.  
    <script src = "https://p5js.org/assets/js/p5.dom.min.js"></script>
  5.  
    <script src="./logic.js"></script>
  6.  
    <script src="./particle.js"></script>
  7.  
    <script src="./Firework.js"></script>
  8.  
    </head>
  9.  
    <body>
  10.  
    </body>
  11.  
    </html>

particle.js

  1.  
     
  2.  
     
  3.  
    function Particle(x,y,hu,firework){
  4.  
    this.firework = firework;
  5.  
    this.lifespan = 255;
  6.  
    this.hu = hu;
  7.  
     
  8.  
    this.pos = createVector(x,y);
  9.  
    if (this.firework)
  10.  
    this.vel = createVector(0,random(-10,-18));
  11.  
    else{
  12.  
    this.vel = p5.Vector.random2D();
  13.  
    this.vel.mult(random(4,12));
  14.  
    }
  15.  
    this.acc = createVector(0,0);
  16.  
     
  17.  
     
  18.  
    this.applyForce = function(force){
  19.  
    this.acc.add(force);
  20.  
    }
  21.  
     
  22.  
    this.done = function(){
  23.  
    if (this.lifespan<=0)
  24.  
    return true;
  25.  
    else return false;
  26.  
    }
  27.  
     
  28.  
    this.update = function(){
  29.  
    if(!this.firework){
  30.  
    this.vel.mult(0.9);
  31.  
    this.lifespan -=4;
  32.  
    }
  33.  
    this.vel.add(this.acc);
  34.  
    this.pos.add(this.vel);
  35.  
    this.acc.mult(0);
  36.  
    }
  37.  
     
  38.  
    this.show = function(){
  39.  
    colorMode(HSB);
  40.  
    if(!this.firework){
  41.  
    strokeWeight(2);
  42.  
    stroke(hu,255,255,this.lifespan);
  43.  
    }
  44.  
    else {
  45.  
    stroke(hu,255,255);
  46.  
    }
  47.  
     
  48.  
    point(this.pos.x,this.pos.y);
  49.  
    }
  50.  
     
  51.  
    }
学新通

logic.js

  1.  
     
  2.  
    var fireworks = [];
  3.  
     
  4.  
    function setup(){
  5.  
    createCanvas(window.innerWidth,window.innerHeight);
  6.  
    colorMode(HSB);
  7.  
    stroke(255);
  8.  
    strokeWeight(4);
  9.  
    gravity = createVector(0,0.2);
  10.  
    background(0);
  11.  
     
  12.  
    }
  13.  
     
  14.  
     
  15.  
     
  16.  
    function draw(){
  17.  
    colorMode(RGB);
  18.  
    background(0,0,0,25);
  19.  
    if (random(1)<0.05){
  20.  
    fireworks.push(new Firework());
  21.  
    }
  22.  
    for (var i = fireworks.length-1; i >= 0; i--) {
  23.  
    fireworks[i].update();
  24.  
    fireworks[i].show();
  25.  
    if (fireworks[i].done()){
  26.  
    fireworks.splice(i,1);
  27.  
    }
  28.  
    }
  29.  
    console.log(fireworks.length);
  30.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhghcifk
系列文章
更多 icon
同类精品
更多 icon
继续加载