在做旋转操作之前一定要理解一句话:旋转的是画布的坐标系而不是图形本身,好了,理解了这一句话后,接下来的就很简单了。
首先认识一下画圆的坐标:
复制代码代码如下:
<script language="javascript">
var cxt=document.getElementById('demo').getContext("2d");
cxt.beginPath();
cxt.arc(100,100,50,Math.PI*0.75,Math.PI*1.75,false);
cxt.fillStyle="#F00";
cxt.fill();
cxt.beginPath();
cxt.arc(170,100,50,Math.PI*1.25,Math.PI*0.25,false);
cxt.fillStyle="#F00";
cxt.fill();
cxt.beginPath();
cxt.rotate(45*Math.PI/180);
cxt.fillRect(141.1,-50,100,100);
cxt.fillStyle="#F00";
cxt.fill();
cxt.beginPath();
cxt.rotate(-45*Math.PI/180);
cxt.font="60px 微软雅黑";
cxt.strokeStyle="#f00";
cxt.strokeText("我爱html5",0,300);
cxt.stroke();
var grd=cxt.createLinearGradient(0,45,175,50);
grd.addColorStop(0,"#FF0000");
grd.addColorStop(0.25,"#FFFF00");
grd.addColorStop(0.5,"#00FF00");
grd.addColorStop(0.75,"#00FFFF");
grd.addColorStop(1,"#FFFF00");
cxt.strokeStyle=grd;
cxt.strokeText("我爱canvas",0,400);
cxt.stroke();
</script>
效果图:
