Skip to content

Instantly share code, notes, and snippets.

@camb416
Last active August 1, 2016 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camb416/4fb8d2d67ca8df158e33eed69d20e0c3 to your computer and use it in GitHub Desktop.
Save camb416/4fb8d2d67ca8df158e33eed69d20e0c3 to your computer and use it in GitHub Desktop.
Distance and easing decay
ArrayList<Ball> balls;
boolean doScale;
void setup() {
doScale = false;
size(1280, 720);
balls = new ArrayList<Ball>();
for (int i=0; i<width/20; i++) {
for (int j=0; j<height/20; j++) {
Ball b = new Ball();
b.setup(i*20, j*20);
balls.add(b);
}
}
fill(255);
noStroke();
}
void draw() {
background(0);
for (int i=0; i<balls.size(); i++) {
balls.get(i).update(mouseX, mouseY);
}
for (int i=0; i<balls.size(); i++) {
balls.get(i).draw();
}
}
void mousePressed() {
doScale = !doScale;
}
class Ball {
float scale,
dScale;
float x,
y;
float decay;
void setup(float _x, float _y) {
x = _x;
y = _y;
scale = 5.0f;
decay = 64.0f; // change this to make the ease faster (4.0 is pretty quick but nice)
}
void update(int _mx, int _my) {
if (doScale) {
float aDist = dist(_mx, _my, x, y);
if(aDist < 3.0f) aDist = 3.0f; // prevent division by zero
dScale = 1500.0f/aDist;
} else {
dScale = 3.0f;
}
scale += (dScale - scale)/decay; // this is the meat of the ease!
}
void draw() {
ellipse(x, y, scale, scale);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment