首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >画布拼图项目的getImageData和putImageData

画布拼图项目的getImageData和putImageData
EN

Stack Overflow用户
提问于 2017-08-27 06:20:51
回答 2查看 60关注 0票数 2

我有以下代码来处理我的画布:

代码语言:javascript
复制
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的网格,函数"clickBox“当前获取鼠标的坐标并清除一个单元格。当从鼠标单击事件调用时,此函数中的所有内容都按预期工作。

现在,我要做的是复制clearRect清除的画布部分,每当我单击另一个区域时,将该副本放回画布中。

我尝试了几种不同的方法,并进行了一些修补。我想我需要使用画布函数getImageData和putImageData,但一直没有成功地让它工作。

我试着在方框被清除时存储我的X和Y坐标,然后将这些坐标传递给getImageData,然后在每次新的单击发生时将putImageData放入。我不确定我做得是否正确,get/put似乎从来没有做过任何事情。

我的理论是在clearRect函数发生之前使用getImageData,然后在下一次单击时,对上一次单击的单元格使用putImageData。

有人能告诉我这个项目中getImageData和putImageData的正确用法吗?

这是我试图解决的问题:

代码语言:javascript
复制
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);
                }
            }
        }
    }
}

我也尝试过:

代码语言:javascript
复制
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);
                }
            }
        }
    }
}

Puzzle

EN

回答 2

Stack Overflow用户

发布于 2017-08-27 10:21:20

我终于修好了。

代码语言:javascript
复制
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变量,并在函数开头附近添加了一个检查,以查看数组是否已创建,而不是事先创建一个空数组。

最终添加:

代码语言:javascript
复制
if (typeof lastLocation !== 'undefined' && lastLocation.length > 0)
        {
            // the array is defined and has at least one element
            ctx.putImageData(imgData, lastLocation[0], lastLocation[1]);
        }
票数 2
EN

Stack Overflow用户

发布于 2017-08-27 07:06:08

在我看来,您需要使用这些部分作为表单元格来创建一个表。然后,您可以轻松地隐藏和取消隐藏事件或其他内容上的单元格。

您可以设置<tr id="your_cell" style="display: none;">,然后使用JavaScript重新显示它:

代码语言:javascript
复制
var cell_with_data = document.getElementById('your_cell').style;
cell_with_data.display = 'table-row'/'block'; 

单元格可以通过以下方式删除

代码语言:javascript
复制
var row = document.getElementById("myRow");
row.deleteCell(0);

顺便提一下,这只是一个建议。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45900126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档