canvas绘画扇形图

来源:互联网 发布:python写登录接口 编辑:程序博客网 时间:2024/05/01 16:31
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<div style="height: 100px"></div>
<div style="text-align: center">
    <canvas id="myCanvas" style="border: 1px solid gray;background-color: lightblue" width="600" height="400">
    </canvas>
</div>
<div>
    <input type="button" value="好样的red" onclick="lujing1()"/>
    <input type="button" value="好样的blue" onclick="lujing2()"/>
    <input type="button" value="好样的aqua" onclick="lujing3()"/>
</div>
<script>
    var canvas = document.getElementById('myCanvas');
    var ctx = canvas.getContext('2d');
    function teyong(){
        CanvasRenderingContext2D.prototype.sector = function(x,y,r,angle1,angle2){
            this.save();
            this.beginPath();
            this.moveTo(x,y);
            this.arc(x,y,r,angle1*Math.PI/180,angle2*Math.PI/180,false);
            this.closePath();
            this.restore();
            return this;
        };
    }
function lujing1(){
    teyong();
    ctx.fillStyle = 'red';
    ctx.sector(230,200,100,270,390).fill();
}
    function lujing2(){
        teyong();
        ctx.fillStyle = 'blue';
        ctx.sector(220,200,100,150,270).fill();
    }
    function lujing3(){
        teyong();
        ctx.fillStyle = 'aqua';
        ctx.sector(225,210,100,30,150).fill();
    }


</script>
</body>
</html>
0 0