canvas绘制图形——圆弧与圆形

canvas 绘制圆弧

绘制圆弧使用 context.arc( ) 函数,包含六个参数。

context.arc(
	centerx,centery,radius,
    startingAngle,endingAngle,
    anticlockwise = false
)

分别代表:圆心 x 值,圆心 y 值,半径,开始的弧度值,结束的弧度值,(是否逆时针)。

例如:

window: onload = function(){
    var canvas = document.getElementById("canvas");
    var context = canvas.getContext("2d");
    canvas.width = 800;
    canvas.height = 800;

    context.lineWidth = 5;
    context.strokeStyle = "#005588";
    context.arc(300, 300, 200, 0, 1.5*Math.PI)
    context.stroke();
}

当需要绘制多条圆弧时,还是需要调用 context.beginPath( ) 和 context.closePath( ) 。但是当使用 context.closePath( ) 时,会自动将图形封闭,因此如果需要绘制不封闭圆弧,可以省略 context.closePath( )。

绘制实心圆

跟之前的多边形一样,使用 context.fill( ) ,代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <canvas id="canvas"></canvas>
    <script>
        window: onload = function(){
            var canvas = document.getElementById("canvas");
            var context = canvas.getContext("2d");
            canvas.width = 800;
            canvas.height = 800;

            context.lineWidth = 5;
            context.strokeStyle = "#005588";
            context.arc(300, 300, 200, 0, 1.5*Math.PI)
            context.stroke();
            context.fillStyle = "red";
            context.fill();
        }
    </script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/TalonZhang/article/details/82891555