Following the publication of Introduction to jCanvas: jQuery Meets HTML5 Canvas on SitePoint, a thread of great comments initiated by Craig Buckler has sprung up around a core topic: are there any use-cases for preferring HTML5 Canvas over SVG?
Both are standards-based HTML5 technologies you can use to create amazing graphics and visual experiences. But should it matter which one you use in your project?
At one point, Craig’s comment goes straight to the heart of the matter as he writes:
One benefit of canvas is that it doesn’t have a DOM! It allows you to animate thousands of objects without that overhead – which is ideal for games.
Following Craig’s trails, I’m going to do my best to explore the question outlined above and discuss key factors to consider when opting for one technology over the other.
First, let’s spend a few words introducing HTML5 Canvas and SVG.
What is HTML5 Canvas?
Here’s how the WHATWG specification introduces the canvas element:
The canvas element provides scripts with a resolution-dependent bitmap canvas, which can be used for rendering graphs, game graphics, art, or other visual images on the fly.
In other words, the <canvas>
tag exposes a surface where you can create and manipulate rasterized images pixel by pixel using a JavaScript programmable interface.
Here’s a basic code sample.
The HTML:
[code language="html"]
<canvas id="myCanvas" width="800px" height="800px"></canvas>
[/code]
The JavaScript:
[code language="javascript"]
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
context.fillStyle = '#c00';
context.fillRect(10, 10, 100, 100);
[/code]
You can take advantage of the HTML5 Canvas API methods and properties by getting a reference to the 2D context object. In the example above, I have drawn a simple red square, 100 x 100 pixels in size, placed 10px from the left and 10px from the top of the <canvas>
drawing surface.
Being resolution-dependent, images you create on <canvas>
may lose quality when enlarged or displayed on retina screens.
Drawing simple shapes is just the tip of the iceberg. The HTML5 Canvas API allows you to draw arcs, paths, text, gradients, etc. You can also manipulate your images pixel by pixel. This means that you can replace one color with another in certain areas of the graphics, you can animate your drawings, even draw a video onto the canvas and change its appearance.
Continue reading %Canvas vs. SVG: Choosing the Right Tool for the Job%
by Maria Antonietta Perna via SitePoint
No comments:
Post a Comment