HTML Graphics - SwatiMaurya08/html-notes GitHub Wiki

HTML Graphics

HTML Canvas

The HTML <canvas> element is used to draw graphics on a web page.

What is HTML Canvas?

The HTML <canvas> element is used to draw graphics, on the fly, via JavaScript.

The <canvas> element is only a container for graphics. You must use JavaScript to actually draw the graphics.

Canvas has several methods for drawing paths, boxes, circles, text, and adding images.

A canvas is a rectangular area on an HTML page. By default, a canvas has no border and no content.

The markup looks like this:

<canvas id="myCanvas" width="200" height="100"></canvas>

Note: Always specify an id attribute (to be referred to in a script), and a width and height attribute to define the size of the canvas. To add a border, use the style attribute.

HTML SVG

What is SVG?

  1. SVG stands for Scalable Vector Graphics.
  2. SVG is used to define graphics for the Web.
  3. It basically defines vector-based graphics in XML format.
  4. SVG graphics do NOT lose any quality if they are zoomed or resized.
  5. Every element and every attribute in SVG files can be animated.

What are the advantages of SVG?

Advantages of using SVG over other image formats (like JPEG and GIF) are:

  1. SVG images can be created and edited with any text editor.
  2. SVG images can be searched, indexed, scripted, and compressed.
  3. SVG images are scalable.
  4. SVG images can be printed with high quality at any resolution.

Differences between HTML SVG and HTML Canvas

  1. SVG is a language for describing 2D graphics in XML whereas Canvas draws 2D graphics, on the fly with a JavaScript.
  2. If attributes of an SVG object are changed, the browser can automatically re-render the shape whereas Canvas is rendered pixel by pixel.In canvas, once the graphic is drawn, it is forgotten by the browser.
  3. SVG is resolution independent whereas CANVAS is resolution dependent.
  4. SVG supports event handlers whereas CANVAS doesnt have a support for event handlers.
⚠️ **GitHub.com Fallback** ⚠️