【问题标题】:Click on line in canvas单击画布中的线条
【发布时间】:2015-03-03 21:44:37
【问题描述】:

是否可以获取信息,如果在画布上点击线?

var canvas;
var x = 200;
var y = 200;
var dx=4;
var dy=4;
var d = 5;
var width, height;

function init() {
    canvasE = document.getElementById('game');
    canvas= canvasE.getContext('2d');
    width = canvasE.width;
    height = canvasE.height;
    canvasE.addEventListener("click", onClick, false);
    drawCircle();
}

function drawCircle() {
 clear();
    
 canvas.arc(x-1,y-1,d,0,Math.PI*2,true);
 canvas.beginPath();
 canvas.arc(x-1,y-1,d,0,Math.PI*2,true);
 canvas.closePath();
 canvas.fill(); 
    
 canvas.beginPath();
 canvas.moveTo(20,20);
 canvas.lineTo(150,100);
 canvas.stroke();
    
 //check if click coords within current path
 if(canvas.isPointInPath(x,y)) {
   document.getElementById("inPath").innerHTML = "yes";
 } else {
   document.getElementById("inPath").innerHTML = "no";
 }
    
 canvas.closePath();
 
}

function clear() {
    canvas.fillStyle="#ffffff";
    canvas.fillRect(0,0,width,height);
    canvas.fillStyle="#ff0000";
    canvas.strokeRect(0,0,width,height);
}

function onClick(e) {
    var element = canvasE;
    var offsetX = 0, offsetY = 0

        if (element.offsetParent) {
      do {
        offsetX += element.offsetLeft;
        offsetY += element.offsetTop;
      } while ((element = element.offsetParent));
    }

    x = e.pageX - offsetX;
    y = e.pageY - offsetY;
    
    drawCircle();
    document.getElementById("info").value = "x: " + x + ", y: " + y;
}

init();
canvas, input {
    margin: 10px;
    text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<input id="info" type="text" readonly>
<span id="inPath">No</span>
<canvas id="game" width="500" height="500"></canvas>

谢谢

【问题讨论】:

    标签: javascript canvas line


    【解决方案1】:

    基于JavaScript eyedropper (tell color of pixel under mouse cursor),注意每个像素都表示为数组data中的一个4元素,即data[0] = red of (0,0),data[1] = green of (0, 0), data[2] = (0,0) 的蓝色, data[3] = (0,0) 的 alpha, data[4] = (0,1) 的红色, ...

    var canvas;
    var x = 200;
    var y = 200;
    var dx = 4;
    var dy = 4;
    var d = 5;
    var width, height;
    
    function init() {
      canvasE = document.getElementById('game');
      canvas = canvasE.getContext('2d');
      width = canvasE.width;
      height = canvasE.height;
      canvasE.addEventListener("click", onClick, false);
      drawCircle();
    }
    
    function drawCircle() {
      clear();
    
      canvas.arc(x - 1, y - 1, d, 0, Math.PI * 2, true);
      canvas.beginPath();
      canvas.arc(x - 1, y - 1, d, 0, Math.PI * 2, true);
      canvas.closePath();
      canvas.fill();
    
      canvas.beginPath();
      canvas.moveTo(20, 20);
      canvas.lineTo(150, 100);
      canvas.stroke();
    
      //check if click coords within current path
      if (canvas.isPointInPath(x, y)) {
        document.getElementById("inPath").innerHTML = "yes";
      } else {
        document.getElementById("inPath").innerHTML = "no";
      }
    
      canvas.closePath();
    
    }
    
    function clear() {
      canvas.fillStyle = "#ffffff";
      canvas.fillRect(0, 0, width, height);
      canvas.fillStyle = "#ff0000";
      canvas.strokeRect(0, 0, width, height);
    }
    
    function onClick(e) {
      var element = canvasE;
      var offsetX = 0,
        offsetY = 0
    
      if (element.offsetParent) {
        do {
          offsetX += element.offsetLeft;
          offsetY += element.offsetTop;
        } while ((element = element.offsetParent));
      }
    
      x = e.pageX - offsetX;
      y = e.pageY - offsetY;
    
      document.getElementById("info").value = "x: " + x + ", y: " + y;
    
      function pixelIndex(x, y) {
        return 4 * (y * canvasE.width + x);
      }
    
      // data is an array where each 4 elements define a pixed, these are red,green,blue,alpha
      var data = canvas.getImageData(0, 0, canvasE.width, canvasE.height).data;
      var pixelIndexStart = pixelIndex(x, y);
      // only need the first three to see if there's something drawn there
      var red = 255 - data[pixelIndexStart + 0];
      var green = 255 - data[pixelIndexStart + 1];
      var blue = 255 - data[pixelIndexStart + 2];
      var hasLine = red + green + blue > 0;
      document.getElementById("info").value = 'has line: ' + hasLine;
      
      drawCircle();
    }
    
    init();
    canvas,
    input {
      margin: 10px;
      text-align: center;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <input id="info" type="text" readonly>
    <span id="inPath">No</span>
    <canvas id="game" width="500" height="500"></canvas>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-03
      • 2019-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多