简体   繁体   English

Javascript Canvas-检查是否单击矩形

[英]Javascript Canvas - Check if clicking on rectangle

I have a canvas with a grid, and if the user clicks on a rectangle, it should be filled with a color. 我有一个带有网格的画布,如果用户单击一个矩形,则应该用一种颜色填充它。 What I'm having trouble doing is checking if the user is clicking on a rectangle already colored, so the program can either remove it or change its color. 我遇到的麻烦是检查用户是否单击了已经着色的矩形,因此程序可以删除它或更改其颜色。

Fiddle: http://jsfiddle.net/JQd4j/2/ 小提琴: http : //jsfiddle.net/JQd4j/2/

var build_canvas = document.getElementById("builder-canvas");
var build_context = build_canvas.getContext("2d");
var builder = function () {

    var x;
    for (x = 0.5; x <= 800; x += 15) {
        build_context.moveTo(x, 0);
        build_context.lineTo(x, 390);
    }

    var y;
    for (y = 0.5; y < 400; y += 10) {
        build_context.moveTo(0, y);
        build_context.lineTo(796, y);
    }

    build_context.strokeStyle = "#000000";
    build_context.stroke();
};
builder();

build_context.fillStyle = 'red';
build_canvas.onclick = function (e) {
    var rect = build_canvas.getBoundingClientRect(),
        x = e.clientX - rect.left,
        y = e.clientY - rect.top;

    x = Math.floor((x / 15)) * 15;
    y = Math.floor((y / 10)) * 10;

    build_context.fillRect(x + 1, y + 1, 14, 9);
}

Thank you in advance. 先感谢您。

Create an array which corresponds to the grid. 创建一个与网格相对应的数组。 When clicked simply set the array's value to something else than what it is initially. 单击时,只需将数组的值设置为初始值即可。

Initialize an array with number of columns x number of rows: 用列数x行数初始化数组:

var cols = 27;
var rows = 40;
var clicked = new Uint8Array(cols * rows);  // initialized with 0

Then simply calculate the index of the cell in that array and check for a value, for example 1: 然后,只需计算该数组中单元格的索引并检查一个值,例如1:

build_canvas.onclick = function (e) {

    var rect = build_canvas.getBoundingClientRect(),
        x = e.clientX - rect.left,
        y = e.clientY - rect.top,
        cellX = (x / 15)|0,           // get hor. cell position
        cellY = (y / 10)|0,           // get ver. cell position
        index = cols * cellY + cellX; // get array index

    if (clicked[index] === 1) {       // clicked ? (===1)
        alert('clicked already');     // do some action
        return;
    }

    clicked[index] = 1;               // is clicked now...

    x = cellX * 15;                   // get pixel position
    y = cellY * 10;

    build_context.fillRect(x + 1, y + 1, 14, 9);
}

Modified fiddle 改良的小提琴

The index of a 1-dimensional array representing a 2D array is always calculated width xy position + x position . 代表2D数组的一维数组的索引始终是计算得到的width xy position + x position

Another way, by checking the color of where you are trying to paint: 另一种方法是,通过检查要绘制的颜色来检查颜色:

    var p = build_context.getImageData(x+1, y+1, 1, 1).data; 
    var hex = "#" + ("000000" + rgbToHex(p[0], p[1], p[2])).slice(-6);
    if(hex=='#000000'){
        build_context.fillRect(x + 1, y + 1, 14, 9);
    }else{
        //do something
    }


function rgbToHex(r, g, b) {
    if (r > 255 || g > 255 || b > 255)
        throw "Invalid color component";
    return ((r << 16) | (g << 8) | b).toString(16);
}

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

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