Skip to Content
HTML5 for Publishers
book

HTML5 for Publishers

by Sanders Kleinfeld
October 2011
Intermediate to advanced
56 pages
3h 2m
English
O'Reilly Media, Inc.
Content preview from HTML5 for Publishers

Chapter 1. Canvas for Publishers

With the <canvas> element, publishers now have the opportunity to embed a dynamic sketchpad into HTML5 content. The What is HTML5 for doing so is quite simple:

<canvas id="my_first_canvas" width="200" height="225">
  The content you put here will show up if your rendering engine 
  doesn't support the <canvas> element.
</canvas>

The <canvas> element accepts two attributes that specify the dimensions of your drawing area in pixels: width and height. Anything you place within the opening and closing tags of the element will only be displayed if the rendering engine does not support <canvas>; this gives you the option of providing fallback content for backward compatibility with non-HTML5 environments (see HTML5 Canvas, EPUB, and Ereader compatibility for more on compatibility).

And that’s where the HTML starts and ends; it merely sets aside the space within the HTML document in which to place your graphics. To actually draw on your <canvas>, you’ll use JavaScript code to interact with the Canvas API, which provides you with an elegant set of functions for creating lines, arcs, shapes, and text. You also have access to more advanced graphic-manipulation calls to scale, rotate, or crop your images.

Drawing on your <canvas>

Let’s draw a smiley face on the canvas we just created above. Here’s a list of the Canvas API functions we’ll use:

strokeRect(x1, y1, x2, y2)

Draw a rectangular outline from the point (x1, y1) to (x2, y2). Note: by default, the “origin” of the ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

HTML5 Hacks

HTML5 Hacks

Jesse Cravens, Jeff Burtoft
What Employees Want Most in Uncertain Times

What Employees Want Most in Uncertain Times

Kristine W. Powers, Jessica B.B. Diaz

Publisher Resources

ISBN: 9781449320065Supplemental ContentErrata Page