info@tutsfinder.in



HTML SVG



SVG is also a category of HTML graphics which defines vector based graphics in XML format.

SVG stands for Scalable Vector Graphics. SVG is used to define graphics for the web.


The HTML <svg> Element

It is a container for SVG graphics.

SVG has several methods for drawing paths, boxes, circles, text, and graphic images.


Syntax:
<svg width =”100” height = “100”></svg>

SVG Rectangle



Examples:
<!DOCTYPE html>
<html>
<body>

<svg width="500" height="200">
<rect width="500" height="200" 
  style="fill:rgb(0,255,255);stroke-width:10;stroke:rgb(0,0,0)" />
</svg>

</body>
</html>


Output:



SVG Rounded Rectangle



Examples:
<!DOCTYPE html>
<html>
<body>

<svg width="500" height="200">
<rect x="50" y="20" rx="20" ry="20" width="150" height="150"
style="fill:purple;stroke:black;stroke-width:5;opacity:0.5" />
</svg>

</body>
</html>


Output:



SVG Star



Examples for HTML Canvas:

Draw a circle:
<!DOCTYPE html>
<html>
<body>

<svg width="500" height="200">
<polygon points="100,10 40,198 190,78 10,78 160,198"
style="fill:yellow;stroke:purple;stroke-width:5;fill-rule:evenodd;" />
</svg>

</body>
</html>


Output:



As we study two types of Graphic element in HTML SVG and Canvas, let us now study about the difference between both.

SVG is a language for describing 2D graphics in XML.

Canvas draws 2D graphics, on the fly (with a JavaScript).

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

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.