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

Tracking mouse movement

From: EaselJS First Look

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.

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.

Show transcript

This video is part of

Image for EaselJS First Look
EaselJS First Look

21 video lessons · 4857 viewers

Ray Villalobos
Author

 

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.

Join now "Already a member? Log in

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 EaselJS First Look.

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.