canvas的arc绘制弧形

1.绘制弧形

context.arc(
    centerx, centery, radius,//圆点坐标和半径
    startingAngle,endingAngle,//起始弧度,结束弧度
    anticlockwise = false//默认顺时针,绘制路径的方式很重要,因为fill会根据非零环绕原则来进行填充,可以达到剪纸等效果
)

startingAngle和endingAngle对应的图

eg:

<!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>canvas画弧形</title>
</head>

<body>
    <canvas id="canvas">浏览器不支持canvas,请更换浏览器</canvas>
    <script type="text/javascript">
        window.onload = function() {
            var canvas = document.getElementById('canvas');
            canvas.width = 1024;
            canvas.height = 768;
            var context = canvas.getContext('2d');
            // 绘制状态
            context.arc(300, 300, 200, 0, 1.5 * Math.PI);
            context.lineWidth = 5;
            context.strokeStyle = 'blue';
            context.stroke();
        }
    </script>
</body>

</html>

运行结果:

2.beginPath()和closePath()不用成对出现。

beginPath()代表重新规划一个路径;

closePath()代表要结束当前的路径,如果当前路径没封闭,会自动封闭当前路径,如果不想要封闭,则使用beginPath()就好了,不用使用closePath()。

closePath()对fill()不起作用。因为fill()也会自动封闭当前路径,然后填充。

猜你喜欢

转载自blog.csdn.net/DreamFJ/article/details/81449212
今日推荐