Chapter 2. Drawing on the Canvas
Using HTML5 Canvas effectively requires a strong foundation in drawing, coloring, and transforming basic two-dimensional shapes. While the selection of built-in shapes is relatively limited, we can draw any shape we desire using a series of line segments called paths, which we will discuss in the upcoming section Using Paths to Create Lines.
Note
The HTML5 Canvas API is well covered in many online forms. The W3C site has an exhaustive and constantly updated reference that details the features of the Canvas 2D Drawing API. It can be viewed at http://dev.w3.org/2006/canvas-api/canvas-2d-api.html.
However, this online reference lacks concrete examples on using the API. Rather than simply reprinting this entire specification, we will spend our time creating examples to explain and explore as many features as we have space to cover.
The Basic File Setup for This Chapter
As we proceed through the Drawing API, all the examples in this
chapter will use the same basic file setup, shown below. Use this code as
the basis for all of the examples we create. You will only have to change
the contents of the drawScreen()
function:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Ch2BaseFile - Template For Chapter 2 Examples</title> <script src="modernizr-1.6.min.js"></script> <script type="text/javascript"> window.addEventListener('load', eventWindowLoaded, false); function eventWindowLoaded() { canvasApp(); } function canvasSupport () { return Modernizr.canvas; ...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.
Read now
Unlock full access