Video: Tracking mouse movement

When building games and interactive projects, you often need to worry about controlling movies with the mouse. Let's take a look at how EaselJS lets you access the mouse movement on the stage. We'll take a bitmap animation and have it follow the mouse, add friction to the mouse movement and orient a bitmap to the mouse position. My HTML and JavaScript files are setup like what we ended up with on the video on using sprite sheets for animation. So if you have any questions about how to work with bitmap animations, you may want to check out that movie. We have a standard HTML file with a canvas tag, as well a JavaScript document with a canvas variable, a stage, a sprite sheet and a bitmap animation.

EaselJS is a free JavaScript library that makes creating interactive web content for HTML5 more straightforward and intuitive. This course transitions web designers, animators, and content creators who may be used to working with Adobe Flash and ActionScript to this new open web standard. Author Ray Villalobos first explains the capabilities of the EaselJS framework and the HTML5 Canvas element, and what they mean for web design. The rest of the course shows how to use EaselJS's helper classes and hierarchal display list to load images, draw, animate, and handle mouse input from visitors.

Tracking mouse movement

When building games and interactive projects, you often need to worry about controlling movies with the mouse. Let's take a look at how EaselJS lets you access the mouse movement on the stage. We'll take a bitmap animation and have it follow the mouse, add friction to the mouse movement and orient a bitmap to the mouse position. My HTML and JavaScript files are setup like what we ended up with on the video on using sprite sheets for animation. So if you have any questions about how to work with bitmap animations, you may want to check out that movie. We have a standard HTML file with a canvas tag, as well a JavaScript document with a canvas variable, a stage, a sprite sheet and a bitmap animation.

The ship is currently centered on screen and unfortunately immediately set to explode. So if I refresh this window right here, you'll see that it's showing the explosion part of the animation. So I'll switch that out to the flying part of the animation right here. So now I save this and when I refresh, the ship is animating with the flying sequence. So moving the mouse is super simple. If we look at the documentation for the Stage class, we can see that it keeps track of the mouse position within the stage. It has a mouseX and a mouseY property.

So all we need to do to have the ship follow the mouse is add a couple of lines to this ship function. Now I am going to save this and refresh and you'll see that the ship moves with the mouse. If you want to you can go ahead and delete the part of the code that centers the ship. So let's save that and refresh and you can see that the ship follows the mouse. Now if you want to get rid of the cursor, the easies way to do that is with CSS. So I am going to open up the CSS file and just add a line that sets the cursor to nothing.

So cursor none, save it, and when I refresh while the cursor is on the stage, it's not going to show up. As soon as the cursor leaves the stage it'll show up again. So let's add a bit of friction to the mouse movement so that the ship moves towards the mouse at a fraction of the speed. To do that we're going to need to set up two variables that keep track of the difference between the position of the mouse and the ship. We'll do that inside the Ticker function. So now we can move the ship towards the position of the mouse, but not at full speed, at a fraction of the distance.

So let's see how that works, I am going to save this and when I refresh, you'll see that the ship is moving a little bit slower. Can't really tell because the mouse is hidden, so let's go ahead and show the mouse again. So now we can see that the mouse and the ship don't move at the same speed. The ship moves towards the mouse at a fraction of the speed. We can control that speed by changing this divisor. So let's go ahead and change it to 20 and you'll see that it will move a lot slower. I am refreshing and now ship moves but a lot slower than it used to. So let's say that we wanted the ship to orient itself to the mouse position as it follows the mouse.

When we figure out the difference between the position of the mouse and the cursor, we're sort of inadvertently creating a vector. A vector is an entity that has a magnitude and a direction. We could use that direction if we had a function that could convert the direction to an angle. JavaScript has a function that does just that, it's called the arctangent 2 function, so let's check it out. So we'll setup the Math a tangent function to our vector that's the difference between the mouse position and the ship position.

Now this is going to return a number in radians, because EaselJS wants the rotation to be in degrees, we're going to multiply it by 180 and divide it by Math.PI. So I am going to save this and refresh. So our ship can follow not only the speed of your mouse movement but also the direction of the movement as well, and that can make up for some really super interesting games and interfaces. The information we're learning to gather with our code, it's going to make it easier to build exciting projects with EaselJS.

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.

Already a member ?

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

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

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:

new course releases

newsletter

general communications

special notices

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:

new course releases

newsletter

general communications

special notices

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