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

Animating bitmaps with math


From:

EaselJS First Look

with Ray Villalobos

Video: Animating bitmaps with math

So far, we've animated objects with simple motion. When building games and interfaces, you'll often want to animate things with more interesting motion patterns. So let's take a look at a couple of types of animation patterns: randomness and oscillation. I started out with both an HTML as well as a JavaScript file with some starter code. In this JavaScript function, I set up the canvas and the stage then create a variable to keep track of the center of the stage and then I place a bitmap on the center of the screen. Then I add that bitmap as a child of the stage and create a simple animation function.

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

Animating bitmaps with math

So far, we've animated objects with simple motion. When building games and interfaces, you'll often want to animate things with more interesting motion patterns. So let's take a look at a couple of types of animation patterns: randomness and oscillation. I started out with both an HTML as well as a JavaScript file with some starter code. In this JavaScript function, I set up the canvas and the stage then create a variable to keep track of the center of the stage and then I place a bitmap on the center of the screen. Then I add that bitmap as a child of the stage and create a simple animation function.

If you need to review some of this, make sure you check out the movie on importing bitmap and vector graphics. So let's say for example that we want our ship to shake on screen. Maybe it's experiencing some turbulence. For that we can move its position by a random amount on every frame using JavaScript's random function. But make sure that it's on the center, and then we'll add a random amount. So let's go ahead and save this and I'll refresh my page. So you can see that the ship is shaking a little bit. The random method is going to return a number between zero and one.

If we want to make our shaking be more violent, we can multiply this by a number. I am going to save and refresh and you can see that it's shaking more violently. Now you can't really tell but the random number is making our ship shake only to the right and down, because the increments are always going to be positive. I'll show that by making our increments huge. If we want to make sure that this is going to be centered we need to just subtract -.5 from our random number before we multiply it. So I'll cover it with parentheses and subtract .5 from our random number.

Now I am going to save and refresh, and now it's going to shake around the center of the object. That's really important. So let's go ahead and delete this shaking, because it's a little bit extreme. Let me go ahead and save this and refresh my screen and my ship is staying put right now. So shaking is great, but sometimes you want create oscillating wave-like motion. To do that, you can use JavaScript's sine or cosine function. Sine and cosines are ratios used in calculating the length of a line segment in a right triangle, but that's really not that important.

If you feed this sine or a cosine function an increment over time, the functions are going to generate a number between -1 and 1, and the numbers are going to increment in a fluid like motion, which is exactly what we want for a lot of animation problems. So if I just do this--I am going to save this and refresh--my animation is going to reset right here. Now that's happening because I haven't fed a number into the sine function. So I am just going to put in a number, just any number and you'll see that it will reset back to the center. So the sine function by itself, it's not going to do anything.

To make it work, it needs to be fed a constantly increasing number. Now most people usually generate a counter for this, but our ticker function creates one automatically for us. Let's go ahead and use the getTicks method of the Ticker class. I am going to save that and refresh, and you can sort of see what's happening here. It's shaking the ship but in a more smooth like method than what randomness was doing. Not shaking a great bit, it's only going from -1 to 1. We can improve on that by adding some divisions or multiplications that is going to control how the ship moves from side to side.

Let's go ahead and modify our function here, and what I'll do is divide our Ticks by 7 and then multiply that number times 50. So I am going to save this and refresh, you can see that ship moving from side to side in a more smooth motion. So these numbers are going to be controlling our motion. So let's go ahead and change this number to 100 and see what happens. So now you can see that the ship moves a little bit farther. So this number is going to control how far away the ship is moving, set it back to 50. The other number is going to control how fast it's moving.

So you can see here dividing it by 2 makes it move a lot faster. You just have to find a number that works for you. So the divisor is going to give you the speed of the motion, and the multiplication is going to give you the size of the wave. So let's take a look at what it would take to make it move vertically. So I am going to go ahead and comment this out, and I'll make a copy of it and just modify the Y direction of the ship. So I'll save this, and I'll refresh and now this is moving up and down. So let's see what happens if I move them both up and down and right and left.

I am going to save and refresh and you can see that it's now moving diagonally. So that's okay, but we can actually do circular motion really easily. We could do that by using both a sine and a cosine function. Cosine is pretty similar to sine. Without getting into trig, it's essentially a sine wave moved over so that the oscillation is the same but happens at a different time than in a sine wave, perfect for what we need. So instead of sine for one of these motions, I am going to use the cosine function. Save it and refresh and now we have beautiful circular motion.

So this is instance when playing around with math functions is going to be a lot of fun, because we can see how the math affects our objects. Learning to modify our JavaScript math function allows you to create more interesting motion techniques.

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.