如何找到我在HTML5画布中单击的网格方块元素

时间:2016-09-02 08:37:36

标签: javascript jquery html5-canvas

我使用StrokeLineX和StrokeLineY在html5画布中绘制了一个网格。我想在网格中点击它时突出显示特定的方形/矩形。 我已经尝试使用math.floor来定义空间的索引,但是一旦宽度或高度增加,它就会开始给出不同的答案。在最后发布之前我已经尝试了太多次。 这是代码。

var canvas = document.getElementById("myCanvas");
      canvas.addEventListener('click', on_canvas_click, false);
      var ctx = canvas.getContext("2d");

      var tray_length = 800;
      var tray_depth = 800;
      var boxHeight = 50;
      var boxWidth = 50;

var canvas_X = tray_length;
      var canvas_Y = tray_depth;

      var box_x_pixels = canvas_X/no_of_columns;
      var box_y_pixels = canvas_Y/no_of_rows;

// Drawing the grid
      for (var y = boxWidth; y < canvas_Y; y += boxWidth) {
          strokeLineX(ctx, y);
      }

      for (var x = boxHeight; x < canvas_X; x += boxHeight) {
              strokeLineY(ctx, x);

          }

function strokeLineX(ctx, y) {
          ctx.beginPath();
          ctx.strokeStyle = 'green';
          ctx.moveTo(0, y);
          ctx.lineTo(canvas_X, y);
          ctx.stroke();
          ctx.closePath();
      }



      function strokeLineY(ctx, x) {
          ctx.beginPath();
          ctx.strokeStyle = 'red';
          ctx.moveTo(x, 0);
          ctx.lineTo(x, canvas_Y);
          ctx.stroke();
          ctx.closePath();
      }

function on_canvas_click(ev) {
          var x = ev.pageX - canvas.offsetLeft;
          var y = ev.pageY - canvas.offsetTop;

          console.log(x+":"+y);
          var coordinateDisplay = "x=" + x + ", y=" + y;

            if (y>= 0 && y <= y+boxHeight ) {    

                var indexOfX = Math.floor(x/boxWidth); //divide on width and round off
                var indexOfY = Math.floor(y/boxHeight);
                // alert('You clicked bar index: ' + indexOfX+"-"+indexOfY);


               ctx.fillRect="green";
               ctx.rect(x,y,box_x_pixels,box_y_pixels);
               ctx.stroke();

                console.log(indexOfX + "-" + indexOfY);


            }


      }

1 个答案:

答案 0 :(得分:2)

tcp::socket acceptor; 中更改以下内容:

而不是

on_canvas_click

做的:

    ctx.fillRect="green";

而不是:

    ctx.fillStyle="green";

做的:

    ctx.rect(x,y,box_x_pixels,box_y_pixels);

......你不需要:

    ctx.fillRect(boxWidth*indexOfX, boxHeight*indexOfY, boxWidth, boxHeight);

以下是应用这些更改的工作代码段,但也有一些与您的问题无关的简化:

  • 删除未使用的变量;
  • 使用一个函数绘制线条,而不是两个;
  • 使用 ctx.stroke(); canvas.width属性;
  • 在外部网格边界上绘制网格线

canvas.height
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var boxHeight = 50;
var boxWidth = 50;
var canvas_X = canvas.width;
var canvas_Y = canvas.height;

canvas.addEventListener('click', on_canvas_click, false);

// Drawing the grid
for (var y = 0; y <= canvas_Y; y += boxWidth) {
    strokeLine(ctx, 0, y, canvas_X, y, 'green');
}

for (var x = 0; x <= canvas_X; x += boxHeight) {
    strokeLine(ctx, x, 0, x, canvas_Y, 'red');
}

function strokeLine(ctx, x0, y0, x1, y1, color) {
    ctx.beginPath();
    ctx.strokeStyle = color;
    ctx.moveTo(x0, y0);
    ctx.lineTo(x1, y1);
    ctx.stroke();
    ctx.closePath();
}

function on_canvas_click(ev) {
    var x = ev.pageX - canvas.offsetLeft;
    var y = ev.pageY - canvas.offsetTop;
    if (y>= 0 && y <= y+boxHeight ) {    
        var indexOfX = Math.floor(x/boxWidth); //divide on width and round off
        var indexOfY = Math.floor(y/boxHeight);
        ctx.fillStyle="green";
        ctx.fillRect(boxWidth*indexOfX, boxHeight*indexOfY, boxWidth, boxHeight);
    }
}