Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Tracking mouse movement


From:

EaselJS First Look

with Ray Villalobos

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.
please wait ...
Watch the Online Video Course EaselJS First Look
1h 28m Beginner Sep 21, 2012

Viewers: in countries Watching now:

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.

Topics include:
  • Installing EaselJS
  • Understanding how Canvas draws and animates
  • Drawing lines and strokes
  • Drawing with graphic primitives
  • Using the chaining and compacting commands
  • Animating shapes
  • Working with text
  • Importing bitmaps and vector graphics
  • Working with sprite sheets
  • Handling mouse events
Subject:
Web
Software:
CreateJS EaselJS
Author:
Ray Villalobos

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.

There are currently no FAQs about EaselJS First Look.

 
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

* Estimated file size

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

Upgrade to View Courses Offline

login

With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

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 ?

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:

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

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.

Start your FREE 10-day trial

Begin learning software, business, and creative skills—anytime,
anywhere—with video instruction from recognized industry experts.
lynda.com provides
Unlimited access to over 4,000 courses—more than 100,000 video tutorials
Expert-led instruction
On-the-go learning. Watch from your computer, tablet, or mobile device. Switch back and forth as you choose.
Start Your FREE Trial Now
 

A trusted source for knowledge.

 

We provide training to more than 4 million people, and our members tell us that lynda.com helps them stay ahead of software updates, pick up brand-new skills, switch careers, land promotions, and explore new hobbies. What can we help you do?

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.