Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Working with easing

From: Interactive Data Visualization with Processing

Video: Working with easing

One of the more interesting dynamic tricks that you can do in Processing is to have an object follow your mouse. Now, this is pretty easy to do, because when you specify, for instance, the center of an ellipse, instead of using X and Y, you can simply use the built-in Processing commands mouseX and mouseY. However, the problem with that, they end of following too quickly, and it doesn't seem natural. It's a little jerking in motion. Instead you can use an adaptation of this that's known as easing. Now, there's not a built-in function for easing.

Working with easing

One of the more interesting dynamic tricks that you can do in Processing is to have an object follow your mouse. Now, this is pretty easy to do, because when you specify, for instance, the center of an ellipse, instead of using X and Y, you can simply use the built-in Processing commands mouseX and mouseY. However, the problem with that, they end of following too quickly, and it doesn't seem natural. It's a little jerking in motion. Instead you can use an adaptation of this that's known as easing. Now, there's not a built-in function for easing.

It's something you have to code, but it's a simple adaptation, and it allows things to gradually follow with some lag where the mouse, for instance, is going. And that's what I'm going to show you in this particular sketch. Put a comment in with the name. Then I'm going to bring it in a color palette from the palette text file. I'll use this one down here at the bottom. Then what I am going to do is I am going to do kind of an interesting thing for the way we set these things up. For easing there's a lot of variables that need to be set up ahead of time, so I am going to go through this. The first one is I'm going to create a variable for the width of the window.

Now, I can use the word width, but I find it easier to just use a related one. I can say "wide" and then I can refer back to it. So I'm going to do 600 pixels right here. I can do int and then high, and I'll do 200, because I am going to using those a little bit later. Then what I want is I want a variable for the diameter of one of the ellipses I am going to be drawing. That's going to be a 20. Then I need a series of floating variables, because what I am going to be drawing in this is one ellipse and then three rings that are following it around.

To get the rings, what I need to do is this. I need to create a floating variable for the X coordinate for the first one, and it's going to start in a random position. And then I need to do the same thing for its Y variable, so I'm in a create y1, and that's going to be at random(high). And then I am going to create a variable that's used in calculating the easing, or the degree to which there is a lag in following the mouse.

So what I am going to do is I create float, and then I will just simply call it easing1. And I put this little coefficient in here, which I'm choosing as (.1), and you'll see how you can change the coefficients and it affects the degree of easing that you have. But right now I am going to use this small number. Now I have three rings that are going to be chasing the circle. So what I can do is I can just copy this code and I can change x1, keep both as 2s, and just change the numbers in here as I go through, and that facilitates some of the code.

I am also going to use different values for easing. So the middle one I am going to change from 01 to 03, and the last one I am going to change to 05. Now what I'm going to do is I am going to into the setup block. So these are all global variables that I have created ahead of time. The first thing I am going to do is I am going to set the size of the window, and because I created these global variables that corresponded to width and height, I can simply use those ones over again, so in case I change those values at the top, the actual size of the window will follow it.

I am going to turn on the anti-aliasing, and I am going to put a strokeWeight of 3 pixels. Now, actually, I am going to show you what the drawing looks like with the default cursor beforehand, and I'll show you some of the options for dealing with that. Now what I need to do is I need to go down to draw block. This one gets a little long, because I need to repeat the code for the easing of each of the variables. The first thing I'm going to do is I'm going to draw a dot that that the rings will be chasing.

So I'll just call that one Dot and that's where the cursor is going to be. For that one I don't an outline; I just want it to be a red dot. So it's going to be no stroke, and its fill is going to come from the palette. It's going to be color number 4, which is a dark red. And then I use the ellipse, and this one stays right on the mouse, by using these built-in variables mouseX mouseY and d and d, our global variables for the diameter that I defined earlier.

Now let me show you what we can do right now. This will draw at the moment just showing following the mouse. Now, what I haven't done is I'm not refreshing the background. So you just see a trail of where the mouse has been. I can close that now. So one of the things I am going to do is I am going to come back up to the top of draw and I am going to put in background, so that will refresh now, every time we draw. And that's going to be the first color and now when I do that, you'll see that we don't have a trail.

It just follows the mouse. Now I can move quickly, but it ends up right where the mouse is, very quickly. Now there's one problem here, and that is you saw that the arrow was right on top of the mouse. It's a little intrusive. I don't really want it right there. Processing gives us a few options on how we deal with the cursor, and I am going to come back up to setup for just a moment and show you what some of those options are. There is a cursor function, and the default cursor is the arrow. That's one we have been using, but it gets in the way.

I just want to show you what the other options are for right now. I've got several options. So I'll put a few of these down here. The next one is across. The one after that is a hand. The one after that there is text and there is weight. I'll delete these last two. We don't need those. And here's what happens is I go through each of these and I can comment them out and look at the effect of the others. So you remember what it was like with the arrow right on top of the red dot.

Now when I run it, I have that cross. Slightly less intrusive, but still, it seems more like a design element and it's in the way. So I can comment that one out and I can try the hand. It looks like a cute little thing. Now I'm dragging it around with my finger, but still, it seems like it's an intrusive design element. I don't want that. I can use text. Now, what this means is it brings in the IBM cursor that's used for inserting text. So, less intrusive, still not what I want.

And if you want to mess with people, you can use WAIT, which is otherwise known as the spinning beachball of death, as you go through, but that really could just serves to cause anxiety in people. So I am not going to use that one. I do have one other option though, and this is the one that I want to use for this one, and that is no cursor at all. So what this does is it simpl--there is no cursor, but since the red dot follows the cursor, the red dot itself becomes the cursor, and that is a much better way of doing this, because then I don't have this extraneous element that's mucking it up.

So now I've got that one figured out. I have got my dot that follows the mouse, and it is itself the cursor. Now we're going to add some repetitive code for three rings to chase the dot around. I actually am going to put them before the dot, because I want them to be underneath it. We will put Circle 1 here, and here is how I am going to draw this one. I am going to put stroke(palette), noFill. Now, it's going be in the ellipse. Put x1, y1 and d+15.

I want it to be slightly larger than the original dot. Then we're going to add this thing that calculates the easing. Now this ellipse is going to start at a random position on the screen, and what I am going to do is I am going to update it so that every time--so it's going to say the new value of excellent is going to add on a fraction of the difference between the target, which is the mouse, and the current position. So what we do is we do mouse x-x1, so that gets the difference between the mouse and the current position, and then it multiplies times this little coefficient that we put in earlier, and that's the easing number.

So for number of Circle 1, the number was .01, and what that means is it's only going to approach it a little bit each time it loops through. But it loops through 60 times a second, so it will get there pretty quickly. Then I can copy that and then I do a similar thing for y. Just need to hit that back up in the right place, put this one down, and change those Xs to Ys. Great. So that will do the will do the first circle.

Now what I am going to do is I am going to copy this code and I am going to paste it and do it for circles 2 and 3. All I need to do is go through and change the numbers. x2, y2. I am going to make this one a little bit smaller. So instead of adding 15 pixels onto the diameter, I'll only add 10. It has its own value of easing, and we refer back to its own values here. Great! Then I am going to come down to Circle 3 and do the same thing.

This one is going to be, again, a little smaller. Nearly there. Okay that's it. So now what we have here is a rather long string of code, but a lot of it is just copy and paste and adapting for the three different circles, and here's what it looks like when we run it. I am sure I am missing a semicolon right there. There we go. So thank you, there, for my error message.

Now I will run it again, and this time you can see how the three circles chase the red dot at different speeds, and if I leave the dot stationary, they will all catch up eventually, gradually, gradually, until I move it. Anyhow, the nice thing about easing is it provides a more organic response to movement as opposed to the immediate responsiveness of the mouse, and this can be a nice way of depicting change over time in a way that's a little easier for people to perceive and understand.

Show transcript

This video is part of

Image for Interactive Data Visualization with Processing
Interactive Data Visualization with Processing

72 video lessons · 12251 viewers

Barton Poulson
Author

 
Expand all | Collapse all
  1. 3m 16s
    1. Welcome
      58s
    2. What you should know
      1m 22s
    3. Using the exercise files
      56s
  2. 11m 51s
    1. Overview of data visualization
      11m 51s
  3. 11m 53s
    1. Installing Processing
      3m 38s
    2. Overview of Processing
      4m 5s
    3. Exploring libraries
      4m 10s
  4. 1h 1m
    1. Basic setup
      7m 31s
    2. Drawing points
      4m 37s
    3. Drawing lines
      5m 6s
    4. Drawing ellipses and circles
      5m 24s
    5. Drawing arcs
      6m 54s
    6. Drawing rectangles and squares
      4m 58s
    7. Drawing quadrangles
      3m 25s
    8. Drawing triangles
      2m 55s
    9. Drawing polygons
      3m 37s
    10. Drawing simple curves
      4m 54s
    11. Drawing complex curves
      6m 46s
    12. Drawing Bézier curves
      5m 38s
  5. 54m 3s
    1. Introduction to variables
      10m 44s
    2. Understanding variable scope
      6m 53s
    3. Modifying variables
      9m 8s
    4. Creating arrays
      9m 53s
    5. Modifying arrays
      6m 37s
    6. Creating strings
      7m 3s
    7. Modifying strings
      3m 45s
  6. 1h 2m
    1. Incorporating randomness
      7m 59s
    2. Using Perlin noise
      4m 24s
    3. Shuffling with Java
      3m 30s
    4. Specifying line attributes
      8m 2s
    5. Changing placement modes
      5m 45s
    6. Understanding color attributes and functions
      4m 16s
    7. Exploring color spaces
      7m 44s
    8. Using color palettes
      7m 5s
    9. Transforming the grid
      8m 38s
    10. Exploring the attribute matrix
      5m 33s
  7. 52m 7s
    1. Building code blocks
      5m 57s
    2. Writing a while loop
      3m 52s
    3. Using for loops
      5m 35s
    4. Creating conditionals
      14m 50s
    5. Working with easing
      10m 51s
    6. Creating spirals
      11m 2s
  8. 18m 55s
    1. Mouse tracking
      3m 54s
    2. Hovering and clicking
      11m 16s
    3. Understanding keyboard interaction
      3m 45s
  9. 27m 32s
    1. Specifying fonts
      6m 43s
    2. Using images
      5m 51s
    3. Playing a video loop
      6m 20s
    4. Exporting video
      3m 47s
    5. Adding sound
      4m 51s
  10. 20m 49s
    1. Creating functions
      11m 48s
    2. Creating classes and objects
      9m 1s
  11. 31m 10s
    1. Using embedded data
      5m 26s
    2. Working with appended text data
      6m 4s
    3. Working with appended tabular data
      10m 26s
    4. Reading XML data
      9m 14s
  12. 48m 17s
    1. Generating dot plots
      11m 11s
    2. Building scatter plots
      10m 0s
    3. Making line plots
      9m 55s
    4. Creating bar charts
      9m 12s
    5. Checking out examples of maps, hierarchies, and networks
      7m 59s
  13. 20m 57s
    1. Introducing some principles of 2D design
      13m 44s
    2. Understanding color theory
      7m 13s
  14. 24m 46s
    1. Interacting with zooming, rotating, and sliding
      6m 26s
    2. Implementing slicing
      6m 47s
    3. Using rollovers
      5m 58s
    4. Introducing the GUI libraries
      5m 35s
  15. 10m 35s
    1. Sharing via OpenProcessing and other sites
      3m 19s
    2. Saving as a desktop application
      2m 42s
    3. Saving as JavaScript
      1m 47s
    4. Saving as an Android application
      2m 47s
  16. 2m 38s
    1. Where to go from here
      2m 38s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Interactive Data Visualization with Processing.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.