Skip to content

Instantly share code, notes, and snippets.

@Craigson
Created November 10, 2014 20:39
Show Gist options
  • Select an option

  • Save Craigson/1b205f1a0a345604d38e to your computer and use it in GitHub Desktop.

Select an option

Save Craigson/1b205f1a0a345604d38e to your computer and use it in GitHub Desktop.
soundball Javascript file
//set boolean variables for toggling size chang and colour change
var changeColour = false;
var changeSize = false;
var x,y;
var input;
var analyser;
//setting the velocity in x and y directions randomly
var velocityX = 1;
var velocityY = 1;
//create variables for the colour
var r = random(0, 255);
var g = random(0, 255);
var b = random(0, 255);
var diam = random(10,30);
function setup() {
createCanvas(600, 600);
noStroke();
mic = new p5.AudioIn();
mic.start();
//x and y position of the circle is determined randomly
x = random(1, 595);
y = random(1, 595);
}
function draw() {
background(125);
ellipse(x, y, diam, diam);
var vol = mic.getLevel();
var l = map(vol,0,1,1,100);
var c = map(vol,0,1,0,255);
//sets the x and y positions as the current position + the velocity, ie speed at which it moves
x = x + velocityX*l;
y = y + velocityY*l*1.2;
//if the changeSize variable is set to true, the dimension is 55, if false, it is 32
if (changeSize){
diam = 55;
}else{
diam = 32;
}
//these lines of code tell the circle to change direction when it reaches the border
if ((x > width) || (x < 0)) {
velocityX = -velocityX;
}
else if ((y > height) || (y < 0)) {
velocityY = - velocityY;
}
}
function mousePressed() {
changeColour = !changeColour;
}
function keyPressed(){
changeSize = !changeSize;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment