Empty Canvas


Empty Canvas

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

The graphic to the left is created with <canvas>. It shows four elements: a red rectangle, a gradient rectangle, a multicolor rectangle, and a multicolor text.

 

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.

 

 

Canvas Examples

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.

Here is an example of a basic, empty canvas:







Live Demo & Try it yourself!

Draw a Line using HTML Canvas with JavaScript


Draw a Line using HTML Canvas with JavaScript

After creating the rectangular canvas area, you must add a JavaScript to do the drawing.

Here are some examples:







Live Demo & Try it yourself!

Draw a Circle using HTML Canvas with JavaScript


Draw a Circle using HTML Canvas with JavaScript







Live Demo & Try it yourself!

Draw Linear Gradient in Canvas


Draw Linear Gradient in Canvas







Live Demo & Try it yourself!

Draw Circular Gradient in Canvas


Draw Circular Gradient in Canvas







Live Demo & Try it yourself!

Draw Image in Canvas using HTML


Draw Image in Canvas using HTML







Live Demo & Try it yourself!

SVG Circle


SVG Circle

What is SVG?

  • SVG stands for Scalable Vector Graphics
  • SVG is used to define graphics for the Web
  • SVG is a W3C recommendation

The HTML <svg> Element

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

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







Live Demo & Try it yourself!

SVG Logo


SVG Logo







Live Demo & Try it yourself!