openobject.org

Morphing Ball

From Physical Programming

//The ball change colors and its shape according to the mouse's location

 void setup(){
   frameRate(60);
   size(500,500);
   smooth();
 }
 
 void draw(){
   float cellWidth=250, cellHeight=250;
   float x, y;
   
   background(0);
   x=mouseX;
   if (mouseX<125){
     cellWidth=mouseX*2;
     x=mouseX;
   }
 
   if (mouseX<50){
     x=50;
     cellWidth=100;
   }
   
   if (mouseX>375){
     cellWidth=(500-mouseX)*2;
     x=mouseX;
   }
   
   if (mouseX>450){
     x=450;
     cellWidth=100;
   }
   
   y=mouseY;
   
   if (mouseY<125){
     cellHeight=mouseY*2;
     y=mouseY;
   }
   
   if (mouseY<50){
     y=50;
     cellHeight=100;
   }
   
   if (mouseY>375){
     cellHeight=(500-mouseY)*2;
     y=mouseY;
   }
   
   if (mouseY>450){
     y=450;
     cellHeight=100;
   }
   
   morph(cellWidth,cellHeight, x, y);
 
 }
 
 void morph(float cellWidth, float cellHeight, float x, float y){
   fill(abs(x-y), abs(255-x), abs(255-y));
   stroke(255);
   ellipse(x, y, cellWidth, cellHeight);
 }