canvas繪畫扇形圖
<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>