Offscreen drawing
In some cases, you want to be able to draw things on a blank image, before drawing it to the screen. This can be easily done in Processing with the PGraphics
object.
How to do it...
The first thing you need to do is declare a PGraphics
object at the beginning of your sketch, and initialize it with the createGraphics()
function inside setup()
. I've added the x
and y
variable to add some animation to the sketch. You can clear the background by clicking the mouse.
PGraphics pg; float x; float y; void setup() { size( 640, 480 ); smooth(); pg = createGraphics( 64, 64, JAVA2D ); background( 255 ); imageMode( CENTER ); x = 0; y = 0; }
The first thing we'll do inside the draw()
function is draw some lines onto the PGraphics
object. The object will then be drawn to the screen using the image()
function. The last piece of code inside the draw function is used to calculate the x
and y
values to animate the sketch.
void draw() { pg.beginDraw(); pg.background( 255...