用线条绘制图形
发布时间:2023-05-24 14:17:13 所属栏目:教程 来源:
导读:绘制折线上一小节我们已经学过了,利用 moveto、多个 lineto、stroke 这三个方法就可以做到。本小节我们先用之前学过的内容绘制一个矩形。
先看整体案例,请在firefox浏览器中查看本案例。
<!DOCTYPE html>
<
先看整体案例,请在firefox浏览器中查看本案例。
<!DOCTYPE html>
<
绘制折线上一小节我们已经学过了,利用 moveto、多个 lineto、stroke 这三个方法就可以做到。本小节我们先用之前学过的内容绘制一个矩形。 先看整体案例,请在firefox浏览器中查看本案例。 <!DOCTYPE html> <html> <head> <Meta charset="utf-8"> <title>网Wiki</title> <style> #imooc{ border:px solid #ccc; } </style> </head> <body> <canvas id="imooc">您的浏览器不支持 HTML5 canvas 标签</canvas> <script> const canvas = document.getElementById('imooc'); const ctx = canvas.getContext('2d'); ctx.moveto(,); ctx.lineto(,); ctx.lineto(,); ctx.lineto(,); ctx.lineto(,); ctx.strokeStyle="blue" ctx.linewidth= ctx.stroke(); </script> </body> </html> 既然上面代码绘制的矩形不是很完美,那有没有比这个更好的绘制方案呢?这就要用到我们今天学习的新方法 closePath()。 closePath() 方法用于创建从笔触当前点到开始点的路径。 先看整体案例,依然是在 firefox 浏览器下查看效果。 <!DOCTYPE html> <html> <head> <Meta charset="utf-8"> <title>网Wiki</title> <style> #imooc{ border:px solid #ccc; } </style> </head> <body> <canvas id="imooc">您的浏览器不支持 HTML5 canvas 标签</canvas> <script> const canvas = document.getElementById('imooc'); const ctx = canvas.getContext('2d'); ctx.moveto(,); ctx.lineto(,); ctx.lineto(,); ctx.lineto(,); // ctx.lineto(10,10); //去掉了这一行 ctx.closePath(); //最后一笔路径使用了closePath方法 ctx.strokeStyle="blue" ctx.linewidth= ctx.stroke(); </script> </body> </html> 线条绘制三角形 我们利用 closePath 再绘制一个三角形,先看整体案例。 <!DOCTYPE html> <html> <head> <Meta charset="utf-8"> <title>网Wiki</title> <style> #imooc{ border:px solid #ccc; } </style> </head> <body> <canvas id="imooc">您的浏览器不支持 HTML5 canvas 标签</canvas> <script> const canvas = document.getElementById('imooc'); const ctx = canvas.getContext('2d'); ctx.moveto(,); ctx.lineto(,); ctx.lineto(,); ctx.closePath(); //最后一笔路径使用了closePath方法 ctx.strokeStyle="blue" ctx.linewidth= ctx.stroke(); </script> </body> </html> (编辑:汽车网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |