Home Web Front-end H5 Tutorial html5 Canvas drawing tutorial (5)—arc method of drawing curves in canvas_html5 tutorial skills

html5 Canvas drawing tutorial (5)—arc method of drawing curves in canvas_html5 tutorial skills

May 16, 2016 pm 03:50 PM
canvas

In this article Drawing Lines on Canvas, I talked about how to draw straight lines. Logically, this article on drawing curves should have been published long ago, but since drawing curves on canvas is quite special, I haven’t figured it out yet, so Try it step by step.
One of the difficulties in drawing curves in canvas is that there are only 4 functions for curves! They are arc, arcTo, quadraticCurveTo, and bezierCurveTo. Let me start with the simplest arc method.
The function of arc is to draw a regular arc, which can be a complete circle or a certain arc of a circle. The syntax for arc is as follows: 🎜>

context.arc(x, y, radius, startAngle, endAngle, anticlockwise)
I will explain its parameters, that is,
arc(center of circle x, center of circle y, radius, starting angle, ending angle, counterclockwise or not)

What should we do if we use arc to draw a complete circle? Everyone noticed that there is a start angle and an end angle in the parameters. If our start angle is 0 and the end angle is 360, isn't it a perfect circle?
Correct answer! But it should be noted that the angle here is expressed in "radians" (the same is true for Flash). A complete circle is 360 degrees, which is 2PI radians. So we write like this:


Copy the code


The code is as follows:


ctx.arc(400,400,20,0,Math.PI*2); ctx.fill(); ctx.stroke();

Like lineTo, arc is also a drawing path, so we have to call the fill or stroke method behind it to display the graphics (red strokeStyle and translucent red fillStyle are used in the picture).
Now let’s draw a 1/4 circle, the angle is 90 degrees. As mentioned before, a 360-degree angle is 2PI radians, so a one-degree angle is 2PI/360=PI/180 radians, then 90 degrees is 2PI/360*90=PI/2 radians (please calculate other angles by yourself).



Copy code

The code is as follows:

ctx.arc(400,400,20,0 ,Math.PI*2/4);




From the figure, we can determine that 0 degrees of arc is 0 degrees commonly used in mathematics, but the angle defaults to The hour hand is open, which is opposite to the mathematical model (it is related to the coordinates, because the canvas coordinates are also opposite to the mathematical coordinates). But isn’t there a parameter in front to determine whether it is counterclockwise? How about we set it to true?
Copy code

The code is as follows:

ctx.arc(400,400,20,0 ,Math.PI*2/4,true);



You will see that the angle becomes counterclockwise, causing the arc to become 360 -90=270 degrees. But! One thing everyone should pay attention to is that the calculation method of the starting point and the end point is always starting from 0 degrees and extending clockwise. There is no such thing as forward and reverse. Clockwise and counterclockwise are just the directions in which arcs are drawn. This not only prevents confusion from going back and forth, but also makes calculations easier. However, using counterclockwise flexibly can sometimes be useful. In the above example, our starting angles are all 0. Now let’s try other starting angles, such as 90 degrees.


Copy code

The code is as follows:


ctx.arc(400,400,20,Math .PI*2/4,Math.PI*2 Math.PI);


If our starting point is 90 degrees and the end point is also 90 degrees, the result is that nothing can be drawn, so I put the end point The angle was changed to 180 degrees, and finally the graph below was obtained.
Question: If we draw a complete circle from a starting point other than 0 degrees, is it okay? Of course you can, as long as you set the end point of the arc to the 360-degree starting angle, such as:


Copy code


Code As follows:

ctx.arc(400,400,20,Math.PI*2/4,Math.PI*2 Math.PI*2/4); //The starting point is 90 degrees, the end point is 360 90 Degree
However, this approach is just looking for trouble, and normal people would not use it.
Summary: The arc method of Canvas is a way to draw a positive arc. It can only draw positive arcs and cannot draw other strange arcs, such as S-shape - although I like it best S-shaped.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Which schools use canvas? Which schools use canvas? Aug 18, 2023 pm 05:59 PM

Schools using canvas include Stanford University, MIT, Columbia University, University of California, Berkeley, etc. Detailed introduction: 1. Stanford University uses Canvas as its main online learning platform. Teachers and students at Stanford University use Canvas to manage and communicate course content, and learn through functions such as online discussions, assignment submissions, and exams; 2. Ma Provincial Polytechnic Institute and MIT also use Canvas as their online learning management system and conduct course management through the Canvas platform; 3. Columbia University, etc.

What are the canvas arrow plug-ins? What are the canvas arrow plug-ins? Aug 21, 2023 pm 02:14 PM

The canvas arrow plug-ins include: 1. Fabric.js, which has a simple and easy-to-use API and can create custom arrow effects; 2. Konva.js, which provides the function of drawing arrows and can create various arrow styles; 3. Pixi.js , which provides rich graphics processing functions and can achieve various arrow effects; 4. Two.js, which can easily create and control arrow styles and animations; 5. Arrow.js, which can create various arrow effects; 6. Rough .js, you can create hand-drawn arrows, etc.

What are the details of the canvas clock? What are the details of the canvas clock? Aug 21, 2023 pm 05:07 PM

The details of the canvas clock include clock appearance, tick marks, digital clock, hour, minute and second hands, center point, animation effects, other styles, etc. Detailed introduction: 1. Clock appearance, you can use Canvas to draw a circular dial as the appearance of the clock, and you can set the size, color, border and other styles of the dial; 2. Scale lines, draw scale lines on the dial to represent hours or minutes. Position; 3. Digital clock, you can draw a digital clock on the dial to indicate the current hour and minute; 4. Hour hand, minute hand, second hand, etc.

What versions of html2canvas are there? What versions of html2canvas are there? Aug 22, 2023 pm 05:58 PM

The versions of html2canvas include html2canvas v0.x, html2canvas v1.x, etc. Detailed introduction: 1. html2canvas v0.x, which is an early version of html2canvas. The latest stable version is v0.5.0-alpha1. It is a mature version that has been widely used and verified in many projects; 2. html2canvas v1.x, this is a new version of html2canvas.

Learn the canvas framework and explain the commonly used canvas framework in detail Learn the canvas framework and explain the commonly used canvas framework in detail Jan 17, 2024 am 11:03 AM

Explore the Canvas framework: To understand what are the commonly used Canvas frameworks, specific code examples are required. Introduction: Canvas is a drawing API provided in HTML5, through which we can achieve rich graphics and animation effects. In order to improve the efficiency and convenience of drawing, many developers have developed different Canvas frameworks. This article will introduce some commonly used Canvas frameworks and provide specific code examples to help readers gain a deeper understanding of how to use these frameworks. 1. EaselJS framework Ea

uniapp implements how to use canvas to draw charts and animation effects uniapp implements how to use canvas to draw charts and animation effects Oct 18, 2023 am 10:42 AM

How to use canvas to draw charts and animation effects in uniapp requires specific code examples 1. Introduction With the popularity of mobile devices, more and more applications need to display various charts and animation effects on the mobile terminal. As a cross-platform development framework based on Vue.js, uniapp provides the ability to use canvas to draw charts and animation effects. This article will introduce how uniapp uses canvas to achieve chart and animation effects, and give specific code examples. 2. canvas

What properties does tkinter canvas have? What properties does tkinter canvas have? Aug 21, 2023 pm 05:46 PM

The tkinter canvas attributes include bg, bd, relief, width, height, cursor, highlightbackground, highlightcolor, highlightthickness, insertbackground, insertwidth, selectbackground, selectforeground, xscrollcommand attributes, etc. Detailed introduction

Explore the powerful role and application of canvas in game development Explore the powerful role and application of canvas in game development Jan 17, 2024 am 11:00 AM

Understand the power and application of canvas in game development Overview: With the rapid development of Internet technology, web games are becoming more and more popular among players. As an important part of web game development, canvas technology has gradually emerged in game development, showing its powerful power and application. This article will introduce the potential of canvas in game development and demonstrate its application through specific code examples. 1. Introduction to canvas technology Canvas is a new element in HTML5, which allows us to use

See all articles