New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Start learning with our library of video tutorials taught by experts. Get started

Interactive Data Visualization with Processing
Illustration by

Hovering and clicking


From:

Interactive Data Visualization with Processing

with Barton Poulson

Video: Hovering and clicking

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

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
Interactive Data Visualization with Processing
7h 43m Beginner Sep 25, 2012

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.

Topics include:
  • Exploring the need for creative data visualization
  • Drawing basic lines and shapes
  • Introducing variables, strings, and arrays
  • Modifying drawing attributes such as color
  • Making drawings more dynamic with animation loops and spirals
  • Creating keyboard- and mouse-based interactions
  • Adding images, video, and sound
  • Reading in text or XML data
  • Creating plots and charts
  • Publishing and sharing your work
Subjects:
Developer Programming Languages
Software:
Processing
Author:
Barton Poulson

Hovering and clicking

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.

 
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.
Upgrade now


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.

join now Upgrade now

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

Notes cannot be added for locked videos.

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.