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

598<br />

nozzleRadius+r<strong>and</strong>om(sprayFeather);<br />

py = mouseY-nozzleRadius*2+sin(radians(strokeAngle))* ➥<br />

nozzleRadius+r<strong>and</strong>om(sprayFeather);<br />

px2 = pmouseX-nozzleRadius*2+cos(radians(strokeAngle))* ➥<br />

nozzleRadius+r<strong>and</strong>om(sprayFeather);<br />

py2 = pmouseY-nozzleRadius*2+sin(radians(strokeAngle))* ➥<br />

nozzleRadius+r<strong>and</strong>om(sprayFeather);<br />

line(px, py, px2, py2);<br />

strokeAngle+=1;<br />

}<br />

}<br />

//eraser<br />

else if (isEraserSelected){<br />

stroke(canvasBg);<br />

strokeWeight(sliderValue*4);<br />

line(mouseX, mouseY, pmouseX, pmouseY);<br />

}<br />

}<br />

From within draw(), the value of <strong>Processing</strong>’s built-in mousePressed Boolean variable can<br />

be checked. As I already discussed, <strong>Processing</strong> also has a mousePressed() function, which<br />

I use a bit later in the sketch <strong>and</strong> outside of the draw() function. In the preceding code, I<br />

used an outer if clause to check if the mouse is pressed over the drawing canvas. If this is<br />

true, the nested if clauses check which tool is currently selected. If the pencil tool is<br />

selected, then the process is pretty straightforward: a stroke color <strong>and</strong> stroke weight are<br />

set, <strong>and</strong> <strong>Processing</strong>’s line() function is called. The line() function uses <strong>Processing</strong>’s<br />

mouse <strong>and</strong> pmouse x <strong>and</strong> y variables: line(mouseX, mouseY, pmouseX, pmouseY);. mouseX<br />

<strong>and</strong> mouseY are the current mouse coordinates, while pmouseX <strong>and</strong> pmouseY are the mouse<br />

x <strong>and</strong> y coordinates one frame prior to the current one. These are very convenient variables<br />

that work great for allowing you to easily simulate a drawing tool. Skipping the brush<br />

tool for a moment, the eraser tool works identically to the pencil tool—only the stroke is<br />

a little wider <strong>and</strong> the stroke color is always the color of the canvas background.<br />

The brush code is a bit more complicated: as the brush is dragged, a series of lines are<br />

drawn somewhat r<strong>and</strong>omly around the mouse position, giving a brushy look to the mark.<br />

I used the trig functions to generate the points around the current mouse position. You’ve<br />

looked at code like this in numerous other places in the book, as well as earlier in the<br />

chapter. The cos() function is used to get the x coordinate <strong>and</strong> sin() is used to get the y<br />

coordinate. The expression nozzleRadius+r<strong>and</strong>om(sprayFeather); controls how far the<br />

lines are drawn around the mouse position. You’ll notice that I subtracted nozzleRadius*2<br />

at the beginning of each of the px, py, px2, <strong>and</strong> py2 lines. I did this so that the cursor would<br />

appear in the center of the paint mark, not at the top-left corner. Try removing<br />

nozzleRadius*2 from each of the lines if you want to see how the cursor shifts. When<br />

painting with the brush, if you drag it quickly, the brush marks will straighten out, which is<br />

a nice (unexpected) effect, thanks again to the pmouseX <strong>and</strong> pmouseY variables.<br />

//slider dragging<br />

if (isSliderMovable && mousePressed && mouseX > ➥<br />

sliderBar[0] && mouseX

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

Saved successfully!

Ooh no, something went wrong!