我正在尝试在HTML5画布上呈现网格,但是我希望用户在画布上绘制的任何内容都可以放置在网格后面。我试图通过有2个画布来重新创建该画布:一个带有网格,另一个带有用户绘制的画布。带有网格的那个放置在用户绘制的画布上,并且该画布应该是透明的,但不显示用户绘制的任何内容。如果我将用户绘制的画布放在顶部,则可以看到用户绘制的内容,但是该绘制在网格的前面,并且我希望该绘制在网格的后面。
您可以在这里查看我尝试过的内容: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>
提前致谢。
最佳答案
在由顶部网格画布覆盖的底部画布上绘图
只需在顶部的网格画布上侦听鼠标事件,然后在绘图画布上绘制即可。
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);
};
其他几个问题
样式问题:在mousemove上,绘制完整的线段,而不仅仅是继续“打开”路径。您可以通过记住lastX,lastY并在该最后一点和当前鼠标位置之间绘制一段来实现此目的。
如果鼠标移出画布,则应结束绘图操作。否则,您将得到一个“粘性”图形,使用户无法轻易停止该图形。
这是您的代码经过稍微重构以包含上述想法:
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>
关于javascript - 在HTML5 Canvas 上渲染网格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37092104/