admin管理员组

文章数量:1332389

var c = document.getElementById("myCanvas");
var cir = c.getContext("2d");
cir.beginPath();
cir.arc(100, 100, 50, 0, 2* Math.PI );
cir.stroke();

or

var c = document.getElementById("myCanvas");
var cir = c.getContext("2d");
cir.beginPath();
cir.arc(100, 100, 50, 0, 6.14);
cir.stroke();

This two code can draw a full circle so in this why we are using pi value in this ?

var c = document.getElementById("myCanvas");
var cir = c.getContext("2d");
cir.beginPath();
cir.arc(100, 100, 50, 0, 2* Math.PI );
cir.stroke();

or

var c = document.getElementById("myCanvas");
var cir = c.getContext("2d");
cir.beginPath();
cir.arc(100, 100, 50, 0, 6.14);
cir.stroke();

This two code can draw a full circle so in this why we are using pi value in this ?

Share edited Sep 25, 2013 at 5:07 Tepken Vannkorn 9,72314 gold badges62 silver badges86 bronze badges asked Sep 25, 2013 at 4:54 user2578480user2578480 3
  • 1 that is weird question... ever heard about geometry? – Flash Thunder Commented Sep 25, 2013 at 5:05
  • 1 By using a value more than 2*Math.PI you just overdraw the circle. However by using values less that 2*Math.PI you can create unplete circle (exactly what is says - arc). This is how it works: w3schools./tags/img_arc.gif – sybear Commented Sep 25, 2013 at 5:09
  • 1 create a circle with bigger radius and you'll able to notice that 6.14 does not create full circle. The difference is so small now , that you are not seeing it with naked eyes. Zoom in the page, it may help. – Jashwant Commented Sep 25, 2013 at 5:14
Add a ment  | 

4 Answers 4

Reset to default 8

Angles can be measured with a variety of different measures. The most mon are:

  • Degrees, where 360 degrees equals one full circle
  • Radians, where the mathematical constant π (Greek letter Pi) — which is roughly 3.14159265 — is one half circle, and a full circle is .

The arc(x, y, radius, startAngle, endAngle, anticlockwise) function takes angles in radians, so passing the values startAngle=0 and endAngle=2*Math.PI will draw a plete circle with radius radius from point (x,y).

Edit: The line of code that you posted:

cir.arc(100, 100, 50, 0, 6.14);

Will draw most of a circle, as 6.14 < 2π. 2π is approximately 6.283185, so the arc will be about 98% of a full circle.

Its using Radians as the unit of measure to describe the angle of the arc. This is very mon in programming languages, especially in graphics programming. A full circle, 360 degrees, is equal to 2 * PI Radians.

Value of Math.PI: 3.141592653589793 is more accurate. And cir.arc(100, 100, 50, 0, 6.14); does not make full circle.

The second code does not draw a full circle: there is a gap on the right. The reason is that 6.14 is less than a full circle (in radians), which is 2π, about 6.28. It is mon to use the property Math.PI for π in JavaScript, since it gives a very accurate approximation (the most accurate approximation achievable using a double precision floating point number in the system).

P.S. This is not about the canvas tag but about JavaScript code that draws on a canvas specified by a canvas element.

本文标签: javascriptWhy html5 canvas tag uses PI value to draw circleStack Overflow