Top 10 HTML5 Canvas Features
- Feature #10: Path-Based Graphics and the Nonzero Winding Rule
- Feature #9: Hit Detection
- Feature #8: canvas Is an HTML Element
- Feature #7: Temporary Drawing
- 功能# 6:画面以外的画布
- Feature #5: Transforming the Coordinate System
- Feature #4: Image Manipulation
- Feature #3: Drawing Images and More with drawImage()
- Feature #2: The Clipping Region
- Feature #1: New Functionality in the 2D API
- Conclusion
Thecanvaselement is arguably the single most exciting aspect of HTML5. Initially implemented by Apple for Dashboard widgets on Mac OS X and adopted as a standard by theWeb Hypertext Application Technology Working Group(WHATWG) in 2005,canvasis a scriptable bitmap that lets you implement all sorts of interesting applications such as data visualization and video games that were never before possible with HTML, JavaScript, and CSS.
Using thecanvastag is straightforward: You put it in your HTML, like this:
The text inside thecanvaselement is known asfallback content, meaning content the browser "falls back" (reverts) to when it doesn't support HTML5 Canvas. As one of the earliest additions to the HTML 5 specification, Canvas is widely supported among browser vendors, so it's likely that the fallback content in yourcanvaselements will never be displayed.
You access thecanvaselement and its underlying 2D graphics context like this:
var canvas = document.getElementById('game-canvas'), context = canvas.getContext('2d'), // Note: It's '2d', not '2D'
The preceding JavaScript obtains a reference to thecanvaselement. That element is merely a container for a graphics context that the preceding code accesses through thecanvasgetContext()method. There is also a 3D context, commonly known asWebGL, which is more powerful than the 2D context, but it comes with a much steeper learning curve and an obtuse application programming interface (API). For 2D graphics, the 2D context, which is the topic of this article, is typically a better choice than WebGL.
The 2D context provides more than 30 methods that let you do everything from drawing graphics primitives (such as lines and rectangles) to manipulating the individual bits of a particular image. For example, you could draw a filled rectangle 100 pixels wide by 100 pixels high at (10, 10) like this:
context.fillStyle = 'rgba(200, 200, 200, 0.5)'; // semi-transparent light gray context.fillRect(10, 10, 100, 100); // (x, y, w, h)
When most people think of Canvas, they typically think of 2D games, and for good reason. Canvas has always had a solid API for implementing games, and with the advent of hardware acceleration, Canvas is well suited for implementing high-octane video games.
You can use the Canvas 2D context API for a lot more than just video games, however. In the rest of this article, I'll discuss what I consider to be the top 10 features of that API:
- New functionality in the 2D API
- The clipping region
- Drawing images and more withdrawImage()
- Image manipulation
- Transforming the coordinate system
- Offscreen canvases
- Temporary drawing
- canvasis an HTML element
- Hit detection
- Path-based graphics and the nonzero winding rule
Feature #10: Path-Based Graphics and the Nonzero Winding Rule
Like scalable vector graphics, Apple's Cocoa, and Adobe's Illustrator, the Canvas 2D API is path-based. To draw graphics primitives, such as lines, rectangles, and arcs, you create apath—which is just a sequence of points—and then stroke or fill that path.
A single path can contain multiple subpaths; for example, the application shown inFigure 1draws the cutouts from a single path—the rectangle enclosing the cutouts—that contains paths for the small rectangle, triangle, and circle.
Figure 1Drawing cutouts.
Here's an excerpt of the code that draws the cutouts for the application inFigure 1:
function drawCutouts() { context.beginPath(); addOuterRectanglePath(); // clockwise addCirclePath(); // counter-clockwise addRectanglePath(); // counter-clockwise addTrianglePath(); // counter-clockwise context.fill(); // Cut out shapes } function addOuterRectanglePath() { context.rect(110, 25, 370, 335); } function addCirclePath() { context.arc(300, 300, 40, 0, Math.PI*2, true); } function addRectanglePath() { rect(310, 55, 70, 35, true); } function addTrianglePath() { context.moveTo(400, 200); context.lineTo(250, 115); context.lineTo(200, 200); context.closePath(); } function rect(x, y, w, h, direction) { if (direction) { // counter-clockwise context.moveTo(x, y); context.lineTo(x, y + h); context.lineTo(x + w, y + h); context.lineTo(x + w, y); context.closePath(); } else { context.moveTo(x, y); context.lineTo(x + w, y); context.lineTo(x + w, y + h); context.lineTo(x, y + h); context.closePath(); } }
The preceding code uses the following 2D context methods to create paths:moveTo(),lineTo(),rect(), andarc(). Then it usesfill()to fill the path.
The more interesting aspect of the preceding code, however, is its use of thenonzero winding ruleto create the cutout effect. When a path's subpaths intersect or overlap, as is the case in the preceding application, the context must determine which areas to fill when thefill()method is invoked. For that, the context uses the nonzero winding rule, which uses a path's direction to determine what areas to fill.
With Canvas, you can create a complex path, possibly containing subpaths, out of regular or irregular shapes, and then you can stroke or fill that path, or use the path for other purposes. For example, you specify the clipping region of the Canvas (feature #2 in our countdown), with a path.
Paths are an important Canvas feature, and are even more powerful in the latest version of the Canvas specification. (I discuss the latest changes in the section "Feature #1: New Functionality in the 2D API," at the end of this article.)