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

Handling mouse events

From: EaselJS First Look

Video: Handling mouse events

Because EaselJS uses JavaScript, any element that is a child of the DisplayObject can have JavaScript properties the track mouse events like onClick and onMouseOver. In this movie, we're going to take a look at how you can take advantage of mouse events with EaselJS. So I've set up a standard HTML file with a canvas tag that has an ID of easel, as well as a JavaScript document that has a canvas, a stage as well as a sprite sheet. I've centered the ship on the stage and set up the animation loop.

Handling mouse events

Because EaselJS uses JavaScript, any element that is a child of the DisplayObject can have JavaScript properties the track mouse events like onClick and onMouseOver. In this movie, we're going to take a look at how you can take advantage of mouse events with EaselJS. So I've set up a standard HTML file with a canvas tag that has an ID of easel, as well as a JavaScript document that has a canvas, a stage as well as a sprite sheet. I've centered the ship on the stage and set up the animation loop.

If you've got some questions about working with sprite sheets or setting up animations, make sure you check out the movie on using sprite sheets for animation. Once you create a DisplayObject you can perform actions with the object using event handlers. So let's take our ship animation and have it display a different sequence when clicked on. So what we're doing here is creating an event handler on our ship that checks to see if it is being clicked on. If that's the case, then we're asking the ship to go and play another animation sequence. This is the animation sequence called explode which is right here. So I'm going to save this and I'm going to refresh, and then I'm going to click and see what happens.

So you could see that right now nothing is happening and that's actually because of a problem with the browser. So we're using Google Chrome right here, if I right-click and select Inspect Elements, you could see that there is 10 errors happening on this movie. If I click on it one more time you'll see 12 and 14. So I'm going to click on the errors and see what it's telling me. What this is saying is that this browser is having an issue when you're trying to access pixel data with a local domain. What that means is because we're working with a local file not a file on a website, this browser has some security issues with that.

And so what we could do is just put these files on a server or find a browser that works well without those security issues. And thankfully Safari works pretty well, so I'm going to right-click on the index file, I'm in Espresso, my editor, and then I'm going to Open With Safari and now when I click on the ship you could see the animation sequence that explodes the ship play. So that's just something you have to be aware of if you are working with local files and you're using bitmap data. So let's go back into our script right here and you may notice that we were able to use something called gotoAndPlay.

So if you come from an Adobe Flash world, you'll notice that that's a very familiar method that you can use with flash ActionScript. EaselJS brings in a couple of those different types of methods, so you could do stuff like gotoAndPlay and gotoAndStop and then use a frame name or a frame number. So if I gotoAndStop frame 10, then I'll save this, refresh, and when I click you'll see that this stops at frame 10. If you go to the EaselJS manual for BitmapAnimation, you could see that there are a lot of properties and methods that we're familiar from working with Flash.

So we have stuff like currentAnimation, currentFrame. We have stuff like the gotoAndPlay, gotoAndStop and other things that are super familiar if you're working with Flash. EaselJS also allows you to work with events like MouseOver, but not by default because they can be expensive. So let's try a MouseOver event on the ship. And we'll have a gotoAndPlay our explode animation. Save it, refresh, and when I move my mouse over that ship should be exploding, but it's not.

And that's because EaselJS allows you to use MouseOver events, but not by default because they can be expensive processing wise. So we have to actually turn on those types of events and you could do that with this simple command stage.enableMouseOver, so that method once we save and we refresh, will allow us to use MouseOver events just like that. Now if you want to check for drag and drop events, it's a little different than what you do in Flash. We have to check for a double event so we have to modify this.

First to check for an onPress event, onPress is what happens when you click the mouse on an object and then hold it there. And then we have to check for and onMouseMove event. Inside the onPress event, we're going to check for and onMouseMove event. And inside the onMouseMove handler we're going to make whatever we clicked on be in the same position as whatever we're dragging. So if I save and refresh I can click and drag on this element.

So EaselJS is going to give you a rich library of methods and properties to work with. It's really cool that it works with a lot of methods that you're going to be familiar if you're coming from Flash. There are a couple of challenges you have to watch out for like turning on MouseOver event and dealing with browser security issues.

Show transcript

This video is part of

Image for EaselJS First Look
EaselJS First Look

21 video lessons · 5004 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 preferences from 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.

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.