Canvas and JavaScript. Arcs and Circles

Canvas and JavaScript. Arcs and Circles

Today we will learn how to draw arcs and circles. This can be done using the method arcwhich draws an arc from the center at a given point with coordinates x, y… Depending on the value of the specified angle of the circle, we can get an arc or a full circle. Uses the value to calculate the circumference Math.PI… These shapes will become visible if you apply the methods to them stroke (outline) or (figure).

Draw an arc

Method arc () let’s pass the following parameters:

  • X, Y – coordinates of the center of the circle
  • radius – radius of the circle
  • startAngle – the starting angle of the circle
  • endAngle – end angle of the circle
  • anticlockwise – drawing direction, false (clockwise)

#drawRound {
    width: 600px;
    height: 300px;
    margin: 40px;
    background-image: url(grid.png);
}

// Получение элемента canvas, контекста и свойства Math.PI
let canvas = document.getElementById('drawRound');
let ctx = canvas.getContext('2d');
let pi = Math.PI;

ctx.beginPath();// начало нового пути
ctx.lineWidth = 7; // толщина обводки
ctx.strokeStyle = "green"; // цвет обводки
// Координаты центра круга, радиус, начальный угол, конечный угол, направление по часовой стрелке
ctx.arc(100, 100, 75, 0, pi/2, false );
ctx.stroke();

Canvas and JavaScript. Arcs and Circles.

If we replace false on true, then the direction of sweeping the arc will go counterclockwise.


ctx.arc(100, 100, 75, 0, pi/2, true);

Canvas and JavaScript. Arcs and Circles.

Draw half a circle

The angle of the circle depends on the number PI… Half a circle corresponds to 180 degrees or an integer PI.


ctx.arc(150, 150, 75, 0, pi, true); // против часовой стрелке

Canvas and JavaScript. Arcs and Circles.


ctx.arc(150, 150, 75, 0, pi, false); // по часовой стрелке

Canvas and JavaScript. Arcs and Circles.

Full circle


ctx.beginPath();
ctx.lineWidth = 7;
ctx.strokeStyle = "green";
ctx.fillStyle="yellow";
ctx.arc(150, 150, 85, 0, 2*pi, false);
ctx.stroke();
ctx.fill();

Canvas and JavaScript. Arcs and Circles.

Overlay circles

Nothing prevents you from superimposing circles or arcs on top of each other, just manipulating the coordinates.


ctx.beginPath();
ctx.lineWidth = 7;
ctx.strokeStyle = "green";
ctx.fillStyle="yellow";
ctx.arc(150, 150, 85, 0, 2*pi, false);
ctx.stroke();
ctx.fill();

ctx.beginPath();
ctx.lineWidth = 7;
ctx.strokeStyle = "gray";
ctx.fillStyle="pink";
ctx.arc(250, 150, 85, 0, 2*pi, false );
ctx.stroke();
ctx.fill();

Canvas and JavaScript. Arcs and Circles.

JavaScript is an amazingly versatile programming language and you can’t do without it in web development. And why resist when there is such a wonderful JavaScript video coursewhere educational material is presented in a simple and understandable language.

Previous article Next article

Copying of materials is allowed only with the indication of the author (Mikhail Rusakov) and an indexed direct link to the site (http://myrusakov.ru)!

Add to my friends VK: http://vk.com/myrusakov.
If you want to rate me and my work, then write it in my group: http://vk.com/rusakovmy.

If you do not want to miss new materials on the site,
then you can subscribe to updates: Subscribe to updates

If you still have any questions, or you have a desire to comment on this article, then you can leave your comment at the bottom of the page.

Recommend this article to your friends:

If you liked the site, then post a link to it (on your site, on the forum, in contact):

  1. Button:

    It looks like this: How to create your website

  2. Text link:

    It looks like this: How to create your website

  3. BB-code of the link for forums (for example, you can put it in the signature):

Related Posts

Property Management in Dubai: Effective Rental Strategies and Choosing a Management Company

“Property Management in Dubai: Effective Rental Strategies and Choosing a Management Company” In Dubai, one of the most dynamically developing regions in the world, the real estate…

In Poland, an 18-year-old Ukrainian ran away from the police and died in an accident, – media

The guy crashed into a roadside pole at high speed. In Poland, an 18-year-old Ukrainian ran away from the police and died in an accident / illustrative…

NATO saw no signs that the Russian Federation was planning an attack on one of the Alliance countries

Bauer recalled that according to Article 3 of the NATO treaty, every country must be able to defend itself. Rob Bauer commented on concerns that Russia is…

The Russian Federation has modernized the Kh-101 missile, doubling its warhead, analysts

The installation of an additional warhead in addition to the conventional high-explosive fragmentation one occurred due to a reduction in the size of the fuel tank. The…

Four people killed by storm in European holiday destinations

The deaths come amid warnings of high winds and rain thanks to Storm Nelson. Rescuers discovered bodies in two separate incidents / photo ua.depositphotos.com Four people, including…

Egg baba: a centuries-old recipe of 24 yolks for Catholic Easter

They like to put it in the Easter basket in Poland. However, many countries have their own variations of “bab”. The woman’s original recipe is associated with…

Leave a Reply

Your email address will not be published. Required fields are marked *