Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML5 SVG

HTML5 SVG


SVG is defined as Scalable Vector Graphics.

HTML5 supports inline SVG.

HTML The element is a container for SVG graphics.

SVG has several ways to draw paths, boxes, circles, text, and graphic images.

SVG Sorry, your browser does not support inline SVG.

What is SVG?


Advantages of SVG

Compared with other image formats (such as JPEG and GIF), the advantages of using SVG are:


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

"

/>

svg

Try 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 dependent
  • No event handler support
  • Poor text rendering capability
  • Able to save the resulting image in .png or .jpg format
  • Best for image-intensive games, in which many objects are redrawn frequently
  • | |