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.

initialize x, y<br />

x = width/2.0;<br />

y = height/2.0;<br />

frameRate(30);<br />

smooth();<br />

noStroke();<br />

}<br />

void draw(){<br />

// fading background<br />

fill(0, 5);<br />

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

fill(255);<br />

ellipse(x, y, r*2, r*2);<br />

}<br />

void mouseDragged() {<br />

x = mouseX;<br />

y = mouseY;<br />

}<br />

This example works similarly to the earlier mousePressed Boolean variable example. When<br />

you press the mouse in the display window <strong>and</strong> drag, the ellipse follows the mouse.<br />

The next example adds a mouseMoved() function that controls a jitter <strong>and</strong> a scatter<br />

variable, which are set as you move the mouse around the display window (without it<br />

being pressed). The scatter variable controls the limit on a for loop, while the jitter<br />

variable controls the radius on a ring of r<strong>and</strong>om ellipses created in the for loop. Upon<br />

dragging the mouse (i.e., with the button pressed), the ring of ellipses will follow the cursor<br />

using the last jitter <strong>and</strong> scatter settings set. This is because mouse dragging <strong>and</strong><br />

mouse moving, similarly to the press <strong>and</strong> release events, are processed as unique mouse<br />

event states—you can’t press <strong>and</strong> release at the same time, nor can you move <strong>and</strong> drag at<br />

the same time.<br />

// mouseMoved Example<br />

float x, y;<br />

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

float r = 1.5;<br />

float jitter = 5;<br />

float scatter = 1;<br />

void setup(){<br />

size(400, 400);<br />

// initialize x, y<br />

x = width/2.0;<br />

y = height/2.0;<br />

frameRate(30);<br />

smooth();<br />

noStroke();<br />

INTERACTIVITY<br />

569<br />

12

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

Saved successfully!

Ooh no, something went wrong!