Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.