04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 21: Upcoming API s<br />

The JavaScript code here simply attaches an event handler to the button that either pauses or plays the<br />

video, depending on its current state. Then, an event handler is set for the < video > element ’ s load event<br />

so that the duration can be displayed. Last, a repeating timer is set to update the current time display.<br />

You can extend the behavior of this custom video player by listening for more events and making use of<br />

more properties.<br />

The media elements haven ’ t been implemented in any released browsers as of July 2008, although there<br />

are some experimental browser builds that have partial implementations. The media elements are<br />

scheduled to be implemented in Firefox 3.1 and Safari 4, though there is no word on other browsers<br />

implementing them as of the time of this writing.<br />

The < canvas > Element<br />

One of the most unique additions to HTML 5 is the < canvas > element, which provides a surface for<br />

drawing bitmaps on a page. Firefox 1.5, Opera 9, Chrome, and Safari 2 have implemented < canvas > to<br />

varying degrees of completeness. The drawing canvas always begins empty, and the only way to set its<br />

display is through JavaScript. The < canvas > element requires at least its width and height attributes to<br />

be set in order to indicate the size of the drawing to be created. Any content appearing between the<br />

opening and closing tags is fallback data that is displayed only if the < canvas > element isn ’ t supported.<br />

For example:<br />

< canvas id=”drawing” width=”200” height=”200” > A drawing of something. < /canvas ><br />

As with other elements, the width and height attributes are also available as properties on the DOM<br />

element object and may be changed at any time. The entire element may be styled using CSS as well.<br />

To begin drawing on a canvas, you need to retrieve a drawing context. The < canvas > element officially<br />

supports a single 2D drawing context (though a 3D context may be added later). A reference to the<br />

drawing context can be retrieved using the getContext() method and passing in “ 2d ” as follows:<br />

var drawing = document.getElementById(“drawing”);<br />

//make sure < canvas > is completely supported<br />

if (drawing.getContext){<br />

var context = drawing.getContext(“2d”);<br />

}<br />

//more code here<br />

When using the < canvas > element, it ’ s important to test for the presence of the getContext() method.<br />

Some browsers create default HTML element objects for elements that aren ’ t officially part of HTML. In<br />

that case, the getContext() method would not be available and could cause script execution errors.<br />

The 2D drawing context has its origin (0,0) at the upper left of the element. Coordinate values are<br />

calculated in relation to that point. By default, the width and height indicate how many pixels<br />

are available in each direction.<br />

682

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

Saved successfully!

Ooh no, something went wrong!