需要使用: 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()之后绘制的路径; 

评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!