怎样设置路径的边框(描边)颜色

需要使用: ctx.strokeStyle

<!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.strokeStyle = "red";
            ctx.stroke();

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

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

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

</html>

 

注意: 

1. 每次执行 ctx.beginPath() 都相当于是画画时的"落笔"这个动作, 必须要给它指定一个落笔的位置点;

2. ctx.strokeStyle 作用的是当前路径, 也就是最近的一次执行ctx.beginPath()之后绘制的路径; 

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

导航