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.
Another very common form of interaction with the mouse is to hover over an object and have it change or to click on the object and have it change, and in this I want to show you how both of those are going to work in Processing. I'm going to start with a comment here, and then I am going to start by putting in a palette. Come back to my list of palettes here. I think I'll use this one, copy that, and paste it back where I was.
And then I am going to create a few different variables that it will be using as part of the mouse interaction. What it is is I am going to be drawing two shapes. I'm giong to be drawing a circle and a square, and I am going to be changing the colors of them depending on whether we hover or click on them. So what I am going to do is create an integer variable here for the fill of the left shape, so left fill, lf. And that one is going to be the index number 2 in the palette. Then I am going to do a similar thing.
I am just going to copy that and do the right fill, so rf is for right fill, and again, that will be the same color. Then I am going to have another variable that determines the size of the objects that I am going to draw, int s is for size. I am going to make these 120 pixels. So it will a circle of 120 pixels in diameter and a square of 120 pixels on the side. Then I am going to do the setup block, and in there I put the size of the window.
I also turn on the anti-aliasing. I am going to be using the rectmode center. I want to be able to position this square the same way that I positioned the circle, so I have to put in rectmode, and then in all caps I put CENTER. So when I give the X, Y for the rectangle, it will just be the middle part. Then I am going to put a border around shapes. Now, they'll come from the first color in the palette. And I am going to make the borders five pixels wide, so strokeWeight (5). And that completes my setup. Save that.
The next block is draw. The first thing I am going to do is I am going to put in the background, the idea is that this refreshes every time it goes through. It doesn't preserve the previous stuff. I will put background, and this would be index 0 in the palette, the first color. Then I can put some code to draw a circle.
So this would be on the left side. And what I am going to do is I am first going to specify the fill color, which is lf. That's for the fill, except that needs to be in standard parentheses. Great! Then I put the code for the ellipse. I am going to put the ellipse one third of the way over. If you want to evenly space two objects, you put each of them on the dividing lines for the thirds. So width/3. I am going to have it be right in the middle from top and bottom, so center it on the halfway through the height, and then the diameter will be s and s.
That's for the size that I put earlier. That's 120 pixels. So that's the code for the circle. Then what I am going to do is I am going to draw a square on the right side. This one is also going to have a fill. It's going to be rf, for the right fill, and then I will draw a rectangle. And because I have put this rectmode (CENTER), I can specific the center point, and I am going to do width. I want this to be two-thirds over, so I am going to put width*2/3.
I also want it to be in the middle between top and bottom, so I put height/2, and s and s are the width and height of the square. This is also 120 pixels, so it would be the same size of this circle. So that should actually be enough to draw the two shapes, so there they are: circle on the left, square on the right. I think I have a little green fill and brown border. But what I am going to do is make it so that when we hover over them, or later when we click on them, they will do something. This requires a rather lengthy if statement, because what I have to do is detect the position of the cursor relative to each shape.
So the first thing I'm going to do is I am going to put Detect hovering over the circle. And the way this works is I put if and then I can use a function called distance. It abbreviate as dist, and it looks at the distance between the mouse and between another point. And what I am going to do is I am going to specify the center of the ellipse, and so all I have to do, really, is copy this right here.
That specifies the center of this circle, and then I give the coordinates for the mouse, mouseX and mouseY, built-in variables that track the mouse. So if that distance is less than the radius, I can put is less than s/2. So that actually means 60 pixels because the total diameter is 60. So I say if the distance between the current mouse position and the center of the circle is less than the radius, then that means we are hovering over it.
And so I can close that off, and I can now put curly brackets for a block of code. So what I am saying is if this is true, then do this. And what I am going to put is if you are hovering over, I want you to change the fill, so lf, is equal to palette 4. We are going to go to a different position in the palette. And then I am going to make an else statement in case we are not hovering, so we put else. And it gets its own curly brackets.
I am going to copy this to make it just a little bit simpler here. And if it's not hovering, leave it where it was at 2. So we can run this real fast and look, as I move the mouse over, it changes the color. It's detecting the hovering of the mouse, and this is really nice because Processing has this dist function that makes that easy to do. Now it turns out though, with a square or a rectangle it's a little different, because you can't just use a radius, because the corners are father away than the midpoints are.
And so to detect hovering over a square, we have to use a slightly more complicated approach. Let's put Detect hovering over square. What I am going to do here is I am going to type if, and I need to see if the mouse is to the right of the left border, to the left of the right border, above the bottom border, and below the top border. And so I actually need to have four separate statements here. What I am going to do is I am going to first going to see whether the mouseX, so its X position, is greater than the left border, and since all of these are positioned two-thirds of the way across the width-- that's the center point-- I just need to subtract a little bit to get the left border.
So if mouseX is greater than the left border, which is from the center 2/3rds minus the half of the width, then I put a conjunction here. I have used two ampersands to indicate that this other condition must also be true. What I am going to do now is to say it needs to be the left of the right border. That has a similar way of going, except I have to use the different direction, and I put width*2/3, and this time I put plus one half of the size of this square.
That gets me to the right border. Then I need to add the top and bottom borders. I am going to put my double smpersand and then just go to the next line. I am going to put mouseY this time--and when this mouseY is greater than the height/2, so that's the center point where the squared is positioned, -s/2. And to get the next border I put mouseY is less than the height/2+s/2.
This looks at whether the mouse is above the bottom border. Again, the way I am doing this is by height/2 represents the center point where the box was positioned, and then the +s/2 simply gets me to the border, because that's half of the size. And so I close that, and once I execute this, I will just put in the code right here. And what I am going to do is I am going to copy this code right here, because it's going to be very similar. All I want to do is take this back here and change that to right fill and right fill, and we should be good to go. I will save it.
We can hover over the circle--there it is--and we can hover over the Square, and there that is. This one only needs a single function to look at the distance because of the radius is uniform all the way around. This one needs four functions to determine if it's below the top, above the bottom, and to the left and right. Anyhow, that's how we can detect hovering. Now I am going to add one more line of code to show how you can make it that it only does it if you're hovering and you click. It's a very small addition to this. What I am going to do is I'm going to come over here, over the circle, I am going to just copy that code, and I am going to paste it right there, because I just don't want to get rid of what I have.
I say if this is true, but I am going to add one more condition to it. So it has to be over the mouse, but it also has to have mousePressed, and now it won't happen unless the cursor is over the shape and the mouse is pressed. And then do a similar thing down here. I am going to copy that and comment it out for right now. I did that by controlling, Ctrl+/ on the PC or Command+/ on the Mac. I paste it in and I simply add one more condition.
This will go to one more line, mousePressed. Oh, sorry, I see that I put this in the wrong place. I've got to remove that from there. There we go. It goes on the outside of that statement. Now, it's not enough to hover. I have to click at the same time. I hover and I click. And so that's another way of interacting, so we can do the hovering or clicking on the shape to get it to do something different, and that's one of the major elements of interactivity in a sketch. 00:11:15.89]
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.