Last active
March 24, 2018 22:03
-
-
Save alexanderhenne/8893906c704bc23c297f9b98c36f08e3 to your computer and use it in GitHub Desktop.
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
import java.util.*; | |
void setup() { | |
size(800, 600); | |
} | |
static final int OVAL_COUNT = 30; | |
static final int START_OPACITY = 255; | |
static final long SPAWN_DELAY = 250; | |
static final long DURATION = 1000; | |
List<Mandala> mandalas = new ArrayList<Mandala>(); | |
long lastSpawnTime; | |
void draw() { | |
background(14); | |
noFill(); | |
long currentTime = System.currentTimeMillis(); | |
// Lägg till en mandala varje SPAWN_DELAY millisekunder | |
if (currentTime - lastSpawnTime >= SPAWN_DELAY) { | |
spawnMandala((int) random(50, width - 50), (int) random(50, height - 50)); | |
lastSpawnTime = currentTime; | |
} | |
Iterator<Mandala> iterator = mandalas.iterator(); | |
while (iterator.hasNext()) { | |
Mandala mandala = iterator.next(); | |
pushMatrix(); | |
translate(mandala.x, mandala.y); | |
stroke(mandala.shapeColor, mandala.opacity); | |
float radToRotate = TWO_PI / OVAL_COUNT; | |
for (int i = 0; i < OVAL_COUNT; i++) { | |
rotate(radToRotate); | |
ellipse(0, 0, mandala.shapeW, mandala.shapeH); | |
} | |
popMatrix(); | |
// Gör mandalan mindre opak beroende på hur länge mandalan funnits | |
long timeSinceAddition = currentTime - mandala.addedTime; | |
mandala.opacity = (int) (START_OPACITY - timeSinceAddition * START_OPACITY / (float) DURATION); | |
// Ta bort mandalan om den är 100% transparent | |
if (mandala.opacity <= 0) { | |
iterator.remove(); | |
} | |
} | |
} | |
void spawnMandala(int x, int y) { | |
int h = round(random(150, 200)); | |
mandalas.add(new Mandala(x, y, 200 - h, h, generateRandomColor())); | |
} | |
// http://stackoverflow.com/a/43235/4313694 | |
color generateRandomColor() { | |
int r = round(random(0, 255)); | |
int g = round(random(0, 255)); | |
int b = round(random(0, 255)); | |
r = (r + 200) / 2; | |
g = (g + 200) / 2; | |
b = (b + 200) / 2; | |
return color(r, g, b); | |
} | |
void keyPressed() { | |
// Ta bort alla mandalas när man trycker på space | |
if (keyCode == 32) { | |
mandalas.clear(); | |
} | |
} | |
class Mandala { | |
final int x; | |
final int y; | |
final int shapeW; | |
final int shapeH; | |
final color shapeColor; | |
int opacity = START_OPACITY; | |
final long addedTime = System.currentTimeMillis(); | |
Mandala(int x, int y, | |
int shapeW, int shapeH, | |
color shapeColor) { | |
this.x = x; | |
this.y = y; | |
this.shapeW = shapeW; | |
this.shapeH = shapeH; | |
this.shapeColor = shapeColor; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very good!