canvas知识总结

canvas元素绘制图像的时候有两种方法,分别是

context.fill()//填充 context.stroke()//绘制边框

style:在进行图形绘制前,要设置好绘图的样式

context.fillStyle//填充的样式 context.strokeStyle//边框样式 context.lineWidth//图形边框宽度

context.arc(centerx圆心横左边,centery圆心纵坐标,radius半径,startingAngle起始弧度值,endingAngle结束弧度值,anticlockwise='false'顺时针默认false)

2.绘制非填充线段

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title></title> <link href="https://r01.uzaicdn.com/content/v1/styles/subject.css"> <link href="https://www.jb51.net/styles/lianxi.css"> <script src="https://r01.uzaicdn.com/content/v1/scripts/core.js"></script> <script src="https://www.jb51.net/scripts/lianxi.js"></script> <!--[if lt IE 9]><script src="https://r.uzaicdn.com/content/libs/html5shiv.js"></script><![endif]--> <!--[if IE 6]><script src="https://r.uzaicdn.com/content/libs/dd_belatedpng_0.0.8a-min.js" type="text/javascript"></script><script>DD_belatedPNG.fix('.png');</script><![endif]--> <style type="text/css"> .canvas{border: 1px solid #000;display: block;margin: 0 auto;margin-top: 50px;} </style> <script> window.onload=function(){ function draw(){ var canvas = document.getElementById('canvas'); if (canvas.getContext){ var ctx = canvas.getContext('2d'); canvas.width=300; canvas.height=300; ctx.beginPath(); //一个绘画开始 ctx.moveTo(50,50);//线段起点 ctx.lineTo(100,100);//终点1 ctx.lineTo(50,100);//终点2 ctx.lineTo(50,50);//终点3 ctx.lineWidth=5;//边框宽度 ctx.strokeStyle="red"; //边框样式 ctx.closePath(); //一个绘画结束 ctx.stroke();//绘制线段 }else{ alert('当前浏览器不支持,请更换浏览器'); } } draw(); } </script> <style tyrp="text/css"> canvas{ border: 1px solid black;margin: 0 auto;display: block;} </style> </head> <body> <canvas>当前浏览器不支持,请更换浏览器</canvas> </body> </html>

3.绘制填充图形

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title></title> <link href="https://r01.uzaicdn.com/content/v1/styles/subject.css"> <link href="https://www.jb51.net/styles/lianxi.css"> <script src="https://r01.uzaicdn.com/content/v1/scripts/core.js"></script> <script src="https://www.jb51.net/scripts/lianxi.js"></script> <!--[if lt IE 9]><script src="https://r.uzaicdn.com/content/libs/html5shiv.js"></script><![endif]--> <!--[if IE 6]><script src="https://r.uzaicdn.com/content/libs/dd_belatedpng_0.0.8a-min.js" type="text/javascript"></script><script>DD_belatedPNG.fix('.png');</script><![endif]--> <style type="text/css"> .canvas{border: 1px solid #000;display: block;margin: 0 auto;margin-top: 50px;} </style> <script> window.onload=function(){ function draw(){ var canvas = document.getElementById('canvas'); if (canvas.getContext){ var ctx = canvas.getContext('2d'); canvas.width=300; canvas.height=300; ctx.beginPath(); //一个绘画开始 ctx.moveTo(50,50);//线段起点 ctx.lineTo(100,100);//终点1 ctx.lineTo(50,100);//终点2 ctx.lineTo(50,50);//终点3 ctx.fillStyle='red'; ctx.fill(); //边框添加 ctx.lineWidth=5;//边框宽度 ctx.strokeStyle="blue"; //边框样式 ctx.closePath(); //一个绘画结束 ctx.stroke();//绘制线段 }else{ alert('当前浏览器不支持,请更换浏览器'); } } draw(); } </script> <style tyrp="text/css"> canvas{ border: 1px solid black;margin: 0 auto;display: block;} </style> </head> <body> <canvas>当前浏览器不支持,请更换浏览器</canvas> </body> </html>

4.绘制圆弧

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title></title> <link href="https://r01.uzaicdn.com/content/v1/styles/subject.css"> <link href="https://www.jb51.net/styles/lianxi.css"> <script src="https://r01.uzaicdn.com/content/v1/scripts/core.js"></script> <script src="https://www.jb51.net/scripts/lianxi.js"></script> <!--[if lt IE 9]><script src="https://r.uzaicdn.com/content/libs/html5shiv.js"></script><![endif]--> <!--[if IE 6]><script src="https://r.uzaicdn.com/content/libs/dd_belatedpng_0.0.8a-min.js" type="text/javascript"></script><script>DD_belatedPNG.fix('.png');</script><![endif]--> <style type="text/css"> canvas{border: 1px solid #000;display: block;margin: 0 auto;margin-top: 50px;} </style> <script> window.onload=function(){ function draw(){ var canvas = document.getElementById('canvas'); if (canvas.getContext){ var ctx = canvas.getContext('2d'); canvas.width=800; canvas.height=800; ctx.beginPath(); //开始一个新的绘画 ctx.lineWidth=5;//边框宽度 ctx.strokeStyle="red"; //边框样式 ctx.arc(100, 100, 30, 0, 1.5*Math.PI); ctx.closePath(); //一个绘画结束,如果绘画不是封闭的,就封闭起来 ctx.stroke();//绘制线段 ctx.beginPath(); //开始一个新的绘画 ctx.lineWidth=5;//边框宽度 ctx.strokeStyle="red"; //边框样式 ctx.arc(200, 100, 30, 0, 2*Math.PI); ctx.closePath(); //一个绘画结束,如果绘画不是封闭的,就封闭起来 ctx.stroke();//绘制线段 ctx.beginPath(); //开始一个新的绘画 ctx.lineWidth=5;//边框宽度 ctx.strokeStyle="red"; //边框样式 ctx.arc(300, 100, 30, 0, 0.5*Math.PI); ctx.closePath(); //一个绘画结束,如果绘画不是封闭的,就封闭起来 ctx.stroke();//绘制线段 ctx.beginPath(); //开始一个新的绘画 ctx.lineWidth=5;//边框宽度 ctx.strokeStyle="red"; //一个绘画结束,如果绘画不是封闭的,就封闭起来 ctx.arc(400, 100, 30, 0, 0.5*Math.PI,true);//注意:0*PI,0.5*PI,1*PI,1。5*PI,2*PI所占据的位置是固定的 ctx.closePath(); //一个绘画结束 ctx.stroke();//绘制线段 ctx.beginPath(); //开始一个新的绘画 ctx.fillStyle="red"; //边框样式 ctx.arc(500, 100, 30, 0, 1.5*Math.PI); ctx.closePath(); //一个绘画结束,如果绘画不是封闭的,就封闭起来 ctx.fill();//绘制填充 ctx.beginPath(); //开始一个新的绘画 ctx.lineWidth=5;//边框宽度 ctx.strokeStyle="red"; //边框样式 ctx.arc(600, 100, 30, 0, 1.5*Math.PI); ctx.stroke();//绘制线段 }else{ alert('当前浏览器不支持,请更换浏览器'); } } draw(); } </script> </head> <body> <canvas>当前浏览器不支持,请更换浏览器</canvas> </body> </html>

5.绘制矩形

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwgpwy.html