简体   繁体   English

在HTML5画布上渲染网格

[英]Render grid on HTML5 Canvas

I am trying to render a grid on a HTML5 canvas, but I want anything that the user draws on the canvas to be placed behind the grid. 我正在尝试在HTML5画布上呈现网格,但是我希望用户在画布上绘制的任何内容都可以放置在网格后面。 I tried to recreate this by having 2 canvas: one with the grid, and the other the one that the user draws on. 我试图通过创建2个画布来重新创建此画布:一个带有网格,另一个带有用户绘制的画布。 The one with the grid is placing on top of the canvas the user draws on, and the canvas should be transparent but it's not showing anything the user draws. 带有网格的那个放置在用户绘制的画布上方,并且该画布应该是透明的,但不显示用户绘制的任何内容。 If I place the canvas the user draws on at the top, then you can see what the user draws, but the drawing is in front of the grid, and i want the drawing behind the grid. 如果我将用户绘制的画布放在顶部,则可以看到用户绘制的内容,但是该图形位于网格的前面,并且我希望该图形位于网格的后面。

You can view what I have tried here: http://codepen.io/vishiswoz/pen/grqMyG 您可以在这里查看我尝试过的内容: http : //codepen.io/vishiswoz/pen/grqMyG

 function buildGrids(gridPixelSize, color, gap, div) { var canvas = $('#' + div + '').get(0); var ctx = canvas.getContext("2d"); //ctx.fillStyle="rgba(255, 255, 255, 0)"; //ctx.fillRect(0, 0, 500, 300); ctx.lineWidth = 0.5; ctx.strokeStyle = color; // horizontal grid lines for (var i = 0; i <= canvas.height; i = i + gridPixelSize) { ctx.beginPath(); ctx.moveTo(0, i); ctx.lineTo(canvas.width, i); if (i % parseInt(gap) == 0) { ctx.lineWidth = 0.5; } else { ctx.lineWidth = 0.5; } ctx.closePath(); ctx.stroke(); } // vertical grid lines for (var j = 0; j <= canvas.width; j = j + gridPixelSize) { ctx.beginPath(); ctx.moveTo(j, 0); ctx.lineTo(j, canvas.height); if (j % parseInt(gap) == 0) { ctx.lineWidth = 0.5; } else { ctx.lineWidth = 0.5; } ctx.closePath(); ctx.stroke(); } for(var ii = 0; ii <=canvas.width; ii+=2) { for(var jj=0; jj <=canvas.height; jj+=2) { ctx.clearRect(ii,jj,1,1); } } } buildGrids(5, "grey", 2, "test"); var el = document.getElementById('c'); var ctx = el.getContext('2d'); ctx.strokeStyle = "black"; var isDrawing; el.onmousedown = function(e) { isDrawing = true; ctx.lineWidth = 10; ctx.lineJoin = ctx.lineCap = 'round'; ctx.moveTo(e.clientX, e.clientY); }; el.onmousemove = function(e) { if (isDrawing) { ctx.lineTo(e.clientX, e.clientY); ctx.stroke(); } }; el.onmouseup = function() { isDrawing = false; }; 
 canvas { position: absolute; top: 0; left: 0; } #c { } 
 <canvas id="c" width="500" height="300"></canvas> <canvas id="test" width="500" height="300"></canvas> 

Thanks in advance. 提前致谢。

Drawing on a bottom canvas covered by a top grid canvas 在由顶部网格画布覆盖的底部画布上绘图

在此处输入图片说明

Just listen for mouse events on the top grid-canvas but draw on the drawing canvas. 只需在顶部的网格画布上侦听鼠标事件,然后在绘图画布上绘制即可。

var gridCanvas=document.getElementById('test');

gridCanvas.onmousedown = function(e) {
  isDrawing = true;
  ctx.lineWidth = 10;
  ctx.lineJoin = ctx.lineCap = 'round';
  ctx.moveTo(e.clientX, e.clientY);
};

A couple of other issues 其他几个问题

  • A matter of style: On mousemove, draw a complete line segment rather than just continuing an "open" path. 样式问题:在mousemove上,绘制完整的线段,而不仅仅是继续“打开”路径。 You can do this by remembering the lastX,lastY and drawing a segment between that last point and the current mouse position. 您可以通过记住lastX,lastY并在该最后一点与当前鼠标位置之间绘制一段来实现此目的。
  • If the mouse goes out of the canvas you should end the drawing operation. 如果鼠标移出画布,则应结束绘图操作。 Otherwise you will end up with a "sticky" drawing where the user can't easily stop the drawing. 否则,您将得到一个“粘性”图形,使用户无法轻易停止该图形。

Here's your code slightly refactored to include the above ideas: 这是您的代码经过稍微重构以包含上述想法:

 var gridCanvas = document.getElementById('grid'); var el = document.getElementById('drawing'); function reOffset(){ var BB=el.getBoundingClientRect(); offsetX=BB.left; offsetY=BB.top; } var offsetX,offsetY; reOffset(); window.onscroll=function(e){ reOffset(); } window.onresize=function(e){ reOffset(); } buildGrids(5, "grey", 2, "test"); var ctx = el.getContext('2d'); ctx.strokeStyle = "orange"; var isDrawing,lastX,lastY; gridCanvas.onmousedown = function(e) { lastX=e.clientX; lastY=e.clientY; ctx.lineWidth = 10; ctx.lineJoin = ctx.lineCap = 'round'; isDrawing = true; }; gridCanvas.onmousemove = function(e) { if (isDrawing) { ctx.beginPath(); ctx.moveTo(lastX,lastY); ctx.lineTo(e.clientX, e.clientY); ctx.stroke(); lastX=e.clientX; lastY=e.clientY; } }; gridCanvas.onmouseup = function() { isDrawing = false; };gridCanvas.onmouseout = function() { isDrawing = false; }; function buildGrids(gridPixelSize, color, gap, div) { var ctx = gridCanvas.getContext("2d"); //ctx.fillStyle="rgba(255, 255, 255, 0)"; //ctx.fillRect(0, 0, 500, 300); ctx.lineWidth = 0.5; ctx.strokeStyle = color; // horizontal grid lines for (var i = 0; i <= gridCanvas.height; i = i + gridPixelSize) { ctx.beginPath(); ctx.moveTo(0, i); ctx.lineTo(gridCanvas.width, i); if (i % parseInt(gap) == 0) { ctx.lineWidth = 0.5; } else { ctx.lineWidth = 0.5; } ctx.closePath(); ctx.stroke(); } // vertical grid lines for (var j = 0; j <= gridCanvas.width; j = j + gridPixelSize) { ctx.beginPath(); ctx.moveTo(j, 0); ctx.lineTo(j, gridCanvas.height); if (j % parseInt(gap) == 0) { ctx.lineWidth = 0.5; } else { ctx.lineWidth = 0.5; } ctx.closePath(); ctx.stroke(); } for(var ii = 0; ii <=gridCanvas.width; ii+=2) { for(var jj=0; jj <=gridCanvas.height; jj+=2) { ctx.clearRect(ii,jj,1,1); } } } 
 body{ background-color: ivory; } canvas{border:1px solid red;} canvas{ position: absolute; top: 0; left: 0;} 
 <canvas id="drawing" width="500" height="300"></canvas> <canvas id="grid" width="500" height="300"></canvas> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM