Draw a path with arc in JavaScript

Description

The following code shows how to draw a path with arc.

Example


<!--from   ww w .j  a  va  2  s. c o m-->
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<canvas id="canvas" width="500" height="140"></canvas>
<script>
var ctx = document.getElementById("canvas").getContext("2d");
ctx.fillStyle = "yellow";
ctx.lineWidth = "3";
ctx.beginPath();
ctx.arc(200, 70, 60, 0, Math.PI * 2, true);
ctx.stroke();
ctx.beginPath();
ctx.arc(200, 70, 60, Math.PI / 2, Math.PI, true);
ctx.fill();
ctx.stroke();
ctx.beginPath();
var val = 0;
for ( var i = 0; i < 4; i++) {
ctx.arc(50, 70, 60, val, val + Math.PI / 4, false);
val += Math.PI / 2;
}
ctx.closePath();
ctx.fill();
ctx.stroke();
</script>
</body>
</html>

Click to view the demo

The code above generates the following result.

Draw a path with arc in JavaScript