Canvas How to - Fill a path








Question

We would like to know how to fill a path.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>//<![CDATA[ 
window.onload=function(){<!--from www .  ja  va2s  .  c  o  m-->
    var canvas = document.getElementById('c');
    var ctx = canvas.getContext('2d');
    ctx.beginPath();
    ctx.moveTo(100, 100);
    ctx.arc(100, 100, 100, 90 * Math.PI / 180.01, 450 * Math.PI / 180.01);
    ctx.fill();
    ctx.beginPath();
    ctx.moveTo(310, 100);
    ctx.arc(310, 100, 100, 0 * Math.PI / 180, 360 * Math.PI / 180);
    ctx.fill();
}//]]>  
</script>
</head>
<body>
  <canvas id="c" width="410" height="300" />
</body>
</html>

The code above is rendered as follows: