/** a spark generator by guru press 'g' to toggle gravity */ import java.util.ArrayList; import java.util.List; List particles; boolean g; void setup() { size(600,300,P2D); particles = new ArrayList(); } void draw() { background(0); fill(255); for( int i =0; i < particles.size(); i++) { Particle p = (Particle)particles.get(i); if (!p.isDead()) { p.update(); p.draw(); } } } void mouseDragged() { if (mousePressed ==true ) { particles.add( new Particle( new PVector( mouseX, mouseY ), new PVector(random(-1,1),random(-1,1)), 30, g)); } } void keyPressed() { if (key == 'g') { g = !g; } } public class Particle { PVector p; PVector s; int ttl; boolean g; PImage img; public Particle( PVector p, PVector s, int ttl, boolean g) { this.p = p; this.s = s; this.g = g; this.ttl = ttl; img = makeTexture(5); } public void update() { if ( ttl > 0 ) { ttl--; p.add( s ); if ( g ) { s = new PVector( s.x, s.y + .1 ); } } } public void draw() { if ( !isDead()) { blend( img, 0,0, img.width, img.height, int(p.x - img.width/2), int(p.y - img.height/2), img.width, img.height, ADD ); } } public boolean isDead() { return ttl <= 0; } PGraphics makeTexture( int r ) { PGraphics res = createGraphics( r * 6, r * 6, P2D); res.beginDraw(); res.loadPixels(); for ( int x = 0; x < res.width; x++) { for( int y = 0; y < res.height; y++ ) { float d = min( 512, 50* sq( r / sqrt( sq( x - 3 * r) + sq( y - 3 * r)))); //if ( d < 10 ) d = 0; res.pixels[y * res.width + x] = color( min(255,d), min(255, d*0.8), d* 0.5 ); } } res.updatePixels(); res.endDraw(); return res; } }