Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to make a circle in JavaScript

Understanding the Basics

Before we delve into making a circle in JavaScript, it's important to understand the basics. JavaScript is a programming language that allows you to implement complex features on web pages. In this context, we are going to use JavaScript to draw a circle on a web page. Just like an artist needs a canvas to paint, in programming, especially in JavaScript, we also need a "canvas". So, what is a canvas in JavaScript?

A canvas is simply a rectangular area in the HTML where we can draw graphics, just like an artist's canvas but in a digital form. It's like a playground where we can draw shapes, add colors, and make our digital artwork come to life.

Setting up the Canvas

To start our journey of making a circle, we first need to set up our canvas. Here is a simple way to do this:

var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');

In the above code, we first get a reference to the canvas using the document.getElementById method. Think of this like finding your canvas in a pile of canvases based on a unique identifier.

Next, we use the getContext('2d') method. This method returns a drawing context on the canvas. Think of it as getting your brush ready to paint on the canvas. The '2d' represents two-dimensional rendering context.

Drawing a Circle

Now that we have our canvas set up, it's time to draw a circle. But how do we draw a circle in JavaScript?

Well, JavaScript provides us with a method called arc(). It's like a special tool in our toolkit that helps us draw arcs or in our case, a full circle.

The arc() method takes six parameters: - x: the x-coordinate of the center of the circle - y: the y-coordinate of the center of the circle - radius: the radius of the circle - startAngle: the starting point of the circle in radians - endAngle: the end point of the circle in radians - anticlockwise: a Boolean value that determines the direction in which the circle is drawn.

What is a radian you ask? Imagine you're eating a pizza. The pizza is a circle and each slice is an arc. The angle of each slice at the tip is measured in radians.

Here's an example of how to draw a circle:

context.arc(100, 100, 50, 0, 2 * Math.PI, false);
context.fillStyle = 'green';
context.lineWidth = 5;
context.strokeStyle = '#003300';

In this example, we first call the beginPath() method. This method is like saying to your brush, "Hey, I want to start a new path or a new drawing."

Then, we call the arc() method with the parameters to draw a circle. The circle will be at coordinates (100, 100), have a radius of 50, start at 0 radians and end at 2 * Math.PI radians (which is a full circle), and will be drawn in the clockwise direction (since false is given).

The fillStyle property sets the color of the circle, and the fill() method actually colors the circle with the specified color. Think of it as choosing the paint color and then filling the circle with that color.

The lineWidth property sets the width of the stroke (outline of the circle), and the strokeStyle property sets the color of the stroke. The stroke() method then applies the stroke to the circle. This is like choosing the pencil to outline your circle and then actually outlining it.


JavaScript is like a digital artist's toolkit. It offers a variety of tools and methods, like arc(), to create and manipulate graphics. Drawing a circle may seem like a small step, but it's a leap in understanding how JavaScript can be used to interact with the HTML canvas.

Remember, every complex application starts with simple steps like these. So, don't worry if you're just drawing a circle now. Soon, you'll be painting a masterpiece in your canvas. So, keep coding, keep experimenting, and most importantly, keep having fun. After all, programming is an art, and every artist needs to start somewhere!