HTML SVG: SVG defines vector-based graphics in XML format.


In this tutorial, you will learn-

What is SVG?

• SVG stands for Scalable Vector Graphics

• SVG is used to define graphics for the Web

• SVG is a W3C recommendation


For what reason would it be a good idea for you to use SVG pictures?

There are various reasons to use SVG pictures, some of which are:

  • SVG images do not lose their quality when zoomed or resized.
  • They can be created and edited with an IDE or text editor.
  • They are accessible and animatable.
  • They have a small file size and are highly scalable.
  • And they can be searched, indexed, scripted, and compressed.

The HTML <svg> Element

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

SVG has a few techniques for drawing ways, boxes, circles, text, and graphic pictures.


SVG Circle

Example

Sorry, your browser does not support inline SVG.
<!DOCTYPE html>
<html>
<body>

<svg width="100" height="100">
  <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>

</body>
</html>

SVG Rectangle

Example

Sorry, your browser does not support inline SVG.
<svg width="400" height="100">
  <rect width="400" height="100" style="fill:rgb(0,0,255);stroke-width:10;stroke:rgb(0,0,0)" />
</svg>

SVG Rounded Rectangle

Sorry, your browser does not support inline SVG.
<svg width="400" height="180">
  <rect x="50" y="20" rx="20" ry="20" width="150" height="150"
  style="fill:red;stroke:black;stroke-width:5;opacity:0.5" />
</svg>

SVG Star

Sorry, your browser does not support inline SVG.
<svg width="300" height="200">
  <polygon points="100,10 40,198 190,78 10,78 160,198"
  style="fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;" />
</svg>

SVG Sorry, your browser does not support inline SVG.
<svg height="130" width="500">
  <defs>
    <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
      <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
    </linearGradient>
  </defs>
  <ellipse cx="100" cy="70" rx="85" ry="55" fill="url(#grad1)" />
  <text fill="#ffffff" font-size="45" font-family="Verdana" x="50" y="86">SVG</text>
  Sorry, your browser does not support inline SVG.
</svg>

Differences Between SVG and Canvas

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

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

SVG is XML based, which means that every element is accessible inside the SVG DOM. You can append JavaScript occasion handlers for an element.

In SVG, each drawn shape is recognized as an object. On the off chance that attributes of a SVG object are changed, the browser can automatically re-render the shape.

Canvas is delivered pixel by pixel. In canvas, when the graphic is drawn, it is forgotten by the browser. On the off chance that its position ought to be changed, the whole scene should be redrawn, including any objects that may have been covered by the graphic.


Comparison of Canvas and SVG

The table underneath shows some significant differences among Canvas and SVG:

CanvasSVG
Resolution dependentNo support for event handlersPoor text rendering capabilitiesYou can save the resulting image as .png or .jpgWell suited for graphic-intensive gamesResolution independentSupport for event handlersBest suited for applications with large rendering areas (Google Maps)Slow rendering if complex (anything that uses the DOM a lot will be slow)Not suited for game applications

Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your projects with us.