Tutorials Point

  HTML-5 Tutorial
  HTML5 Tag Reference
  HTML5 Useful References
  HTML5 Tools
  HTML5 Resources
  Selected Reading

Copyright © 2014 by tutorialspoint

  Home     References     Discussion Forums     About TP  

HTML5 Canvas - Drawing Paths

previous next AddThis Social Bookmark Button


There are following methods required to draw paths on the canvas:

SNMethod and Description
This method resets the current path.
2moveTo(x, y)
This method creates a new subpath with the given point.
This method marks the current subpath as closed, and starts a new subpath with a point the same as the start and end of the newly closed subpath.
This method fills the subpaths with the current fill style.
This method strokes the subpaths with the current stroke style.
6arc(x, y, radius, startAngle, endAngle, anticlockwise)
Adds points to the subpath such that the arc described by the circumference of the circle described by the arguments, starting at the given start angle and ending at the given end angle, going in the given direction, is added to the path, connected to the previous point by a straight line.


Following is a simple example which makes use of above mentioned methods to draw a shape.

<script type="text/javascript">
function drawShape(){
  // get the canvas element using the DOM
  var canvas = document.getElementById('mycanvas');

  // Make sure we don't execute when canvas isn't supported
  if (canvas.getContext){

    // use getContext to use the canvas for drawing
    var ctx = canvas.getContext('2d');

    // Draw shapes
    ctx.arc(75,75,50,0,Math.PI*2,true);  // Outer circle
    ctx.arc(75,75,35,0,Math.PI,false);   // Mouth
    ctx.arc(60,65,5,0,Math.PI*2,true);  // Left eye
    ctx.arc(90,65,5,0,Math.PI*2,true);  // Right eye

  } else {
    alert('You need Safari or Firefox 1.5+ to see this demo.');
<body onload="drawShape();">
   <canvas id="mycanvas"></canvas>

The above example would draw following shape:

Canvas Paths

To learn above concept - Do Online Practice using latest version of either Safari or Opera.

previous next Printer Friendly