Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
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.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98486 Viewers
61 Video lessons · 85778 Viewers
71 Video lessons · 69678 Viewers
56 Video lessons · 101998 Viewers
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.
Your file was successfully uploaded.