我有以下代码用于处理我的画布:
function clickBox() //Get every cell by coordinate on the grid.
{
var xRectFill = 0; //We start our search on the left side of the board.
var yRectFill = 0; //We start our search at the top of the board.
var rectFillArrayX = []; //We will store every X-Coordinate of a cell here.
var rectFillArrayY = []; //We will store every Y-Coordinate of a cell here.
var mouseX = event.offsetX;
var mouseY = event.offsetY;
for (i3 = 0; i3 <= 8; i3++) //We will fill the X and Y values of our storage array here.
{
rectFillArrayX[i3] = xRectFill; //We will get each individual X-Coordinate and store from [0-8]
rectFillArrayY[i3] = yRectFill; //We will get each individual Y-Coordinate and store from [0-8]
xRectFill += 72; //After we fill an X value, we will use the value of the next cell.
yRectFill += 72; //After we fill a Y value, we will use the value of the next cell.
}
for (i4 = 0; i4 <= 8; i4++)
{
if (mouseX >= rectFillArrayX[i4] && mouseX <= (rectFillArrayX[i4] + 72))
{
for (i5 = 0; i5 <= 8; i5++)
{
if (mouseY >= rectFillArrayY[i5] && mouseY <= (rectFillArrayY[i5] + 72))
{
ctx.clearRect(rectFillArrayX[i4], rectFillArrayY[i5], 71, 71);
}
}
}
}
}
我通过设计一个数独拼图来练习画布。我有一个9 x 9网格,功能&#34; clickBox&#34;当前获取鼠标的坐标并清除单元格。从鼠标单击事件调用时,此函数中的所有内容都按预期工作。
我现在要做的是制作clearRect清除的画布部分的副本,并在我点击另一个区域时将该副本放回画布。
我尝试了几种不同的方法并且稍微修改了一下。我想我需要使用canvas函数getImageData和putImageData,但是没有成功使它工作。
我已经尝试在清除一个框时存储我的X和Y坐标,然后将这些坐标传递给getImageData,然后在发生新的点击时将putImageData放入其中。我不确定我是否正确地做了这件事,而且/似乎从未做过任何事情。
我的理论是在clearRect函数发生之前使用getImageData,然后,在下一次单击时,在先前单击的单元格上使用putImageData。
有人能告诉我这个项目正确使用了getImageData和putImageData吗?
这是我试图解决的问题:
function clickBox() //Get every cell by coordinate on the grid.
{
var xRectFill = 0; //We start our search on the left side of the board.
var yRectFill = 0; //We start our search at the top of the board.
var rectFillArrayX = []; //We will store every X-Coordinate of a cell here.
var rectFillArrayY = []; //We will store every Y-Coordinate of a cell here.
var mouseX = event.offsetX;
var mouseY = event.offsetY;
if (lastLocation[0] != null)
{
ctx.putImageData(imgData, lastLocation[0], lastLocation[1], 70);
}
for (i3 = 0; i3 <= 8; i3++) //We will fill the X and Y values of our storage array here.
{
rectFillArrayX[i3] = xRectFill; //We will get each individual X-Coordinate and store from [0-8]
rectFillArrayY[i3] = yRectFill; //We will get each individual Y-Coordinate and store from [0-8]
xRectFill += 72; //After we fill an X value, we will use the value of the next cell.
yRectFill += 72; //After we fill a Y value, we will use the value of the next cell.
}
for (i4 = 0; i4 <= 8; i4++)
{
if (mouseX >= rectFillArrayX[i4] && mouseX <= (rectFillArrayX[i4] + 72))
{
for (i5 = 0; i5 <= 8; i5++)
{
if (mouseY >= rectFillArrayY[i5] && mouseY <= (rectFillArrayY[i5] + 72))
{
var imgData = ctx.getImageData(rectFillArrayX[i4], rectFillArrayY[i4], 72, 72);
var lastLocation = [rectFillArrayX[i4], rectFillArrayY[i5]];
ctx.clearRect(rectFillArrayX[i4], rectFillArrayY[i5], 71, 71);
}
}
}
}
}
我也尝试过:
function clickBox() //Get every cell by coordinate on the grid.
{
var xRectFill = 0; //We start our search on the left side of the board.
var yRectFill = 0; //We start our search at the top of the board.
var rectFillArrayX = []; //We will store every X-Coordinate of a cell here.
var rectFillArrayY = []; //We will store every Y-Coordinate of a cell here.
var mouseX = event.offsetX;
var mouseY = event.offsetY;
var lastLocation = [];
if (typeof lastLocation[0] !== 'undefined')
{
alert("Array is defined");
ctx.putImageData(imgData, lastLocation[0], lastLocation[1], 70);
}
for (i3 = 0; i3 <= 8; i3++) //We will fill the X and Y values of our storage array here.
{
rectFillArrayX[i3] = xRectFill; //We will get each individual X-Coordinate and store from [0-8]
rectFillArrayY[i3] = yRectFill; //We will get each individual Y-Coordinate and store from [0-8]
xRectFill += 72; //After we fill an X value, we will use the value of the next cell.
yRectFill += 72; //After we fill a Y value, we will use the value of the next cell.
}
for (i4 = 0; i4 <= 8; i4++)
{
if (mouseX >= rectFillArrayX[i4] && mouseX <= (rectFillArrayX[i4] + 72))
{
for (i5 = 0; i5 <= 8; i5++)
{
if (mouseY >= rectFillArrayY[i5] && mouseY <= (rectFillArrayY[i5] + 72))
{
var imgData = ctx.getImageData(rectFillArrayX[i4], rectFillArrayY[i4], 72, 72);
var lastLocation = [rectFillArrayX[i4], rectFillArrayY[i5]];
ctx.clearRect(rectFillArrayX[i4], rectFillArrayY[i5], 71, 71);
}
}
}
}
}
答案 0 :(得分:2)
我终于修好了。
function clickBox() //Get every cell by coordinate on the grid.
{
var xRectFill = 0; //We start our search on the left side of the board.
var yRectFill = 0; //We start our search at the top of the board.
var rectFillArrayX = []; //We will store every X-Coordinate of a cell here.
var rectFillArrayY = []; //We will store every Y-Coordinate of a cell here.
var mouseX = event.offsetX;
var mouseY = event.offsetY;
if (typeof lastLocation !== 'undefined' && lastLocation.length > 0)
{
// the array is defined and has at least one element
ctx.putImageData(imgData, lastLocation[0], lastLocation[1]);
}
for (i3 = 0; i3 <= 8; i3++) //We will fill the X and Y values of our storage array here.
{
rectFillArrayX[i3] = xRectFill; //We will get each individual X-Coordinate and store from [0-8]
rectFillArrayY[i3] = yRectFill; //We will get each individual Y-Coordinate and store from [0-8]
xRectFill += 72; //After we fill an X value, we will use the value of the next cell.
yRectFill += 72; //After we fill a Y value, we will use the value of the next cell.
}
for (i4 = 0; i4 <= 8; i4++)
{
if (mouseX >= rectFillArrayX[i4] && mouseX <= (rectFillArrayX[i4] + 72))
{
for (i5 = 0; i5 <= 8; i5++)
{
if (mouseY >= rectFillArrayY[i5] && mouseY <= (rectFillArrayY[i5] + 72))
{
lastLocation = [rectFillArrayX[i4], rectFillArrayY[i5]];
ctx.clearRect(rectFillArrayX[i4], rectFillArrayY[i5], 71, 71);
}
}
}
}
}
我在其他地方创建了一个全局getImageData变量,并在函数开头附近添加了一个检查,以查看是否已创建数组,而不是事先创建一个空数组。
最终补充:
if (typeof lastLocation !== 'undefined' && lastLocation.length > 0)
{
// the array is defined and has at least one element
ctx.putImageData(imgData, lastLocation[0], lastLocation[1]);
}
答案 1 :(得分:1)
在我看来,您需要创建一个包含这些部分作为表格单元格的表格。然后,您可以轻松隐藏和取消隐藏事件或其他任何内容的单元格。
您可以设置<tr id="your_cell" style="display: none;">
,然后使用JavaScript显示回来:
var cell_with_data = document.getElementById('your_cell').style;
cell_with_data.display = 'table-row'/'block';
可以通过
删除细胞var row = document.getElementById("myRow");
row.deleteCell(0);
只是建议btw。