HTML5 SVG
SVG is defined as Scalable Vector Graphics.
HTML5 supports inline SVG.
HTML
SVG has several ways to draw paths, boxes, circles, text, and graphic images.
What is SVG?
- SVG stands for Scalable Vector Graphics
- SVG is used to define vector-based graphics for the Web
- SVG uses the XML format to define graphics
- The graphical quality of SVG images is not lost when they are enlarged or resized
- SVG is a World Wide Web Consortium standard
Advantages of SVG
Compared with other image formats (such as JPEG and GIF), the advantages of using SVG are:
- SVG images can be created and modified with a text editor
- SVG images can be searched, indexed, scripted, or compressed
- SVG is scalable
- SVG images can be printed with high quality at any resolution
- SVG can be enlarged without any loss of image quality
Browser support
The numbers in the table indicate the first browser version number that supports the element.
| Element | |||||
|---|---|---|---|---|---|
| 4.0 | 9.0 | 3.0 | 3.2 | 10.1 |
Embedding SVG Directly in HTML Pages
In HTML5, you can embed SVG elements directly into HTML pages.
SVG Circle
Example
<
svg
xmlns
=
"
http://www.w3.org/2000/svg
"
version
=
"
1.1
"
<
circle
cx
=
"
100
"
cy
=
"
50
"
r
=
"
40
"
stroke
=
"
black
"
stroke-width
=
"
2
"
fill
=
"
red
"
/>
svgTry it »
Result:
SVG Five-Pointed Star
Example
<!DOCTYPE html><
html
<
body
<
svg
xmlns
=
"
http://www.w3.org/2000/svg
"
version
=
"
1.1
"
height
=
"
190
"
<
polygon
points
=
"100,10 40,180 190,60 10,60 160,180
"
style
=
"
fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;
"
svg
body
html
Try it »
Result:
To learn more about the SVG tutorial, please visit SVG Tutorial。
The Difference Between SVG and Canvas
SVG is a language for describing 2D graphics using XML.
Canvas draws 2D graphics through JavaScript.
SVG is based on XML, which means every element in the SVG DOM is available. You can attach JavaScript event handlers to an element.
In SVG, every drawn graphic is treated as an object. If the properties of an SVG object change, the browser can automatically reproduce the graphic.
Canvas renders pixel by pixel. In canvas, once a graphic has been drawn, it no longer receives the browser's attention. If its position changes, the entire scene needs to be redrawn, including any objects that may have been covered by the graphic.
Comparison of Canvas and SVG
The table below lists some differences between canvas and SVG.
| Canvas | SVG |
|---|---|
|
|
- Resolution independent
- Event handler support
- Best for applications with large rendering areas (such as Google Maps)
- High complexity slows down rendering (any application that overuses DOM is not fast)
- Not suitable for game applications