码迷,mamicode.com
首页 > 其他好文 > 详细

Canvas 绘制网格Grid

时间:2020-01-15 16:49:39      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:doc   cto   网格   class   grid   let   tor   get   绘制   

 1 var myCanvas = document.querySelector(‘canvas‘);
 2         var ctx = myCanvas.getContext(‘2d‘);
 3 
 4         let gridSize = 10;
 5         let canvasWidth = ctx.canvas.width;
 6         let canvasHeight = ctx.canvas.height;
 7 
 8         let xLineTotal = Math.floor(canvasHeight / gridSize);
 9         let yLineTotal = Math.floor(canvasWidth / gridSize);
10 
11         for (let i = 0; i <= xLineTotal; i++) {
12             ctx.beginPath();
13             ctx.moveTo(0, i * gridSize - 0.5);
14             ctx.lineTo(canvasWidth, i * gridSize - 0.5);
15             ctx.strokeStyle = ‘#eee‘;
16             ctx.stroke();
17         }
18 
19         for (let i = 0; i <= yLineTotal; i++) {
20             ctx.beginPath();
21             ctx.moveTo(i * gridSize - 0.5, 0);
22             ctx.lineTo(i * gridSize - 0.5, canvasHeight);
23             ctx.strokeStyle = ‘#eee‘;
24             ctx.stroke();
25         }

 

Canvas 绘制网格Grid

标签:doc   cto   网格   class   grid   let   tor   get   绘制   

原文地址:https://www.cnblogs.com/CoffeeEddy/p/12197429.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!