IT源码网

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

shasha 2021年03月09日 编程语言 404 0

需要使用: 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(), 说明这个图形下了四笔.

 

评论关闭
IT源码网

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

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