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 />

580<br />

float y = 30;<br />

float w = 10;<br />

float h = w;<br />

float xSpeed = 0;<br />

void setup(){<br />

size(600, 100);<br />

btnX = width/2;<br />

btnY = height-20;<br />

btnW = 50;<br />

btnH = 20;<br />

}<br />

void draw(){<br />

background(200);<br />

rectMode(CENTER);<br />

fill(250);<br />

stroke(150);<br />

rect(btnX, btnY, btnW, btnH);<br />

fill(0);<br />

noStroke();<br />

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

x+=xSpeed;<br />

}<br />

void mousePressed(){<br />

if (mouseX>btnX-btnW/2 && mouseX< btnX+btnW/2 && ➥<br />

mouseY>btnY-btnH/2 && mouseY< btnY+btnH/2){<br />

xSpeed+=.2;<br />

}<br />

}<br />

In this sketch, each time the mouse is pressed on the rectangle at the bottom of the<br />

screen, the smaller square’s speed increases by .02. The mouse detection is now working<br />

based on the bounds of the button. However, since no feedback is provided, the user<br />

might not underst<strong>and</strong> that the sketch is working (or even that the rectangle is supposed to<br />

be a button). You could improve the sketch by giving the user some more visual cues, such<br />

as changing the cursor icon during detected mouse events, adding different rendering<br />

states to the button (up, over, <strong>and</strong> down), giving the button a label, <strong>and</strong> providing some<br />

output based on the current speed of the moving square. The following sketch is a modified<br />

version of the last that h<strong>and</strong>les these improvements in two steps. The new <strong>and</strong> altered<br />

code is shown in bold.<br />

// Interactive Button (step 1)<br />

// button<br />

float btnX, btnY, btnW, btnH;

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

Saved successfully!

Ooh no, something went wrong!