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

EaselJS First Look
Illustration by

Handling mouse events


From:

EaselJS First Look

with Ray Villalobos

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.

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 ...
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
Subjects:
Web Web Design Web Development
Software:
CreateJS EaselJS
Author:
Ray Villalobos

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.

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

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.