Blog


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[i].x1, ground[i].y1);
    vertex(ground[i].x2, ground[i].y2);
  }
  vertex(ground[segments-1].x2, height);
  vertex(ground[0].x1, height);
  endShape(CLOSE);

The Canvas

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 movable arm.

Classes

Can hold data, do inheritance.

Example: Bouncing an object off of rocky terrain

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; i<numFrames; i++) {
    String imageName = "PT_anim" + nf(i, 4) + ".gif";
    images[i] = loadImage(imageName);
  }
} 
void draw() {
  frame = (frame+1)%numFrames;
  image(images[frame], 0, 0);
}

Demos

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

Tags: graphics, processing, data, visualization, barcamp

BarCampNYC Wrap-up

As I mentioned previously, I attended BarCampNYC this weekend and had a great time. I did two full presentations - both of which were quite successful.

Presentation 1: Hot Demo Session (11am, Sunday)
I started out with a quick overview of my company, Juniper Bay, what I do (programming, entrepreneur) and where I'm looking to go (have my own company running by the time I graduate).

I then did a quick overview and demo of ideaShrub: Real-time Idea Collaboration. The web site managed to die while I was doing the demo, so that kind of fell through (unfortunately). ideaShrub is still in a closed beta, and I am still actively developing on it. I'm planning on making a public release 'soon'.

I then announced a brand new product: Feed Pile: Feed Sharing for Everyone. The premise for the site is very similar to SuprGlu, but is targeted more towards aggregating for your friends/clubs/family and sharing with them. It supports a bunch of neat features: No accounts, OPML, and Autodiscovery. It's super-simple to use - give it a try!

Finally, I announced my second new release of the evening: jQuery: New Wave Javascript. In a nutshell, this code revolutionizes the way you can get Javascript to interact with HTML - it really is an amazing set of code, and I've dumped a lot of time and effort into getting it right. I'm working on the documentation for the site, right now - which should be ready within the next couple days.

Presentation 2: Subverting Social Networks (4:45pm, Sunday)
I gave this particular presentation with Eric Skiff - we were planning on giving similar presentations and decided to merge our efforts, to save time and effort. The reception to the talk seemed to be overwelmingly positive - and really opened the eyes of a lot of people.

Eric released the source code to his MySpace Friends Adder, so if you're interested in checking out some of the code, head on over there.

Additionally, I've posted the slides that we used for the presentation. They may seem rather sparten - but that's because we talked a lot more then we read. If you're interested in the subject matter, feel free to comment or send us an email.

Some people have posted photos of this particular presentation to Flickr, here's one and another.

People/Companies
Finally, I'd like to thank all the great people/companies that I got to know there: GlitchCast, SuprGlu, Simpy, Squidoo, Peter Nofelt, Good Experience, Bitty Browser, Joyent, Jolt, and HorsePigCow.

Update: A basic outline for the presentation can be found on the BarCamp Wiki.

Tags: barcamp, javascript, barcampnyc, rss, web2.0

JavaScript Books

Secrets of the JavaScript Ninja

JavaScript Secrets

Secret techniques of top JavaScript programmers.

Pro JavaScript Techniques

Pro JavaScript

The best techniques for professional JavaScript. Published by Apress.

Micro Updates

John Resig Twitter Updates

@jeresig

Infrequent, short, updates and links.

JavaScript Jobs



Hosting provided by: Ruby Hosting by Engine Yard