Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
You don't always want to or need to create graphics with EaselJS. You may want create more complex graphics in a separate program and import them into your stage. EaselJS provides the bitmap class that lets you easily bring not just bitmap data, but most types of external graphics, so let's take a look. The bitmap class is a child of the DisplayObject. You may want to check out some of the methods and property of the DisplayObject class as well as the bitmap class in the EaselJS documentation. So I am starting off with a basic HTML template. If you haven't already, make sure you watch the video on creating a template and installing EaselJS.
To the basic template we've added a script file that already has a canvas, a stage, as well as an animation using the Ticker class. I've also added a few images in different formats in an images folder. Let's go ahead and add one of them to the stage. To the bitmap class we just need to pass along the location of the image we want to add, and then we need to add it as a child of the stage. So I am going to save this, and I will come over here and refresh my page and our graphic should appear right there at the top of the stage. We can import any type of valid HTML graphic format.
We could switch this to a GIF file--I'll save and refresh--or a JPEG, or any other graphic format our browser will handle. Let me go ahead and switch it back to PNG. Now let's add some variables that keep track of the center of our canvas, and let's go ahead and center our bitmap. I'll refresh my graphic, and it's not exactly centered. It is placing the graphic in the center of the screen. Because the graphic gets placed on the screen from the center position, we need to adjust the graphic's registration point. I know this graphic is 100x100 pixels tall because I drew it that way, so my registration is going to be adjusted by 50 pixels.
I'll save it and refresh and now our graphic is centered on screen. You always have to remember to set the registration point of any bitmap you bring in. So if the file is a vector, you can also use the SVG format, which stands for scalable vector graphics. The SVG format is a lot more resizable than PNG or other formats. So all you have to do is just specify that extension, SVG, save it, and the graphic comes along just fine. Now to save in SVG, you can use a program like Adobe Illustrator to save your graphic in the SVG format.
This is a vector file, one object that I drew, so I can take this graphic and go to the File menu and select Save As. Don't select Export or Save for Web. Save As will give you an option to save as an SVG graphic. You don't want to choose SVG Compressed, just regular SVG is fine. Let me go ahead and put this on the desktop. So once we do that just hit Save and just leave these options looking like this. The default are usually just fine. And I'll click OK and that saves my graphic as an SVG file.
I've already done that in my example, so you could see that it brought it in just fine. Now let's go ahead and scale this graphic by 400% to see how a vector graphic draws smoothly. I am going to save and then refresh the screen; you can see that in the bigger size this graphic looks awesome. Now what happens if we change to PNG? Save it and refresh, you'll see that our PNG looks a lot more pixelated and a little bit blurry. That's because PNG graphics are not vector graphics, they are bitmaps. So if you try the different formats, you are going to find that SVG is going to scale the best; however, because the graphics have to be recalculated and SVG is a vector, performance is going to be a little slower with SVG graphics.
So animating bitmaps is just like animating any other type of DisplayObjects. Since the bitmap class is a child of the DisplayObject, like a shape it's going to inherit properties like rotation and scaling. We've already done scaling here, let's try rotation. Go ahead and switch it back to the original size, I'll go ahead and use an SVG, I'll refresh, then I'll modify this ship's rotation in the animation section. Save this and it will rotate by 5 degrees on every frame. There's more to the bitmap class, so make sure you check out the documentation on the EaselJS page.
So importing graphics is super simple. The bitmap class is flexible enough it will let you import in many different formats. You may want to use SVG graphics when scaling is critical and the rest of the bitmap formats when performance is more important.
There are currently no FAQs about EaselJS First Look.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.