06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

400 <strong>Learning</strong> <strong>Processing</strong><br />

// Draw some stuff!<br />

background(255);<br />

fill(175);<br />

stroke(0);<br />

ellipse(width/2,height/2,160,160);<br />

// Very important, required for the PDF <strong>to</strong> render properly<br />

exit();<br />

If you run this example, you will notice that no window appears. Once you have set the <strong>Processing</strong><br />

rendering mode <strong>to</strong> PDF, the sketch window will no longer appear. Th is is because one often uses PDF<br />

mode <strong>to</strong> create extremely high-resolution, complex images that cannot easily be displayed onscreen.<br />

However, it is possible <strong>to</strong> see the <strong>Processing</strong> sketch window while rendering a PDF using the functions<br />

beginRecord( ) and endRecord( ) . Th is runs slower than the fi rst example, but allows you <strong>to</strong> see what is<br />

being saved.<br />

Example 21-2: PDF using beginRecord( )<br />

import processing.pdf.*;<br />

void setup() {<br />

size(400, 400);<br />

beginRecord(PDF, " filename.pdf " );<br />

}<br />

void draw() {<br />

// Draw some stuff!<br />

smooth();<br />

background(100);<br />

fill(0);<br />

stroke(255);<br />

ellipse(width/2,height/2,160,160);<br />

endRecord();<br />

noLoop();<br />

}<br />

endRecord( ) does not have <strong>to</strong> be called on the fi rst frame rendered, and therefore this mode can be used<br />

<strong>to</strong> generate a PDF compiled from multiple cycles through draw( ) . Th e following example takes the<br />

“ Scribbler ” program from Chapter 16 and renders the result <strong>to</strong> a PDF. Here the colors are not taken from<br />

a video stream, but are picked based on a counter variable. See Figure 21.3 for a sample output.<br />

Example 21-3: Multiple frames in<strong>to</strong> one PDF<br />

import processing.pdf.*;<br />

float x � 0;<br />

float y � 0;<br />

beginRecord( ) starts the process.<br />

The fi rst argument should read PDF<br />

and the second is the fi lename.<br />

endRecord( ) is called <strong>to</strong> fi nish<br />

the PDF.<br />

There’s no reason <strong>to</strong> loop any more<br />

since the PDF is fi nished.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!