Viewers: in countries Watching now:
One of the most exciting additions that HTML5 offers to designers is the ability to draw free-form graphics on a drawing surface known as the Canvas. In this course, author Joe Marini introduces the technical concepts behind Canvas and shows how to perform drawing operations directly in a web page. The course covers drawing basic and complex shapes, setting colors and styles, adding shadows, patterns, and gradients, more advanced techniques such as scaling, rotating, and compositing objects, and how to incorporate Canvas elements in a slideshow and an animation.
We've seen how to declaratively create a canvas in HTML. In this example, let's see how we can use script to affect how canvas looks on the browser. So I'm going to open up my example file, and in my exercise files, and I'm in chapter 04, and we'll open up contextprops_start, and you can see we've got some code we need to write. Let's go and take a look at this in the browser really quick. So I'll bring this up in IE, and you can see there's a dotted outline. There is no background color here, just the outline, and what we're going to do is write some code that will affect how the canvas tag itself is displayed in the browser.
So let's go back to the code and over in my snippets, I'm going to copy the three lines under the ContextProps section, so we'll copy those and we'll put them in here, in the window.onload function. So what we're doing here is when the window loads, we've got some code that's going to get a reference to the canvas element by calling getElementById, and that's thus canvas right here. And then we're going to programmatically change the width and height from 400 and 300, which are the defaults, down to 150 x 150. So we save this and then when we go back to the browser. Now when I refresh this, you can see that the canvas got a little bit smaller.
So this shows us how we can programmatically display the canvas using different properties, just the same way that we can do it from HTML. Now in HTML, we can give it attributes that affect how the canvas is displayed initially, but once that height and width is defined in HTML, we can still go back and code and programmatically affect them.
There are currently no FAQs about HTML5: Graphics and Animation with Canvas.
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.