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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

As usual, I declare global variables at the top of the program, before the setup() function.<br />

The nodeXPos[] <strong>and</strong> nodeYPos[] arrays hold the shape coordinate data as the shapes are<br />

plotted. The initialNode color variable is used to dynamically set a color on the initial<br />

plotted node when it is moused over—this functions like a rollover effect. (Later in the<br />

chapter, I’ll look at more examples of custom interactive buttons in <strong>Processing</strong>.) The<br />

Boolean variable isShapeClosed maintains the status of the closed state of the shape,<br />

which is important, as animation only occurs when this variable is true. The dynamics variables<br />

(most of which were discussed in Chapter 11) h<strong>and</strong>le the animation. Many of the<br />

variables are arrays, since animation is applied to each individual node. Since it’s not<br />

known how many nodes will be plotted for each shape, these arrays are initialized with<br />

zero length. Later in the sketch, as the user plots a node, values (<strong>and</strong> length) are added to<br />

the array using <strong>Processing</strong>’s append() function.<br />

void setup(){<br />

size(400, 600);<br />

frameRate(30);<br />

smooth();<br />

}<br />

The setup() function is nice <strong>and</strong> simple—hopefully, it doesn’t need any explanation.<br />

void draw(){<br />

// create simple shape trails<br />

fill(255, 36);<br />

rect(0, 0, width, height);<br />

if (nodeXPos.length>1){<br />

// draw line between nodes<br />

drawEdge();<br />

// check if mouse is over original node<br />

checkOverInitialNode();<br />

// animate shape<br />

moveShape();<br />

}<br />

// draw node<br />

drawNode();<br />

}<br />

The draw() function is relatively simple as well, considering the amount of stuff happening<br />

in this sketch. By creating some custom functions, I was able to keep the draw()<br />

function pretty lean. I wrapped the three custom function calls (drawEdge(),<br />

checkOverInitialNode(), <strong>and</strong> moveShape()) in a conditional block to ensure that at least<br />

two nodes are plotted prior to connecting the nodes with lines. drawEdge() calls a function<br />

that h<strong>and</strong>les the drawing of the lines between the nodes. I called this function prior to<br />

actually drawing the nodes so that the nodes appear on top of the connecting lines.<br />

checkOverInitialNode() checks if the mouse is over the initial node, allowing the color<br />

of the node to be changed. moveShape() animates the shape after it’s closed. Below the<br />

conditional block, I called drawNode(), which, as you might suspect, draws the nodes.<br />

INTERACTIVITY<br />

575<br />

12

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

Saved successfully!

Ooh no, something went wrong!