Mouse manipulates light

Use mouse to vary light and shadow.
Tutorial by Ramesh Palischat.

DragButton d_s = new DragButton(300,300,40);
DragButton d_e = new DragButton(100,100,60);
float SHADOW_ALPHA = 180;
float SHADOW_MAX_LENGTH = 15;
void setup()
{
 size(500, 500);
 smooth();
 ellipseMode(CENTER);    
}
 
void draw(){    
 background(200);
 drawShadow(d_s);
 drawShadow(d_e);
 d_s.display();
 d_e.display();    
}
 
void drawShadow(DragButton d){
 fill(0,0,0,SHADOW_ALPHA);
 noStroke();
 float dx = mouseX - d._x;
 float dy = mouseY - d._y;
 float ax = dx*SHADOW_MAX_LENGTH/width;
 float ay = dy*SHADOW_MAX_LENGTH/width;
 ellipse(d._x-ax ,d._y-ay,(float)d.bs,(float)d.bs);
}
 
void mousePressed(){
 d_s.onPress();
 d_e.onPress();
}
 
void mouseReleased(){
 d_s.onRelease();
 d_e.onRelease();
}
 
void mouseDragged(){
 d_s.onDrag();
 d_e.onDrag();
}
class DragButton{
 float _x;
 float _y;
 int bs = 20;
 boolean bover = false;
 boolean locked = false;
 float bdifx = 0.0;
 float bdify = 0.0;
 DragButton(float nx,float ny,int ns){
 _x=nx;
 _y=ny;
 bs=ns;
 }
 void display()
 {
 //background(0);
 
 // Test if the cursor is over the box
 if (mouseX > _x-bs && mouseX < _x+bs &&
 mouseY > _y-bs && mouseY < _y+bs) {
 bover = true;
 if(!locked) {
 stroke(255);
 fill(153);
 }
 } else {
 stroke(153);
 fill(153);
 bover = false;
 }
 // Draw the box
 if(locked){ fill(255); }
 ellipse(_x, _y, bs, bs);
 }
 
 void onPress() {
 if(bover) {
 locked = true;
 fill(255, 255, 255);
 } else {
 locked = false;
 }
 bdifx = mouseX-_x;
 bdify = mouseY-_y;
 
 }
 
 void onDrag() {
 if(locked) {
 _x = mouseX-bdifx;
 _y = mouseY-bdify;
 }
 }
 
 void onRelease() {
 locked = false;
 }
}
12.06.2010 | admin |