Skip to content

Instantly share code, notes, and snippets.

@codingedgar
Last active November 16, 2020 23:54
Show Gist options
  • Select an option

  • Save codingedgar/c8bd2cc6974b9e5810ff01f952d0ce8e to your computer and use it in GitHub Desktop.

Select an option

Save codingedgar/c8bd2cc6974b9e5810ff01f952d0ce8e to your computer and use it in GitHub Desktop.
Draw different shapes to demonstrate arc
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="150" height="200" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML canvas tag.
</canvas>
<script>
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
// Draw shapes
for (let i = 0; i <= 3; i++) {
for (let j = 0; j <= 2; j++) {
ctx.beginPath();
let x = 25 + j * 50; // x coordinate
let y = 25 + i * 50; // y coordinate
let radius = 20; // Arc radius
let startAngle = 0; // Starting point on circle
let endAngle = Math.PI + (Math.PI * j) / 2; // End point on circle
let anticlockwise = i % 2 == 1; // Draw anticlockwise
ctx.arc(x, y, radius, startAngle, endAngle, anticlockwise);
if (i > 1) {
ctx.fill();
} else {
ctx.stroke();
}
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment