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.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

568<br />

y = height/2.0;<br />

rectMode(CENTER);<br />

}<br />

void draw(){<br />

background(255);<br />

fill(rectColor);<br />

rect(x, y, w, h);<br />

}<br />

void mousePressed() {<br />

x = mouseX;<br />

y = mouseY;<br />

}<br />

// r<strong>and</strong>omly change fill color upon mouse release<br />

void mouseReleased() {<br />

rectColor = color(r<strong>and</strong>om(255), r<strong>and</strong>om(255), r<strong>and</strong>om(255));<br />

}<br />

In this example, I included the original mousePressed() function <strong>and</strong> added a<br />

mouseReleased() function. When you run the sketch, you’ll see that <strong>Processing</strong> can detect<br />

both parts of the mouse click (the press <strong>and</strong> release) as separate events. The<br />

mouseReleased() function creates a r<strong>and</strong>om color for the rectangle’s fill.<br />

If you’re a detail-oriented person (I’m really not), you may have noticed some inconsistency<br />

with <strong>Processing</strong>’s automatic syntax color highlighting in the text editor. In<br />

general, <strong>Processing</strong>’s built-in functions turn orange, while built-in variables turn red.<br />

However, the mousePressed() function turns red, <strong>and</strong> the mouseClicked() function<br />

remains black. The other mouse event functions, mouseReleased(), mouseMoved(),<br />

<strong>and</strong> mouseDragged(), turn orange. This is definitely confusing, <strong>and</strong> I’ve seen comments<br />

on the <strong>Processing</strong> discussion board saying so as well. Perhaps at some point this<br />

minor issue will be addressed; for now, just realize that it’s not something you’re doing<br />

wrong, <strong>and</strong> that the color of your code in the text editor has no effect on the functionality<br />

of the code. (It’s also possible that by the time you read this, the issue will<br />

have been resolved.)<br />

The mouseMoved() <strong>and</strong> mouseDragged() functions work similarly to mousePressed() <strong>and</strong><br />

mouseReleased(). Here’s a mouseDragged() example:<br />

// mouseDragged Example<br />

float x, y;<br />

// declare <strong>and</strong> initialize radius<br />

float r = 5.0;<br />

void setup(){<br />

size(400, 400);

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

Saved successfully!

Ooh no, something went wrong!