HTML5:canvas之绘制图形
2011-12-08 19:51
309 查看
1绘制矩形
fillRect(x,y,width,height):Drawsafilledrectangle strokeRect(x,y,width,height):Drawsarectangularoutline clearRect(x,y,width,height):Clearsthespecifiedareaandmakesitfullytransparent
例子:
functiondraw(){
varcanvas=document.getElementById('tutorial');
if(canvas.getContext){
varctx=canvas.getContext('2d');//得到画笔
ctx.fillRect(25,25,100,100);
ctx.clearRect(45,45,60,60);
ctx.strokeRect(50,50,50,50);
}
}
效果图
2.绘制路径drawpath
beginPath() closePath() stroke() fill()
moveTo(intx,inty)
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
beginPath():创建一个路径
closePath():关闭一个路径
stroke():空心填充路径
fill():实心填充路径。调用fill时会自动关闭路径,不需要在调用chosePath()
moveTo(intx,inty):设置路径的起始点坐标
3.绘制直线
lineTo(x,y)
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
例子:
functiondrawShape(){
//getthecanvaselementusingtheDOM
varcanvas=document.getElementById('tutorial');
//Makesurewedon'texecutewhencanvasisn'tsupported
if(canvas.getContext){
//usegetContexttousethecanvasfordrawing
varctx=canvas.getContext('2d');
//Filledtriangle
ctx.beginPath();
ctx.moveTo(25,25);
ctx.lineTo(105,25);
ctx.lineTo(25,105);
ctx.fill();
//Strokedtriangle
ctx.beginPath();
ctx.moveTo(125,125);
ctx.lineTo(125,45);
ctx.lineTo(45,125);
ctx.closePath();
ctx.stroke();
}else{
alert('YouneedSafariorFirefox1.5+toseethisdemo.');
}
}
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
效果图:
4. 绘制弧线
arc(x,y,radius,startAngle,endAngle,anticlockwise)
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
x,y : 圆心坐标
radius:半径
startAngle和
endAngle分别是起末弧度(以x轴为基准)
anticlockwise为true表示逆时针,反之顺时针
例子:
functiondrawShape(){
//getthecanvaselementusingtheDOM
varcanvas=document.getElementById('tutorial');
//Makesurewedon'texecutewhencanvasisn'tsupported
if(canvas.getContext){
//usegetContexttousethecanvasfordrawing
varctx=canvas.getContext('2d');
//Drawshapes
ctx.beginPath();
ctx.arc(75,75,50,0,Math.PI*2,true);//Outercircle
ctx.moveTo(110,75);
ctx.arc(75,75,35,0,Math.PI,false);//Mouth
ctx.moveTo(65,65);
ctx.arc(60,65,5,0,Math.PI*2,true);//Lefteye
ctx.moveTo(95,65);
ctx.arc(90,65,5,0,Math.PI*2,true);//Righteye
ctx.stroke();
}else{
alert('YouneedSafariorFirefox1.5+toseethisdemo.');
}
}
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
效果图:
5.贝塞尔和二次方曲线Bezierandquadraticcurves
quadraticCurveTo(cp1x,cp1y,x,y)//BROKENinFirefox1.5(seeworkaroundbelow)
bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y)
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
相关文章推荐
- 学习笔记:HTML5 Canvas绘制简单图形
- HTML5 Canvas(1) 绘制基本图形和线条
- HTML5教程-用Canvas标签绘制坐标变换图形
- HTML5之Canvas 2D入门2 - Canvas绘制图形
- 使用html5 canvas绘制简单图形小作品~灰太狼
- HTML5使用canvas绘制图形
- html5 canvas 笔记一(基本用法与绘制图形)
- HTML5 canvas 绘制精美的图形
- html5之canvas绘制图形的简单使用
- HTML5之Canvas绘图——使用Canvas绘制图形的基本教程
- HTML5 绘制图形canvas 初次接触
- HTML5 Canvas渐变图形绘制[线性、径向]
- HTML5快速入门实例(一)Canvas绘制图形
- HTML5中canvas知识点详解4-绘制变形图形
- HTML5 Canvas绘制多个图形时,点击图形实现页面跳转
- HTML5利用Canvas绘制图形(Canvas基本知识、绘制矩形、使用路径和图形组合)
- HTML5利用Canvas绘制图形(使用图像、裁剪区域、使用文本。阴影效果和操作像素)
- [js高手之路] html5 canvas系列教程 - arc绘制曲线图形(曲线,弧线,圆形)
- html5学习笔记二:利用canvas绘制简单图形