本文主要包含html5,canvas等相关知识,匿名希望在学习及工作中可以帮助到您
1)用路径绘图:
beginPath()——开始一条新路径;
closePath()——尝试闭合现有路径,方法是绘制一条线,连接最后那条线的终点与初始坐标;
fill()——填充用子路径描述的图形;
isPointInPath(x,y)——如果指定的点在当前路径所描述的图形之内则返回true;
lineTo(x,y)——绘制一条到指定坐标的子路径;
moveTo(x,y)——移动到指定坐标而不绘制子路径;
rect(x,y,w,h)——绘制一个矩形,其左上角位于(x,y),宽度是w,高度是h;
stroke()——给子路径描述的图形绘制轮廓线;
<style type="text/css"> canvas{ border:thin solid black; margin: 4px; } body > *{ float: left; } </style>
<canvas id="canvas1" width="500" height="140"> 您的浏览器不支持<code>canvas</code>! </canvas>
<script> //由直线创建路径 var ctx=document.getElementById("canvas1").getContext("2d"); ctx.fillStyle="#136455"; ctx.strokeStyle="blue"; ctx.lineWidth=4; ctx.beginPath(); ctx.moveTo(10,10); ctx.lineTo(110,10); ctx.lineTo(110,120); ctx.closePath(); ctx.fill(); ctx.beginPath(); ctx.moveTo(150,10); ctx.lineTo(200,10); ctx.lineTo(200,120); ctx.lineTo(190,120); ctx.fill(); ctx.stroke(); ctx.beginPath(); ctx.moveTo(250,10); ctx.lineTo(250,120); ctx.stroke(); </script>
lineCap——在绘制线条或闭合图形时设置线条末端的样式;
<canvas id="canvas2" width="500" height="140"> 您的浏览器不支持<code>canvas</code>! </canvas>
<script> //设置lineCap属性 var ctx2=document.getElementById("canvas2").getContext("2d"); ctx2.strokeStyle="red"; ctx2.lineWidth=2; ctx2.beginPath(); ctx2.moveTo(0,50); ctx2.lineTo(200,50); ctx2.stroke(); ctx2.strokeStyle="black"; ctx2.lineWidth=40; var xpos=50; var styles=["butt","round","square"]; for(var i=0;i<styles.length;i++){ ctx2.beginPath(); ctx2.lineCap=styles[i]; ctx2.moveTo(xpos,50); ctx2.lineTo(xpos,150); ctx2.stroke(); xpos+=50; } </script>
<canvas id="canvas3" width="500" height="140"> 您的浏览器不支持<code>canvas</code>! </canvas>
<script> //用Rect方法绘制矩形 var ctx3=document.getElementById("canvas3").getContext("2d"); ctx3.fillStyle="yellow"; ctx3.strokeStyle="black"; ctx3.lineWidth=4; ctx3.beginPath(); ctx3.moveTo(110,10); ctx3.lineTo(110,100); ctx3.lineTo(10,10); ctx3.closePath(); ctx3.rect(110,10,100,90); ctx3.rect(110,100,130,30); ctx3.fill(); ctx3.stroke(); </script>
2)绘制圆弧:
arc(x,y,rad,startAngle,endAngle,direction)——绘制一段圆弧到(x,y),半径为rad,起始角度为 startAngle,结束角度为endAngle。可选参数direction指定了圆弧的方向;
arcTo(x1,y1,x2,y2,rad)——绘制一段半径为rad,经过(x1,y1),直到(x2,y2)的圆弧;
<canvas id="canvas4" width="500" height="140"> 您的浏览器不支持<code>canvas</code>! </canvas>
<script> //使用arcTo方法 var ctx4=document.getElementById("canvas4").getContext("2d"); var point1=[100,10]; var point2=[200,10]; var point3=[200,110]; ctx4.fillStyle="yellow"; ctx4.strokeStyle="black"; ctx4.lineWidth=4; ctx4.beginPath(); ctx4.moveTo(point1[0],point1[1]); ctx4.arcTo(point2[0],point2[1],point3[0],point3[1],100); ctx4.stroke(); drawPoint(point1[0],point1[1]); drawPoint(point2[0],point2[1]); drawPoint(point3[0],point3[1]); ctx4.beginPath(); ctx4.moveTo(point1[0],point1[1]); ctx4.lineTo(point2[0],point2[1]); ctx4.lineTo(point3[0],point3[1]); ctx4.stroke(); function drawPoint(x,y){ ctx4.lineWidth=1; ctx4.strokeStyle="red"; ctx4.strokeRect(x-2,y-2,4,4); } </script>
<canvas id="canvas5" width="500" height="140"> 您的浏览器不支持<code>canvas</code>! </canvas>
<script> //响应鼠标移动绘制圆弧 var canvasElem = document.getElementById("canvas5"); var ctx5 = canvasElem.getContext("2d"); var point1 = [100, 10]; var point2 = [200, 10]; var point3 = [200, 110]; draw(); canvasElem.onmousemove = function (e) { if (e.ctrlKey) { point1 = [e.clientX, e.clientY]; } else if(e.shiftKey) { point2 = [e.clientX, e.clientY]; } else { point3 = [e.clientX, e.clientY]; } ctx5.clearRect(0, 0, 540, 140); draw(); } function draw() { ctx5.fillStyle = "yellow"; ctx5.strokeStyle = "black"; ctx5.lineWidth = 4; ctx5.beginPath(); ctx5.moveTo(point1[0], point1[1]); ctx5.arcTo(point2[0], point2[1], point3[0], point3[1], 50); ctx5.stroke(); drawPoint(point1[0]