标签:绘图
JS绘图基本流程如下:
var c=document.getElementById(‘myCanvas‘)
var cxt=c.getContext(‘2d‘)
canvas绘图相关对象
html代码:
<body>
<p>
<span onclick="btn1_click();" onmouseover="this.style.cursor=‘pointer‘;">实体圆</span>
<span onclick="btn2_click();" onmouseover="this.style.cursor=‘pointer‘;">边框圆</span>
<span onclick="btn3_click();" onmouseover="this.style.cursor=‘pointer‘;">衔接圆</span>
<span onclick="btn4_click();" onmouseover="this.style.cursor=‘pointer‘;">渐变圆</span>
</p>
<canvas id="myCanvas" width="400px" height="400px"></canvas>
</body>
js代码:
<script type="text/javascript">
function $(id)
{
return document.getElementById(id);
}
function btn1_click(){
var cvs=$(‘myCanvas‘);
var cxt=cvs.getContext(‘2d‘);
cxt.clearRect(0,0,400,400);
cxt.beginPath();
cxt.arc(100,100,50,0,Math.PI*2,true);
cxt.closePath();
cxt.fillStyle=‘#eee‘;
cxt.fill();
}
function btn2_click(){
var cvs=$(‘myCanvas‘);
var cxt=cvs.getContext(‘2d‘);
cxt.clearRect(0,0,400,400);
cxt.beginPath();
cxt.arc(100,100,50,0,Math.PI*2,true);
cxt.closePath();
cxt.strokeStyle=‘#666‘;
cxt.lineWidth=2;
cxt.stroke();
}
function btn3_click(){
var cvs=$(‘myCanvas‘);
var cxt=cvs.getContext(‘2d‘);
cxt.clearRect(0,0,400,400);
cxt.beginPath();
cxt.arc(100,100,50,0,Math.PI*2,true);
cxt.closePath();
cxt.strokeStyle=‘#666‘;
cxt.lineWidth=2;
cxt.fillStyle=‘#eee‘;
cxt.fill();
cxt.stroke();
cxt.beginPath();
cxt.arc(180,100,50,0,Math.PI*2,true);
cxt.closePath();
cxt.strokeStyle=‘#666‘;
cxt.lineWidth=2;
cxt.stroke();
}
function btn4_click(){
var cvs=$(‘myCanvas‘);
var cxt=cvs.getContext(‘2d‘);
cxt.clearRect(0,0,400,400);
cxt.beginPath();
cxt.arc(100,100,50,0,Math.PI*2,true);
cxt.closePath();
cxt.strokeStyle=‘#666‘;
cxt.lineWidth=2;
var gnt=cxt.createRadialGradient(100,100,0,100,100,250);
gnt.addColorStop(0,‘#f00‘);
gnt.addColorStop(0.3,‘#0f0‘);
gnt.addColorStop(1,‘#00f‘);
cxt.fillStyle=gnt;
cxt.fill();
}
</script>
效果图:
标签:绘图
原文地址:http://blog.csdn.net/u012193330/article/details/44984863