Viewers: in countries Watching now:
Start communicating ideas and diagramming data in a more interactive way. In this course, author Barton Poulson shows how to read, map, and illustrate data with Processing, an open-source drawing and development environment. On top of a solid introduction to Processing itself, this course investigates methods for obtaining and preparing data, designing for data visualization, and building an interactive experience out of a design. When your visualization is complete, explore the options for sharing your work, whether uploading it to specialized websites, embedding the visualizations in your own web pages, or even creating a desktop or Android app for your work.
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.
There are currently no FAQs about Interactive Data Visualization with Processing.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.