xxxxxxxxxx
111
let theta, sound, amplitude;
function preload() {
sound = loadSound("Edison.mp3");
}
function setup() {
let cnv = createCanvas(500, 500);
cnv.mouseClicked(togglePlay);
amplitude = new p5.Amplitude();
}
function draw() {
background(153, 51, 0);
frameRate(24);
strokeWeight(1.5);
stroke(255, 150);
// Get an angle based on amplitude
let level = amplitude.getLevel();
let a = (level / width) * 110000;
// Convert it to radians
theta = radians(a);
steps = 6
for(let angle = 0 ; angle < 2* PI ; angle += (2*PI)/steps){
makelinetree(angle, a * 2);
makedottree(angle, height/ 4 )
}
}
function makedottree(ang, len){
push();
// Start the tree from the center of the screen
translate(width / 2 , 0);
// Draw a line half the height pixels
translate(0, height/ 2);
rotate(ang);
// Start the recursive branching!
branchdot(len);
pop();
}
function makelinetree(ang, len){
push();
// Start the tree from the center of the screen
translate(width / 2 , 0);
// Draw a line half the height pixels
translate(0, height/ 2);
rotate(ang);
// Start the recursive branching!
branchline(len);
pop();
}
function branchdot(h) {
// Each branch will be 2/3rds the size of the previous one
h *= 0.66; //was 0.66
// All recursive functions must have an exit condition!!!!
// Here, ours is when the length of the branch is 2 pixels or less
if (h > 6) {
push(); // Save the current state of transformation (i.e. where are we now)
rotate(theta); // Rotate by theta
point(0, 0); // Draw the branch
translate(0, h); // Move to the end of the branch
branchdot(h); // Ok, now call myself to draw two new branches!!
pop(); // Whenever we get back here, we "pop" in order to restore the previous matrix state
push();
rotate(-theta);
point(0, 0);
translate(0, h);
branchdot(h);
pop();
}
}
function branchline(h) {
// Each branch will be 2/3rds the size of the previous one
h *= 0.75; //was 0.66
// All recursive functions must have an exit condition!!!!
// Here, ours is when the length of the branch is 2 pixels or less
if (h > 6) {
push(); // Save the current state of transformation (i.e. where are we now)
rotate(theta); // Rotate by theta
line(0, 0, 0, h); // Draw the branch
translate(0, h); // Move to the end of the branch
branchline(h); // Ok, now call myself to draw two new branches!!
pop(); // Whenever we get back here, we "pop" in order to restore the previous matrix state
push();
rotate(-theta);
line(0, 0, 0, h);
translate(0, h);
branchline(h);
pop();
}
}
function togglePlay() {
if (sound.isPlaying()) {
sound.pause();
} else {
sound.loop();
amplitude = new p5.Amplitude();
amplitude.setInput(sound);
}
}