Canvas Element - Canvas Versus Scalable Vector Graphics (SVG)

Canvas Versus Scalable Vector Graphics (SVG)

SVG is an earlier standard for drawing shapes in browsers. However, unlike Canvas which is raster-based, SVG is vector-based, i.e., each drawn shape is remembered as an object in a scene graph or Document Object Model, which is subsequently rendered to a bitmap. This means that if attributes of an SVG object are changed, the browser can automatically re-render the scene.

In the Canvas example above, once the rectangle is drawn, the fact that it was drawn is forgotten by the system. If its position were to be changed, the entire scene would need to be redrawn, including any objects that might have been covered by the rectangle. But in the equivalent SVG case, one could simply change the position attributes of the rectangle and the browser would determine how to repaint it. There are additional JavaScript libraries that add scene-graph capabilities to the Canvas element. It is also possible to paint a canvas in layers and then recreate specific layers.

SVG images are represented in XML, and complex scenes can be created and maintained with XML editing tools.

The SVG scene graph enables event handlers to be associated with objects, so a rectangle may respond to an onClick event. To get the same functionality with canvas, one must manually match the coordinates of the mouse click with the coordinates of the drawn rectangle to determine whether it was clicked.

Conceptually, canvas is a lower-level API upon which an engine, supporting for example SVG, might be built. There are JavaScript libraries that provide partial SVG implementations using Canvas for browsers that do not provide SVG but support Canvas. However, this is not (normally) the case—they are independent standards. The situation is complicated because there are scene graph libraries for Canvas, and SVG has some bitmap manipulation functionality.

Read more about this topic:  Canvas Element

Famous quotes containing the word canvas:

    Cubism had been an analysis of the object and an attempt to put it before us in its totality; both as analysis and as synthesis, it was a criticism of appearance. Surrealism transmuted the object, and suddenly a canvas became an apparition: a new figuration, a real transfiguration.
    Octavio Paz (b. 1914)