/* Austin Garcia
Section C
aegarcia@andrew.cmu.edu
Project 10
*/
//I decided to take some old code from a lab that I bugged out on accident
// and use it as a visually interesting way to generate random y VALS
// PLEASE CLICK ON CANVAS AND DRAG mouseX
var balls = [200]
var ball = {
x: balls,
y: 0,
speed: 0,
display: function() {
fill(255); ellipse(this.x, this.y, 20, 20);
},
move: function() {
this.y = this.y + this.speed;
this.speed = this.speed + gravity;
},
bounce: function() {
if (this.y > height - 11) {
this.speed = this.speed * -1;
}
}
};
var xarray = []
var yarray = []
var size = 0;
var gravity = 0.1;
function setup() {
// you can change the next 2 lines:
createCanvas(400, 700);
createDiv("p5.dom.js library is loaded.");
//======== call the following to use sound =========
useSound();
}
function soundSetup() { // setup for audio generation
// you can replace any of this with your own audio code:
osc = new p5.TriOsc();
osc.freq(500);
osc.amp(.1);
osc.start();
osc1 = new p5.TriOsc();
osc1.freq(200);
osc1.amp(.1);
osc1.start();
frameRate(10)
}
function draw() {
// you can replace any of this with your own code:
background(200);
fill("yellow");
size = 1
//draw circle
for (var i = 0; i < xarray.length; i += 1) {
ellipse(xarray[i], yarray[i], size, size)
size += 1
if (size > 20) {
size = 1
}
yarray.push(random(10, height - 10));
osc1.freq(yarray[i])
}
if (xarray.length > 5 || yarray.length > 5) {
xarray.shift();
yarray.shift();
}
//draw line
stroke("orange");
strokeWeight(4);
for (var i = 0; i <= xarray.length; i += 1) {
//for (var n = 0; n <= yarray.length; n += 1){} ADD THIS IN FOR A COOL EFFECT (DONT FORGET TO CHANGE Y VALS)
line (xarray[i], yarray[i], xarray[i+1], yarray[i+1]);
}
ball.display();
ball.move();
ball.bounce();
osc.freq(-ball.y);
osc.amp();
}
function mouseDragged() {
xarray.push(mouseX);
yarray.push(mouseY);
}
function mousePressed() {
xarray = []
yarray = []
}
I lowered the frame rate to give the randomness less of an oppressive sound. I kept the gradual rise and fall of the sphere in the center as a constant element amidst the randomness.