Skip to content

Instantly share code, notes, and snippets.

@mharju
Created June 5, 2017 16:58
Show Gist options
  • Save mharju/92d350395e35553e64c0c4df3e820629 to your computer and use it in GitHub Desktop.
Save mharju/92d350395e35553e64c0c4df3e820629 to your computer and use it in GitHub Desktop.
void setup() {
size(1100, 600);
}
enum Axis { X, Y };
void setGradient(int x, int y, float w, float h, color c1, color c2, Axis axis ) {
noFill();
if (axis == Axis.Y) { // Top to bottom gradient
for (int i = y; i <= y+h; i++) {
float inter = map(i, y, y+h, 0, 1);
color c = lerpColor(c1, c2, inter*inter*inter*inter);
stroke(c);
line(x, i, x+w, i);
}
}
else if (axis == Axis.X) { // Left to right gradient
for (int i = x; i <= x+w; i++) {
float inter = map(i, x, x+w, 0, 1);
color c = lerpColor(c1, c2, inter*inter*inter*inter);
stroke(c);
line(i, y, i, y+h);
}
}
}
enum State {
BORN, ALIVE, DYING
};
ArrayList<PVector> points = new ArrayList<PVector>();
ArrayList<Float> radii = new ArrayList<Float>();
ArrayList<Float> directions = new ArrayList<Float>();
ArrayList<State> states = new ArrayList<State>();
ArrayList<Integer> frames = new ArrayList<Integer>();
void mouseMoved() {
PVector m = new PVector(mouseX, mouseY);
if (points.size() == 0) {
points.add(m);
radii.add(random(5, 200));
directions.add(random(1));
states.add(State.BORN);
frames.add(0);
return;
}
PVector tail = points.get(points.size() - 1);
if(tail.dist(m) > 50) {
m = m.add(new PVector(random(-50,50), random(-50, 50)));
points.add(m);
radii.add(random(5, 200));
directions.add(random(1));
states.add(State.BORN);
frames.add(0);
}
}
void draw() {
setGradient(0, 0, width, height, color(0), color(47, 27, 84), Axis.Y);
noStroke();
for(int i=0;i<points.size();i++) {
PVector p = points.get(i);
float r = radii.get(i);
float d = directions.get(i) < 0.5 ? -1 : 1;
fill(255, 255, 255, map((float)frames.get(i), 0, 50, 0, 32));
ellipse(p.x, p.y, r, r);
p.y += 0.1 * random(0,1) * r/100 * d;
p.x += 0.1 * random(0,1) * r/100 * d;
points.set(i, p);
if(states.get(i) == State.BORN) {
frames.set(i, frames.get(i) + 1);
if(frames.get(i) == 50) {
states.set(i, State.ALIVE);
}
} else if(states.get(i) == State.DYING) {
frames.set(i, frames.get(i) - 1);
if(frames.get(i) == 0) {
r = random(5, 200);
points.set(i, new PVector(random(r, width-r), random(r, height-r)));
radii.set(i, r);
directions.set(i, random(1));
states.set(i, State.BORN);
}
} else if ((p.x < 0 || p.y < 0 ||
p.x > width || p.y > height) &&
states.get(i) == State.ALIVE) {
states.set(i, State.DYING);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment