Advertisement
SowzzG

Firework JS With wind!

Jan 22nd, 2017
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // NEW FIREWORK WITH WIND
  2. // This needs p5.js
  3. // Link to p5.js: http://p5js.org/download/
  4.  
  5.  
  6.  
  7. //HTML CODE:
  8.  
  9.  
  10. <DOCTYPE html>
  11. <html>
  12. <head>
  13.   <meta charset="UTF-8">
  14.   <script language="javascript" type="text/javascript" src="libraries/p5.js"></script>
  15.   <!-- uncomment lines below to include extra p5 libraries -->
  16.   <!--<script language="javascript" src="libraries/p5.dom.js"></script>-->
  17.   <!--<script language="javascript" src="libraries/p5.sound.js"></script>-->
  18.   <script language="javascript" type="text/javascript" src="sketch.js"></script>
  19.   <!-- this line removes any default padding and style. you might only need one of these values set. -->
  20.   <style> body {padding: 0; margin: 0;} </style>
  21. </head>
  22.  
  23. <body>
  24. </body>
  25. </html>
  26.  
  27.  
  28.  
  29. //JS CODE!
  30. var fireworks = [];
  31. var gravity;
  32. var wind;
  33.  
  34. function setup() {
  35.   createCanvas(1600, 800);
  36.   colorMode(HSB);
  37.   wind = createVector(random(-0.05, 0.05));
  38.   gravity = createVector(0, 0.2);
  39.   stroke(255);
  40.   strokeWeight(4);
  41.   background(0);
  42. }
  43.  
  44. function draw() {
  45.   colorMode(RGB);
  46.     background(0, 0, 0, 55);
  47.     if (random(1) < 0.08) {
  48.         fireworks.push(new Firework());
  49.     }
  50.   for (var i = fireworks.length-1; i >= 0; i--) {
  51.     fireworks[i].update();
  52.     fireworks[i].show();
  53.     if (fireworks[i].done()) {
  54.       fireworks.splice(i, 1);
  55.     }
  56.   }
  57.   console.log(fireworks.length)
  58. }
  59. function Particle(x, y, hu, firework) {
  60.     this.pos = createVector(x, y);
  61.     this.firework = firework;
  62.     this.lifespan = 255;
  63.     this.hu = hu;
  64.     this.wind = wind;
  65.  
  66.     if (this.firework) {
  67.         this.vel = createVector(0, random(-12, -18));
  68.         } else {
  69.         this.vel = p5.Vector.random2D();
  70.         this.vel.mult(random(2, 6));
  71.     }
  72.     this.acc = createVector(0, 0);
  73.  
  74.     this.applyForce = function(force) {
  75.         this.acc.add(force);
  76.     }
  77.  
  78.     this.update = function() {
  79.         if (!this.firework) {
  80.             this.vel.mult(0.85);
  81.             this.lifespan -= 4;
  82.         }
  83.         this.vel.add(this.acc);
  84.         this.pos.add(this.vel);
  85.         this.acc.mult(0);
  86.     }
  87.  
  88.  
  89.  
  90.     this.done = function() {
  91.         if (this.lifespan < 0) {
  92.             return true;
  93.         } else {
  94.             return false;
  95.         }
  96.     }
  97.  
  98.     this.show = function() {
  99.         colorMode(HSB);
  100.         if (!this.firework) {
  101.             strokeWeight(2);
  102.             stroke(hu, 255, 255, this.lifespan);
  103.         } else {
  104.             strokeWeight(4);
  105.             stroke(hu, 255, 255);
  106.         }
  107.         point(this.pos.x, this.pos.y);
  108.     }
  109.  
  110. }
  111. function Firework() {
  112.  
  113.     this.hu = random(255);
  114.     this.firework = new Particle(random(width), height, this.hu, true);
  115.     this.exploded = false;
  116.     this.particles = [];
  117.  
  118.     this.done = function() {
  119.         if (this.exploded && this.particles.length === 0) {
  120.             return true;
  121.         } else {
  122.             return false;
  123.         }
  124.     }
  125.  
  126.     this.update = function() {
  127.  
  128.         if (!this.exploded) {
  129.             this.firework.applyForce(gravity);
  130.             this.firework.applyForce(wind);
  131.             this.firework.update();
  132.             if (this.firework.vel.y >= 0) {
  133.                 this.exploded = true;
  134.                 this.explode();
  135.             }
  136.         }
  137.         if (random(0, 0,8) < 0.08) {
  138.             wind = createVector(random(-0.15, 0.15));
  139.         }
  140.  
  141.  
  142.         for (var i=this.particles.length-1; i >= 0; i--) {
  143.             this.particles[i].applyForce(gravity);
  144.             this.particles[i].applyForce(wind)
  145.             this.particles[i].update();
  146.             if (this.particles[i].done()) {
  147.                 this.particles.splice(i, 1);
  148.             }
  149.         }
  150.     }
  151.  
  152.  
  153.         this.explode = function() {
  154.             for (var i=0; i < 100; i++) {
  155.             var p = new Particle(this.firework.pos.x, this.firework.pos.y, this.hu);
  156.             this.particles.push(p);
  157.         }
  158.     }
  159.  
  160.     this.show = function() {
  161.         if (!this.exploded) {
  162.             this.firework.show();
  163.         }
  164.         for (var i=0; i < this.particles.length; i++) {
  165.             this.particles[i].show();
  166.         }
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement