CanvasRenderingContext2D
.arc()
方法在
Canvas 2D API
adds a circular arc to the current sub-path.
void ctx.arc(x, y, radius, startAngle, endAngle [, anticlockwise]);
arc()
method creates a circular arc centered at
(x, y)
with a radius of
radius
. The path starts at
startAngle
, ends at
endAngle
, and travels in the direction given by
anticlockwise
(defaulting to clockwise).
x
The horizontal coordinate of the arc's center.
y
The vertical coordinate of the arc's center.
radius
The arc's radius. Must be positive.
startAngle
The angle at which the arc starts in radians, measured from the positive x-axis.
endAngle
The angle at which the arc ends in radians, measured from the positive x-axis.
anticlockwise
可选
布尔
。若
true
, draws the arc counter-clockwise between the start and end angles. The default is
false
(clockwise).
This example draws a complete circle with the
arc()
方法。
<canvas></canvas>
The arc is given an x-coordinate of 100, a y-coordinate of 75, and a radius of 50. To make a full circle, the arc begins at an angle of 0 radians (0 ° ), and ends at an angle of 2π radians (360 ° ).
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.arc(100, 75, 50, 0, 2 * Math.PI);
ctx.stroke();
This example draws various shapes to show what is possible with
arc()
.
<canvas width="150" height="200"></canvas>
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();
}
}
}
| Screenshot | Live sample |
|---|---|
|
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'CanvasRenderingContext2D.arc' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
arc
|
Chrome 1 | Edge 12 | Firefox 1.5 | IE 9 | Opera 11.6 | Safari 3 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 1 | Samsung Internet Android 1.0 |
完整支持
CanvasRenderingContext2D
CanvasRenderingContext2D.ellipse()
to draw an elliptical arc.
CanvasRenderingContext2D
addHitRegion()
arc()
arcTo()
beginPath()
bezierCurveTo()
clearHitRegions()
clearRect()
clip()
closePath()
createImageData()
createLinearGradient()
createPattern()
createRadialGradient()
drawFocusIfNeeded()
drawImage()
drawWidgetAsOnScreen()
drawWindow()
ellipse()
fill()
fillRect()
fillText()
getImageData()
getLineDash()
getTransform()
isPointInPath()
isPointInStroke()
lineTo()
measureText()
moveTo()
putImageData()
quadraticCurveTo()
rect()
removeHitRegion()
resetTransform()
restore()
rotate()
save()
scale()
scrollPathIntoView()
setLineDash()
setTransform()
stroke()
strokeRect()
strokeText()
transform()
translate()