IT IS DONE!

Published by taliya on

I have finally finished my Ritual ! Very happy with the result even though there were many frustrating moments within this code I am happy to say I did it! I created the characteristics of the firework I wanted and made it interactive with the audience! I still wish I could have created the 3D version of this but at my beginner level this would consume to much time for me to learn.

The Code:

var fireworks = [];
var gravity;
var mic;

var angle = 0;
//setting up the canvas
function setup() {
createCanvas(windowWidth, windowHeight);
gravity = createVector(0, 0.2);
stroke(255);
strokeWeight(4);
background(0);
mic = new p5.AudioIn();
mic.start();
}

//Using sound to create an action
function draw() {
background(0, 10);
var vol = mic.getLevel();
//creating a Min and a Max.
if(fireworks.length < 5 && vol > .01){
var maxVol = round(vol * 100);
if (maxVol > 12){
maxVol = 12
}
for (var i = 0; i < maxVol; i++) {
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);
}
}

}

function mouseClicked() {
for (var i = 0; i < random(1, 10); i++) {
fireworks.push(new Firework());
}

}

function Firework() {
//colours of particle
this.hu = random(255);
this.colorList = [‘255, 120, 120’, ‘156, 0, 255’, ‘103, 17, 184′, ’94, 249, 152’, ‘144,3,253’, ‘7, 14, 104’, ‘55,245, 220’, ‘255, 255, 255’, ‘137, 11, 29’];
this.fcolor = random(this.colorList)
this.firework = new Particle(random(width), height, this.fcolor, true);
this.exploded = false;
this.particles = [];

this.done = function() {
if (this.exploded && this.particles. length == 0){
return true;
} else {
return false;
}
}
//Applying movemment
this.update = function() {
if (!this.exploded) {
this.firework.applyForce(gravity);
this.firework.update();
if (this.firework.vel.y >= 0) {
this.exploded = true;
this.explode();
}
}

for (var i = this.particles.length-1; i >=0; i–) {
this.particles[i].applyForce(gravity);
this.particles [i].update();
if (this.particles[i].done()) {
this.particles.splice(i, 1);
}
}
}
//Amount of particles in explosion
this.explode = function() {
var exp = random(20, 500)
for (var i = 0; i < exp; i++) {
var p = new Particle(this.firework.pos.x, this.firework.pos.y, this.fcolor, false)
this.particles.push(p);
}
}

this.show = function() {
if (!this.exploded) {
this.firework.show();
}
for (var i = 0; i < this.particles.length; i++) {
this.particles[i].show();
}
}

}

function Particle(x, y, hu, firework) {
this.pos = createVector(random(x, x -100), random (y, y -100));
this.firework = firework;
this.lifespan = 400;
this.hu = hu;

if (this.firework) {
this.vel = createVector(0, random(-20, -8));
} else {
this.vel = p5.Vector.random2D();
this.vel.mult(random (2, 10));
}
this.acc = createVector(0, 0);

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

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.done = function() {
if (this.lifespan <0) {
return true;
} else{
return false;
}
}

//colouring and stroke weight of fireworks
this.show = function() {
if (!this.firework) {
strokeWeight (2.5)
stroke(‘rgba(‘+ this.hu + ‘,’ + this.lifespan + ‘)’);
} else {
strokeWeight(4);
//stroke(this.hu, 255, 255);
stroke(‘rgba(‘+ this.hu + ‘,’ + this.lifespan + ‘)’);
}
point(this.pos.x, this.pos.y);
}

}

 

Goodbye Code

Categories: The process

Leave a Reply

Your email address will not be published. Required fields are marked *