Multitouch Sparks

This sketch has been adapted from a sketch done by Adrian Park. If you have a multitouch screen then you can make sparks appear at any point of contact, otherwise sparks will appear around the mouse pointer.

[processing] // Multitouch Sparks // Adrian Park // 2012 ArrayList mouseTrail; int prevMouseX; int prevMouseY; // if multitouch events are fired, this is set to true boolean useMultiTouch = false; void setup() { size( 655 , 630 ); background( 0 ); smooth(); mouseTrail = new ArrayList(); } void draw() { background( 0 ); if( !useMultiTouch ) { // respond to mouse movement if( mouseX != prevMouseX || mouseY != prevMouseY ) { // create a spark if the mouse is moving Disc newDisc = new Disc( mouseX, mouseY ); mouseTrail.add( newDisc ); } prevMouseX = mouseX; prevMouseY = mouseY; } updateDiscs(); } // instructs each spark to draw itself and removes extinguished sparks void updateDiscs() { for( int i = mouseTrail.size() - 1; i > 0; i-- ) { Disc d = (Disc) mouseTrail.get(i); if( !d.isAlive ) { mouseTrail.remove(i); } d.render(); } } // respond to multitouch events // COMMENT THIS OUT TO MAKE APP COMPILE IN PROCESSING IDE //*/ void touchMove(TouchEvent touchEvent) { useMultiTouch = true; for (int i = 0; i < touchEvent.touches.length; i++) { int x = touchEvent.touches[i].offsetX; int y = touchEvent.touches[i].offsetY; Disc newDisc = new Disc( x, y ); mouseTrail.add( newDisc ); } } //*/ // Multitouch Sparks // Adrian Park // 2012 class Disc{ int discRadius; int minRadius = 2; int maxRadius = 10; float discAlpha = 255.0; float alphaIncrement = 5; float xPos; float yPos; float xVel; float yVel; float friction = 0.98; float gravity = 0.1; int maxStartingVelocity = 8; // colour library color[] colours = new color[3]; color c1 = color(255, 204, 204); color c2 = color(255, 255, 204); color c3 = color(204, 255, 204); color c4 = color(204, 255, 255); color c5 = color(204, 204, 255); color c6 = color(255, 204, 255); int renderColourIndex; color renderColour; // false when alpha <= 0 boolean isAlive = true; Disc( int x, int y ) { discRadius = round( random( minRadius, maxRadius ) ); PVector vector = getRandomVector(); xVel = vector.x; yVel = vector.y; xPos = x; yPos = y; colours[0] = c1; colours[1] = c2; colours[2] = c3; colours[2] = c4; colours[2] = c5; colours[2] = c6; renderColourIndex = round( random(0, colours.length-1 ) ); } // draw the spark to screen void render() { if( isAlive ) { renderColour = getColour(); fill( renderColour ); noStroke(); ellipse( xPos, yPos, discRadius*2, discRadius*2 ); // update properties discAlpha -= alphaIncrement; xPos += xVel; yPos += yVel; xVel = xVel * friction; yVel = (yVel * friction) + gravity; if( discAlpha <= 0 ) { isAlive = false; } } } // randomly chooses a colour from the colour library color getColour() { color randomColour = colours[ renderColourIndex ]; color newColour = color( red(randomColour), green(randomColour), blue(randomColour), discAlpha ); return newColour; } // calculate a random direction and speed for the spark PVector getRandomVector() { float angle = random(0, 2) * PI; float velocity = random( 0, maxStartingVelocity ); float x = cos(angle) * velocity; float y = sin(angle) * velocity; PVector vector = new PVector( x, y ); return vector; } } [/processing]
// Multitouch Sparks
// Adrian Park <http://www.adrianpark.com>
// 2012

ArrayList mouseTrail;

int prevMouseX;
int prevMouseY;

// if multitouch events are fired, this is set to true
boolean useMultiTouch = false;

void setup() {
  size( 655 , 630 );
  background( 0 );
  smooth();
  mouseTrail = new ArrayList();
}

void draw() {
  background( 0 );
 
  if( !useMultiTouch ) {
    // respond to mouse movement
   
    if( mouseX != prevMouseX || mouseY != prevMouseY ) {
      // create a spark if the mouse is moving
      Disc newDisc = new Disc( mouseX, mouseY );
      mouseTrail.add( newDisc );
    }
   
    prevMouseX = mouseX;
    prevMouseY = mouseY;
   
  }
 
  updateDiscs();
 
}

// instructs each spark to draw itself and removes extinguished sparks
void updateDiscs() {
 
  for( int i = mouseTrail.size() - 1; i > 0; i-- ) {
    Disc d = (Disc) mouseTrail.get(i);
   
    if( !d.isAlive ) {
      mouseTrail.remove(i);
    }
   
    d.render();
  }
}

// respond to multitouch events
// COMMENT THIS OUT TO MAKE APP COMPILE IN PROCESSING IDE
//*/
void touchMove(TouchEvent touchEvent) {
  useMultiTouch = true;

  for (int i = 0; i < touchEvent.touches.length; i++) {
    int x = touchEvent.touches[i].offsetX;
    int y = touchEvent.touches[i].offsetY;
   
    Disc newDisc = new Disc( x, y );
    mouseTrail.add( newDisc );
  }
}
//*/
// Multitouch Sparks
// Adrian Park <http://www.adrianpark.com>
// 2012

class Disc{
 
  int discRadius;
  int minRadius = 2;
  int maxRadius = 10;
  float discAlpha = 255.0;
  float alphaIncrement = 5;
  float xPos;
  float yPos;
  float xVel;
  float yVel;
  float friction = 0.98;
  float gravity = 0.1;
  int maxStartingVelocity = 8;
 
  // colour library
  color[] colours = new color[3];
  color c1 = color(255, 204, 204);
  color c2 = color(255, 255, 204);
  color c3 = color(204, 255, 204);
  color c4 = color(204, 255, 255);
  color c5 = color(204, 204, 255);
  color c6 = color(255, 204, 255);
 
  int renderColourIndex;
  color renderColour;
 
  // false when alpha <= 0
  boolean isAlive = true;
 
  Disc( int x, int y ) {
   
    discRadius = round( random( minRadius, maxRadius ) );
   
    PVector vector = getRandomVector();
    xVel = vector.x;
    yVel = vector.y;
   
    xPos = x;
    yPos = y;
   
    colours[0] = c1;
    colours[1] = c2;
    colours[2] = c3;
    colours[2] = c4;
    colours[2] = c5;
    colours[2] = c6;
   
    renderColourIndex = round( random(0, colours.length-1 ) );
  }
 
  // draw the spark to screen
  void render() {
   
    if( isAlive ) {
      renderColour = getColour();
     
      fill( renderColour );
      noStroke();
     
      ellipse( xPos, yPos, discRadius*2, discRadius*2 );
     
      // update properties
      discAlpha -= alphaIncrement;
      xPos += xVel;
      yPos += yVel;
     
      xVel = xVel * friction;
      yVel = (yVel * friction) + gravity;
     
      if( discAlpha <= 0 ) {
        isAlive = false;
      }
    }
  }
 
  // randomly chooses a colour from the colour library
  color getColour() {
   
    color randomColour = colours[ renderColourIndex ];
    color newColour = color( red(randomColour), green(randomColour), blue(randomColour), discAlpha );
    return newColour;
  }
 
  // calculate a random direction and speed for the spark
  PVector getRandomVector() {
    float angle = random(0, 2) * PI;
    float velocity = random( 0, maxStartingVelocity );
    float x = cos(angle) * velocity;
    float y = sin(angle) * velocity;
    PVector vector = new PVector( x, y );
    return vector;
  }
}

Read More