xxxxxxxxxx
109
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// A reference to our box2d world
let world;
// A list for all of our particles
let particles = [];
// An object to store information about the uneven surface
let surface;
var input;
var analyzer;
function setup() {
createCanvas(600, 600);
// Initialize box2d physics and create the world
world = createWorld();
// Create the surface
surface = new Surface();
// Create an audio input
input = new p5.AudioIn();
input.start();
}
function draw() {
background(255, 170, 0);
//Bathroom Backdrop
for (var j = 0; j < 700; j = j + 50){
for( var k = 0; k < 542; k = k + 50){
stroke(255, 235, 196);
line(j, 0, j, 500);
line(0, k, 700, k);
}
}
//tap
fill(255, 235, 196);
rect(width/2, 130, 60, 10);
rect(width/2, 130, 10, 30);
//mirror
push();
noStroke();
fill(237, 255, 249);
rect(width/2, 0, 150, 170);
stroke(255);
strokeWeight(5);
line(width/2 - 10, 30, width/2 + 40, 0);
line(width/2 + 10, 40, width/2 + 60, 10);
line(width/2 + 10, 65, width/2 + 60, 35);
pop();
//plant
push();
noStroke();
fill(209, 255, 73);
ellipse(130, 470, 20, 20);
ellipse(120, 490, 30, 80);
ellipse(103, 470, 30, 30);
ellipse(90, 460, 30, 50);
ellipse(80, 470, 30, 30);
ellipse(70, 470, 20, 20);
fill(150, 125, 117);
rect(100, 500, 80, 60);
pop();
// We must always step through time!
let timeStep = 1.0 / 30;
// 2nd and 3rd arguments are velocity and position iterations
world.Step(timeStep, 10, 10);
// Get overall volume (between 0 and 1.0)
var volume = input.getLevel();
// particles fall from the top every so often
// If the volume > 0.1, the bubbles come out of sink
// The louder the volume, the larger the bubbles.
var threshold = 0.002;
if (volume > threshold){
// if (mouseIsPressed){
if (random(1) < 0.5) {
let sz = random(15 * volume, 40 * volume);
particles.push(new Particle(width / 2, 150, sz));
}
}
// Draw the surface
surface.display();
// Display all the particles
for (let i = particles.length - 1; i >= 0; i--) {
particles[i].display();
if (particles[i].done()) {
particles.splice(i, 1);
}
}
}