04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Continuous mode<br />

In continuous mode, users build upon what they’ve learned in basic mode, with the edition<br />

of code structures called functions <strong>and</strong> classes. As you recall, functions are the main building<br />

blocks used in procedural programming, <strong>and</strong> are simply groupings of lines of code that<br />

execute only when they are explicitly called. Classes are much more complicated structures<br />

than functions, <strong>and</strong> are utilized in OOP. I’ll cover classes in detail in Chapter 8. In<br />

basic mode, code is executed linearly (line by line). Functions <strong>and</strong> classes, on the other<br />

h<strong>and</strong>, allow code to be executed nonlinearly. These structures are only executed when<br />

they are explicitly called, not when they are initially read into memory.<br />

In continuous mode, two basic <strong>Processing</strong> functions are provided, which allow you to add<br />

your own custom structures (functions or classes). The two functions are as follows:<br />

void setup(){<br />

}<br />

void draw(){<br />

}<br />

The setup() function is called only once, at the start of the program. This is the place<br />

where you normally initialize variables that will be used later in the program. Adding the<br />

setup() function to your sketch allows you to add your own additional custom functions<br />

<strong>and</strong> classes. The draw() function adds animation capabilities to your sketch <strong>and</strong> has a<br />

built-in loop, more accurately called a thread or timer. By default, adding the draw() function<br />

to your sketch causes any code between the curly braces of this structure to continuously<br />

execute. There are a number of ways of controlling this behavior, which I’ll cover in<br />

Chapter 11. You can also read more about <strong>Processing</strong>’s draw() function in Appendix A.<br />

The following sketch example is built in continuous mode <strong>and</strong> uses two custom functions,<br />

fadeScreen() <strong>and</strong> paintOutlines(). The sketch is a little drawing program. I’ve added<br />

comments throughout the sketch to explain the program flow, although I don’t necessarily<br />

expect you to fully grasp the code. The sketch allows you to draw by moving <strong>and</strong> also<br />

dragging the mouse (holding down the mouse button as you move the mouse). A screenshot<br />

of the output of the sketch is shown in Figure 5-11.<br />

/*<br />

Drawing Shapes<br />

Ira Greenberg, November 2, 2005<br />

*/<br />

//declare global variables<br />

float radiusX, radiusY;<br />

//controls rate of screen fade<br />

int screenFadeValue = 20;<br />

//controls maximum size of shape<br />

int sizeMax = 30;<br />

THE PROCESSING ENVIRONMENT<br />

159<br />

5

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

Saved successfully!

Ooh no, something went wrong!