HTML GraphicsLesson #49

HTML SVG

[SVG defines vector-based graphics in XML](/graphics/svg_intro.asp), which can be directly embedded in HTML pages.

SVG (Scalable Vector Graphics)

SVG defines vector-based graphics in XML, which can be directly embedded in HTML pages.

SVG graphics are scalable, and do not lose any quality if they are zoomed or resized:

  • SVG Sorry, your browser does not support inline SVG. SVG is supported by all major browsers. ## What is SVG? SVG stands for Scalable Vector Graphics
  • SVG is used to define vector-based graphics for the Web
  • SVG defines graphics in XML format
  • Each element and attribute in SVG files can be animated
  • SVG is a W3C recommendation
  • SVG integrates with other standards, such as CSS, DOM, XSL and JavaScript

The Element

The HTML <svg> element is a container for SVG graphics.

SVG has several methods for drawing paths, rectangles, circles, polygons, text, and much more.


SVG Circle

Sorry, your browser does not support inline SVG.

Example

Example Code

html
Try it Yourself
Live Output Preview:

SVG Rectangle

Sorry, your browser does not support inline SVG.

Example

Example Code

html
Try it Yourself
Live Output Preview:

SVG Rectangle with Opacity and Rounded Corners

Sorry, your browser does not support inline SVG.

Example

Example Code

html
Try it Yourself
Live Output Preview:

SVG Star

Sorry, your browser does not support inline SVG.

Example

Example Code

html
Try it Yourself
Live Output Preview:

SVG Gradient Ellipse and Text

SVG Sorry, your browser does not support inline SVG.

Example

Example Code

html
Try it Yourself
Live Output Preview:

Differences Between SVG and Canvas

SVG is a language for describing 2D graphics in XML, while Canvas draws 2D graphics, on the fly (with JavaScript).

SVG is XML based, which means that every element is available within the SVG DOM. You can attach JavaScript event handlers to SVG graphics.

In SVG, each drawn shape is remembered as an object. If attributes of an SVG object are changed, the browser can automatically re-render the shape.

Canvas is rendered pixel by pixel. In canvas, once the graphic is drawn, it is forgotten by the browser. If its position should be changed, the entire scene needs to be redrawn, including any objects that might have been covered by the graphic.


Comparison of SVG and Canvas

The table below shows some important differences between Canvas and SVG:

SVGCanvas
Resolution independent Support for event handlers Good text rendering capabilities Slow rendering if complex Not suited for game applicationsResolution dependent No support for event handlers Poor text rendering capabilities You can save the resulting image as .png or .jpg Well suited for graphic-intensive games

SVG Tutorial

To learn more about SVG, please read our SVG Tutorial.

Interactive Code Examples

HTML Example 1

html
Try it Yourself
Interactive runnable HTML example from EA Academy.
Live Output Preview:

HTML Example 2

html
Try it Yourself
Interactive runnable HTML example from EA Academy.
Live Output Preview:

HTML Example 3

html
Try it Yourself
Interactive runnable HTML example from EA Academy.
Live Output Preview:

HTML Example 4

html
Try it Yourself
Interactive runnable HTML example from EA Academy.
Live Output Preview:

HTML Example 5

html
Try it Yourself
Interactive runnable HTML example from EA Academy.
Live Output Preview: