Posts in current category

HTML5 Inline SVG


May 03, 2021 13:05 HTML5


Table of contents


HTML5 Inline SVG

SVG represents scalable vector graphics, which are based on extensable markup languages (a subset of standard common markup languages) and are used to describe a graphical format for two-dimensional vector graphics, which became the W3C recommendation on January 14, 2003.

HTML5 supports inline SVG.

HTML5 Inline SVG


What is SVG?

  • SVG refers to scalable vector graphics (Scalable Vector Graphics)

  • SVG is used to define vector-based graphics for networks

  • SVG uses the XML format to define graphics

  • SVG images do not lose their graphic quality when zoomed in or changed in size

  • SVG is the standard of the World Wide Web Alliance

  • SVG is a whole with W3C standards such as DOM and XSL


SVG benefits

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

  • SVG images can be created and modified through the text editor

  • SVG images can be searched, indexed, scripted, or compressed

  • The SVG is scalable

  • SVG images can be printed at any resolution with high quality

  • SVG can be magnified without decreasing image quality


Browser support

HTML5 Inline SVG HTML5 Inline SVG HTML5 Inline SVG HTML5 Inline SVG HTML5 Inline SVG

Internet Explorer 9 Plus, Firefox, Opera, Chrome, and Safari support inline SVG.


Embed SVG directly into the HTML page

In HTML5, you can embed SVG elements directly into HTML pages:

<!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 out . . .

To learn more about the SVG tutorial, visit the SVG tutorial .


The difference between SVG and Canvas

SVG is a language that uses XML to describe 2D graphics.

Canvas draws 2D graphics using JavaScript.

SVG is based on XML, which means that every element in the SVG DOM is available. You can attach a JavaScript event processor to an element.

In SVG, each drawing is treated as an object. If the properties of the SVG object change, the browser can automatically reproduce the graphic.

Canvas is rendered pixel by pixel. I n canvas, once the graphic is drawn, it does not continue to receive browser attention. If its position changes, the entire scene also needs to be redrawn, including any objects that might have been covered by the drawing.


Canvas's comparison with SVG

The following table lists some of the differences between canvas and SVG.

Canvas SVG
  • 依赖分辨率

  • 不支持事件处理器

  • 弱的文本渲染能力

  • 能够以 .png 或 .jpg 格式保存结果图像

  • 最适合图像密集型的游戏,其中的许多对象会被频繁重绘

  • 不依赖分辨率

  • 支持事件处理器

  • 最适合带有大型渲染区域的应用程序(比如谷歌地图)

  • 复杂度高会减慢渲染速度(任何过度使用 DOM 的应用都不快)

  • 不适合游戏应用