怎样设置闭合区域的填充颜色

需要使用: ctx.fillStyle().

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>

<body>
    <canvas id="canv" width="300" height="300"></canvas>
    <script>
        function draw() {
            var canvas = document.getElementById('canv');
            if (!canvas.getContext) return;
            var ctx = canvas.getContext("2d");
            ctx.beginPath();
            ctx.arc(150, 150, 50, 0, -Math.PI/2, true);
            ctx.fillStyle = "red";
            ctx.fill();


            ctx.beginPath();
            ctx.arc(150, 150, 50, -Math.PI/2, -Math.PI, true);
            ctx.fillStyle = "green";
            ctx.fill();

            ctx.beginPath();
            ctx.arc(150, 150, 50, -Math.PI, -Math.PI*3/2, true);
            ctx.fillStyle = "orange";
            ctx.fill();

            ctx.beginPath();
            ctx.arc(150, 150, 50, -Math.PI*3/2, -Math.PI*2, true);
            ctx.fillStyle = "blue";
            ctx.fill();
        }
        draw();
    </script>
</body>

</html>

 

 

注意: 

1. 如果当前Path没有闭合, 则 ctx.fill() 会先闭合再填充;

2. 案例中使用了四次ctx.beginPath(), 说明这个图形下了四笔.

 

posted on 2019-09-23 14:42  aisowe  阅读(704)  评论(0编辑  收藏  举报

导航