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.

ectBackground = color(255, 0, 0);<br />

}<br />

else if (key == 's') {<br />

rectBackground = color(255, 255, 0);<br />

}<br />

else if (key == 'w') {<br />

rectBackground = color(0, 255, 0);<br />

}<br />

else if (key == 'z') {<br />

rectBackground = color(255, 0, 255);<br />

}<br />

}<br />

Finally, instead of using the keyPressed() function, you can use a keyPressed Boolean<br />

variable directly within the draw() function. We already looked at this multiple approach<br />

to solving a problem earlier in the chapter, when I discussed the mousePressed() function<br />

vs. the mousePressed Boolean variable. Here’s the code:<br />

color rectBackground = color(0, 0, 255);<br />

void setup(){<br />

size(400, 400);<br />

background(255);<br />

rectMode(CENTER);<br />

}<br />

void draw(){<br />

fill(rectBackground);<br />

rect(width/2, height/2, 200, 200);<br />

// keyPressed boolean variable<br />

if (keyPressed){<br />

if(key == 'a'){<br />

rectBackground = color(255, 0, 0);<br />

}<br />

else if (key == 's') {<br />

rectBackground = color(255, 255, 0);<br />

}<br />

else if (key == 'w') {<br />

rectBackground = color(0, 255, 0);<br />

}<br />

else if (key == 'z') {<br />

rectBackground = color(255, 0, 255);<br />

}<br />

}<br />

}<br />

I’ve included one more somewhat lengthy interactive example that takes advantage of<br />

<strong>Processing</strong>’s easy-to-use key event detection (see Figure 12-4). The piece is a motion painting<br />

application that uses an animated brush, which is completely controlled by the keyboard.<br />

I’ve killed enough trees this chapter, so I’ve added my descriptions about the sketch<br />

INTERACTIVITY<br />

605<br />

12

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

Saved successfully!

Ooh no, something went wrong!