10.07.2017 Views

246996016-HTML5-Step-by-Step

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

288 Chapter 16<br />

You add a canvas element to a Web page with the aptly titled tag. Like other<br />

HTML tags, the tag accepts attributes, the most basic of which are the pixel<br />

height and width of the canvas on the page. The code to create a 200 × 200 pixel canvas<br />

looks like this:<br />

<br />

Here’s the syntax for an entire page with the 200 × 200 canvas inside:<br />

<br />

<br />

<br />

<br />

Canvas<br />

<br />

<br />

<br />

<br />

<br />

This HTML creates a canvas on the page that is ready for content. But wait! What advantage<br />

does a canvas element provide on a page? How do you make a cool animated clock<br />

or menu or other widget? Alas, therein lies the problem. When simply included in a page<br />

like the one shown, a tag doesn’t do anything. In fact, it’s just blank; go ahead<br />

and load that code into a canvas-compatible browser (Internet Explorer 9, Firefox 1.5 or<br />

newer, Safari, and so on). The page is blank.<br />

So, how might you get the canvas to do something? The answer lies in the programming<br />

language that all modern browsers understand: JavaScript. You bring the canvas element<br />

to life with JavaScript; everything within the canvas is drawn, animated, and destroyed<br />

with JavaScript.<br />

You might not know much, if anything, about JavaScript. However, the information in this<br />

chapter will give you an introduction. After learning a bit about JavaScript you’ll be able<br />

to make the canvas element do some fun and interesting things!<br />

JavaScript, Briefly<br />

JavaScript is not Java; it bears no relation to the Java programming language, the drink,<br />

or the country. JavaScript is a programming language that is used primarily to provide<br />

additional functionality to Web pages and applications, and it’s used heavily in the “Web<br />

2.0” paradigm of highly interactive Web sites.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!