Overview of Processing

This weekend I gave two talks at BarCamp Rochester (which was very well put together and quite enjoyable) – one on jQuery and a very quick one on the Processing language. I’ve deconstructed my slides into some bullet points here. If you’re not familiar with the language, or what it’s capable of, this should give you a good overview.

Processing is a data visualization programming language.

It has three components:

  • The Processing language
  • The Processing drawing API
  • The implementation (in Java – can optionally pass the drawing API through to OpenGL).

The Processing Language and API

  • Strictly typed
  • Has classes, inheritance
  • Includes a bunch of globally-accessible functions (the drawing API – very flat and PHP-like).

Basic Program Structure

Two core methods: setup() and draw()

  • Very OpenGL-like
  • draw() is called continually at a specific framerate (if none is specified then it goes as fast as possible)

Simple example: Drawing a continuous line with the mouse.

void setup() {
  size(200, 200);
  background(102);
}

void draw() {
  stroke(255);
  if(mousePressed) {
    line(mouseX, mouseY, pmouseX, pmouseY);
  }
}

Initialization

  • setup() is called initially
  • size(…) – set the width/height of the drawing area
  • Can include calls to any other number of methods, such as: background(…) – (which draws and fills a background with a specified color).
  • Note: All colors are done in RGBA. background(102) is equivalent to background(102,102,102,255) (opaque gray color)

The draw() loop

  • draw() gets called as fast as possible, unless a frameRate is specified (with framerate(20), for example). You can disable any looping by calling noLoop().
  • stroke() sets color of drawing outline (the color of lines, points, and the outsides of polygons)
  • fill() sets inside color of drawing (inside of polygons)
  • mousePressed is true if mouse is down
  • Very different from typical asynchronous events – since program keeps looping we get state updates automatically. (Unless you specify mousePressed as a global function – then it’ll be called as a callback.)
  • mouseX, mouseY – mouse position, pmouseX, pmouseY – previous mouse position in last draw() call

Drawing

Different drawing methods: line(), rect(), arc(), ellipse(), point(), quad(), triangle(), bezier(), etc.

All use stroke(), strokeWeight(), and fill().

Can also draw complex polygons using beginShape, endShapre, and vertex – like in this example.

  fill(127);
  beginShape();
  for (int i=0; i<segments; i++){
    vertex(ground&#91;i&#93;.x1, ground&#91;i&#93;.y1);
    vertex(ground&#91;i&#93;.x2, ground&#91;i&#93;.y2);
  }
  vertex(ground&#91;segments-1&#93;.x2, height);
  vertex(ground&#91;0&#93;.x1, height);
  endShape(CLOSE);&#91;/js&#93;

<b>The Canvas</b>

Very OpenGL-like. You can mutate the canvas rendering using: translate(), scale(), and rotate().

You can also save and restore the state of the canvas using: pushMatrix() and popMatrix().

A basic example using pushMatrix/popMatrix: <a href="http://processing.org/learning/basics/arm.html">A movable arm</a>.

<b>Classes</b>

Can hold data, do inheritance.

Example: <a href="http://processing.org/learning/topics/reflection2.html">Bouncing an object off of rocky terrain</a>

[js]class Ground {
  float x1, y1, x2, y2, x, y, len, rot;
  Ground(){  }
  Ground(float x1, float y1, float x2, float y2) {
    this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2;
    x = (x1+x2)/2;
    y = (y1+y2)/2;
    len = dist(x1, y1, x2, y2);
    rot = atan2((y2-y1), (x2-x1));
  }
}

Math

A whole mess of math functions are provided, as well: dist(), map(), constrain(), abs(), floor(), ceil(), random(), noise(), atan2(), cos(), sin(), pow(), sqrt(), radians().

Images

Can be used to load in external images. Example: Animation of guy dancing.

int numFrames = 12; // The number of frames in the animation
int frame = 0;
PImage[] images = new PImage[numFrames];
void setup(){
size(200, 200);
frameRate(30);
for(int i=0; iDemos

Some fun demos that I really like:

  • Zipdecode – rendering all zipcodes in the country and searching them in real time.
  • Substrate – rendering a piece of art.
  • Genetic Trees – selectively breed and mutate trees.
  • World is not round – live VJing a song using Processing and a set of physical input controls.

Books

Posted: April 7th, 2008


Subscribe for email updates

9 Comments (Show Comments)



Comments are closed.
Comments are automatically turned off two weeks after the original post. If you have a question concerning the content of this post, please feel free to contact me.


Secrets of the JavaScript Ninja

Secrets of the JS Ninja

Secret techniques of top JavaScript programmers. Published by Manning.

John Resig Twitter Updates

@jeresig / Mastodon

Infrequent, short, updates and links.