/

HTML / CSS / JavaScript Tutorial

JavaScript canvas2d object method: arc()

[this page | pdf | back links]

The arc() method of the JavaScript DOM object returned by the getContext("2d") method applied to the HTML <canvas> element creates a circular arc.

 

It has the following syntax with the following parameters.

 

context.arc(x, y, r, startAngle, endAngle, counterclockwise)

 

Parameter

Required / Optional

Description

x

Required

x-coordinate or circle centre

y

Required

y-coordinate or circle centre

r

Required

Radius of circle

startAngle

Required

Start angle of arc in radians from x-axis

endAngle

Required

End angle of arc in radians from x-axis

counterclockwise

Optional

(default is false). Boolean, if true then draw arc counterclockwise, otherwise clockwise

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head></head>
<body>
<span id="element"></span>

<script>
var x2 = document.createElement("CANVAS");
x2.setAttribute("width", "200");
x2.setAttribute("height", "200");
x2.setAttribute("style", "border: 1px solid black");
var c2 = x2.getContext("2d");
c2.strokeStyle = "red";
c2.beginPath();
c2.arc(35, 45, 30, 0, 1.8 * Math.PI);
c2.stroke();
c2.scale(1.5, 1);
c2.strokeStyle = "blue";
c2.beginPath();
c2.arc(85, 45, 30, 0, 1.8 * Math.PI);
c2.stroke();
document.getElementById("element").appendChild(x2);
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodCanvas2dArc() {
  var x = document.createElement("CANVAS"); var c = x.getContext("2d"); return !!c.arc;
}


NAVIGATION LINKS
Contents | Prev | Next | JavaScript DOM (and BOM)


Desktop view | Switch to Mobile