Adding canvas content
Video: Adding canvas contentIn the previous movie, we discussed the Canvas element and its related 2D API. In this movie, we will apply what we've learned to our trail guide page by adding Canvas content to the page to replace our trail guide map. Currently, the page's map and elevation guide on the page is a single image. We are going to replace that with Canvas element that's going to allow us to pull in our map as a separate image and then draw the elevation guide through the Canvas API. To illustrate that, I've got our map opened here in Illustrator, which was the program I used to create it.
Viewers: in countries Watching now:
In HTML5 First Look, author James Williamson introduces the newest HTML specification, providing a high-level overview of HTML5 in its current state, how it differs from HTML 4, the current level of support in various browsers and mobile devices, and how the specification might evolve in the future. Exercise files accompany the course.
- Understanding the history of HTML5
- Using new tags
- Understanding HTML5 semantics
- Coding ID and class attributes in HTML5
- Structuring documents
- Building forms
- Exploring HTML5 native APIs
- Encoding and adding HTML5 video
- Exploring associated technologies such as CSS3
Adding canvas content
In the previous movie, we discussed the Canvas element and its related 2D API. In this movie, we will apply what we've learned to our trail guide page by adding Canvas content to the page to replace our trail guide map. Currently, the page's map and elevation guide on the page is a single image. We are going to replace that with Canvas element that's going to allow us to pull in our map as a separate image and then draw the elevation guide through the Canvas API. To illustrate that, I've got our map opened here in Illustrator, which was the program I used to create it.
So this is the current map graphic as you see it on the page. So let's say our site has 50 different trails that we need to represent. That would mean that we would need to create a trailed map for every single one of those guys. So a new topographical map, a new pass for the trail itself, plot all the trailheads on it, and then do the trail elevation graph itself. Well, Canvas can make those types of chores a little bit easier. So what we're going to do is we're going to remove the trail elevation plot guides from our map, and then we're going to draw those using Canvas.
So we're going to import this graphic into our Canvas element and then draw on top of it using the Canvas API. Now, this has a lot of potential uses. Imagine, for example, that you could take all of your trails and put them in a database. You could place the trail guide elevation plot points in a database, you could place the topographical maps in a database, you could do the background image in a database, and you can go ahead and plot drawing the trail maps as well in a database. You can assemble them all at runtime based on which trail you were looking at and have the Canvas API draw all those elements together in a composition.
That would make life a little bit easier on you. Now, rather than doing something quite that complex for our example, what we're going to do is sort of just a piece of it. We are going to take this part of the map, which has been exported out as a JPEG, and then we're going to draw on top of it to add these plot points by using the Canvas API. So you should still be able to get a really good feel for how you might accomplish a composition of this nature. Now that we sort of understand what we're going to be doing with our trail guide map, let's switch back over to our HTML page and begin coding it. Okay, so here I am in the trails.htm file.
I've opened this up from the 06_02 directory and currently, we don't have a Canvas element on our page. We just have an image tag that's showing a mockup of what it's eventually going to look like. So I am going to scroll down in my code to find that image, which is found on Line 66 for me right here. There is our Canvas_mockup. So I am just going to go ahead and delete that image tag, and I want to go ahead and add a So I am going to type in canvas, and I need to add some attributes to this. The first attribute I need to add would be an ID.
So the ID for this is going to be myMap. That's how we are going to reference this object through our script to let it know which canvas we want to draw into. The next thing we need to do is establish a width and a height. So I am going to give this a width of 600 and then give it a height of 695. So I will go ahead and close the opening tag and then close the Now, we could just go ahead and leave it as this, but it's a really nice idea to provide some fallback content. So inside the not support Canvas.
Please upgrade to the latest version of Chrome, Firefox, or Safari." So really kind of weak fallback content there, but at least it would display and let somebody know that hey, you are missing out on something. Okay, so we've created our Canvas element, we've given it a name, we've given it a width and a height. So really, the only thing that Canvas element does is it informs the browser that hey, we need a drawing environment, we want it to be placed here within our page, and we want it to be this size.
So I am just going to type in function drawMap and open and close some curly braces. This is a habit I have gotten into over the years because I have a really bad habit of forgetting those closing curly braces, especially if I am working with larger functions. So I always create sort of a skeleton before I do anything else. The next thing we need to do is go ahead and create an object for our map. So I am just going to type in var elvPlot for elevation plot. Next thing, you need to reference the Canvas element on the page that I am going to be drawing into using this object.
So I am going to type in =document. getElementById and remember we named the Canvas element myMap. So I am going to use myMap for that, and then I need to pass in which API we're going to be drawing with. So I am going to do getContext and I am going to pass into that 2d. Now that tells it that I am going to be using the 2D drawing API. So all my methods and properties are going to be coming from that particular API. Remember, the 3D API hasn't even really been formalized yet, but that is something that you need to do so it understands which methods and which specification you're working with.
So I am going to go down to the next line and now we need to worry about the external image that we're going to be loading into our map. The first thing I am going to do to handle that is to create an object to store our image. So I am going to type in var img = newImage and then I need to go out and tell it which image to load into that object. So I am going to do img.src = and here I am just going to give it a path directly to the image. So _images/northridge_map.jpg.
Now, this could be a dynamic link to your image, but in this case we're just pointing it to that specific graphic to tell it that that's the one that we want. Okay, so now that we've created an object to store our image and loaded the image inside of it, we are ready to draw that into our Canvas environment. We do that through the use of the drawImage method. Now, it's really tempting to just go ahead and call the drawImage method and call it a day, but there's a little bit of a problem with that. This script is going to execute very quickly, faster for the most part than your image can load from its server.
So, it's a really good idea to not call the drawImage method until the image has finished loading. We're going to do that by using an onload event handler to call the drawImage method. I'll do that just above where I've declared the source of the image. So what I am going to do here is I am going to type in img.onload = function and then inside the onload event handler here, I am going to type in elvPlot.drawImage. Now, the drawImage method needs three properties passed into it.
The first thing it wants to know is hey, which image am I drawing to the Canvas environment? And that's going to be the img image, and then the next thing it wants to know is what's my X coordinate and what's my Y coordinate, or where should I be placed? So I am going to type in ,0,0. So that's going to position it at the X coordinate of 0, and the Y coordinate of 0. So the top left-hand corner of the image will be aligned with the top left-hand corner of the Canvas itself. Since the Canvas element is the same size as the image, it should be a nice, snug fit.
So I am going to save this and we're almost ready to test it, but as of yet, we have not called the drawMap function. Now, you could trigger that function any way you wanted to. So really that's based upon the application or how you are applying the Canvas element on the page. We are just going to do it through very simple means. I am going to go down to the tag and I am going to trigger it by using an onload event handler on the body. So, a little primitive, but for this exercise it works just fine. So I am going to type in onload= and inside of that, I am going to type in drawMap.
So I am going to go ahead and call that function. All right! I am going to go ahead and save the file and now I am going to go ahead and test this in any browser that supports the Canvas element. In this case, I am going to do Chrome and as I scroll down, cool,. Now I get to see the image. So the image is there, but unlike last time when we were just using the image tag, notice that this version of it does not have those plot points. So we've created a Canvas element on the page, we've written a script that references that element, it's loading an external image and then it draws that image to the Canvas. Well, now that we have that image on our Canvas, we could, if we wanted to, scale it, transform it, tile it or further modify it using any of those methods and properties in the Canvas API, but that's not what we want to do.
We want to draw that graph. So in our next movie, we're going to experiment with drawing in the Canvas element by plotting our elevation graph.
There are currently no FAQs about HTML5 First Look.