09.04.2016 Views

www.ebook777.com

Make_Getting_Started_with_Processing_Second_Edition

Make_Getting_Started_with_Processing_Second_Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

54 Getting Started with Processing<br />

<strong>www</strong>.<strong>ebook777.com</strong><br />

void setup() {<br />

size(480, 120);<br />

stroke(0, 102);<br />

}<br />

void draw() {<br />

float weight = dist(mouseX, mouseY, pmouseX, pmouseY);<br />

strokeWeight(weight);<br />

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

}<br />

Example 5-8: Easing Does It<br />

In Example 5-7 on page 53, the values from the mouse are converted<br />

directly into positions on the screen. But sometimes you<br />

want the values to follow the mouse loosely—to lag behind to<br />

create a more fluid motion. This technique is called easing. With<br />

easing, there are two values: the current value and the value to<br />

move toward (see Figure 5-1). At each step in the program, the<br />

current value moves a little closer to the target value:<br />

float x;<br />

float easing = 0.01;<br />

void setup() {<br />

size(220, 120);<br />

}<br />

void draw() {<br />

float targetX = mouseX;<br />

x += (targetX - x) * easing;<br />

ellipse(x, 40, 12, 12);<br />

println(targetX + " : " + x);<br />

}<br />

The value of the x variable is always getting closer to targetX.<br />

The speed at which it catches up with targetX is set with the eas<br />

ing variable, a number between 0 and 1. A small value for easing

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

Saved successfully!

Ooh no, something went wrong!