Last active
August 1, 2016 22:14
-
-
Save camb416/4fb8d2d67ca8df158e33eed69d20e0c3 to your computer and use it in GitHub Desktop.
Distance and easing decay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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