Created
August 28, 2015 21:58
-
-
Save dariusk/29357df69d508e43e91c 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
int maxParticles = 100; ArrayList <Particle> particles = new ArrayList <Particle> (); int drawMode = 0; color BACKGROUND_COLOR = color(255); color PGRAPHICS_COLOR = color(0); PGraphics pg; void setup() { size(1280, 720, P2D); smooth(16); pg = createGraphics(width, height, JAVA2D); pg.beginDraw(); pg.textSize(500); pg.textAlign(CENTER, CENTER); pg.fill(PGRAPHICS_COLOR); pg.text("TYPE", pg.width/2, pg.height/2); pg.endDraw(); background(BACKGROUND_COLOR); } void draw() { addRemoveParticles(); for (Particle p : particles) { p.update(); p.display(); } } void mousePressed() { drawMode = ++drawMode%4; background(BACKGROUND_COLOR); if (drawMode == 2) image(pg, 0, 0); particles.clear(); } void addRemoveParticles() { if (drawMode >= 1) { for (int i=particles.size()-1; i>=0; i--) { Particle p = particles.get(i); if (p.life <= 0) { particles.remove(i); } } } while (particles.size () < maxParticles) { particles.add(new Particle()); } } class Particle { PVector loc, vel; float radius = 10; float life = 1; float lifeRate = 0.01; Particle() { getPosition(); vel = PVector.random2D(); } void getPosition() { while (loc == null || !isInText(loc)) loc = new PVector(random(width), random(height)); } void update() { vel.rotate(random(-QUARTER_PI, QUARTER_PI)); loc.add(vel); switch(drawMode) { case 0: if (!isInText(loc)) getPosition(); break; case 1: case 2: life -= lifeRate; break; case 3: if (!isInText(loc)) getPosition(); life -= lifeRate; break; } } void display() { fill(255); stroke(0, 125); float r = radius; switch(drawMode) { case 0: break; case 1: case 2: case 3: r *= life; break; } ellipse(loc.x, loc.y, r, r); } boolean isInText(PVector v) { return pg.get(int(v.x), int(v.y)) == PGRAPHICS_COLOR; } } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment