简体   繁体   English

单击矩形时,如何在html画布的矩形框中获得下拉警报?

[英]How to get a dropdown alert at the rectangle box in html canvas on clicking on the rectangle?

I have a rectangle in the html canvas, for which I need a drop down alert by clicking on it, having the details of the name in the box. 我在html画布中有一个矩形,为此我需要单击下拉警报,并在框中输入名称的详细信息。 How can I achieve this using Javascript? 如何使用Javascript实现呢?

My html is: 我的html是:

<script>
$(function loadSQLRecords(){
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

ctx.beginPath();
ctx.lineWidth = "1";
ctx.strokeStyle = "black";
ctx.rect(5, 5, 100, 100);  
ctx.stroke();
ctx.beginPath();
ctx.font = "10px Arial";
ctx.fillStyle = "black";
ctx.fillText("V10165",65,20);
ctx.beginPath();
ctx.lineWidth = "1";
ctx.strokeStyle = "black";
ctx.rect(15, 40, 75, 20);
ctx.fillStyle = "lightgreen";
ctx.fillRect(15,40,75,20);
ctx.font = "10px Arial";
ctx.fillStyle = "black";
ctx.fillText("ESPS",30,55);
ctx.stroke();
ctx.beginPath();
ctx.font = "10px Arial";
ctx.fillStyle = "black";
ctx.fillText("V10166",65,95);
}
</script>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>

<div data-role="header">  

</div>  
<div data-role="content" style="padding: 15px" onload="loadSQLRecords();">

   <canvas id="myCanvas" width="1100" height="550" style="border:1px solid #d3d3d3;">
   </canvas>

</div>

How can I have an event on the inner rectangle to display an alert with name as "ESPS"? 如何在内部矩形上有一个事件来显示名称为“ ESPS”的警报? Can anyone suggest me a code to achieve this? 谁能建议我一个代码来实现这一目标?

The canvas element itself can trigger mouse events, but rectangles drawn on the canvas cannot. canvas元素本身可以触发鼠标事件,但是不能在画布上绘制矩形。 Any drawing on a canvas is just a set of un-remembered pixels. 画布上的任何绘图都只是一组未记忆的像素。

But you can still accomplish click-testing a drawn rectangle. 但是您仍然可以完成对绘制的矩形的点击测试。

First save the rectangle's definition in a javascript object: 首先将矩形的定义保存在javascript对象中:

var box1={
    x:15,y:40,
    w:75,h:20,
    right:15+40,
    bottom:75+20
}

Then listen for mousedown events on the canvas and test if the mouse is inside the rectangle's definition: 然后在画布上侦听mousedown事件,并测试鼠标是否在矩形的定义内:

// listen for mouse events
$("#canvas").mousedown(function(e){handleMouseDown(e);});

function handleMouseDown(e){
  // tell the browser we're handling this event
  e.preventDefault();
  e.stopPropagation();

  // get the mouse position
  mx=parseInt(e.clientX-offsetX);
  my=parseInt(e.clientY-offsetY);

  // test if the mouse is inside box1
  if(mx>=box1.x && mx<=box1.right && my>=box1.y && my<=box1.bottom){
      alert("ESPS");
  }

}

Here's example code and a Demo: 这是示例代码和演示:

 var canvas=document.getElementById("canvas"); var ctx=canvas.getContext("2d"); var cw=canvas.width; var ch=canvas.height; function reOffset(){ var BB=canvas.getBoundingClientRect(); offsetX=BB.left; offsetY=BB.top; } var offsetX,offsetY; reOffset(); window.onscroll=function(e){ reOffset(); } var box1={ x:15,y:40, w:75,h:20, right:15+40, bottom:75+20 } ctx.lineWidth = "1"; ctx.strokeStyle = "black"; ctx.strokeRect(5, 5, 100, 100); ctx.fillStyle = "lightgreen"; ctx.fillRect(box1.x,box1.y,box1.w,box1.h); ctx.strokeRect(box1.x,box1.y,box1.w,box1.h); ctx.font = "10px Arial"; ctx.fillStyle = "black"; ctx.fillText("V10165",65,20); ctx.fillText("ESPS",30,55); ctx.fillText("V10166",65,95); // listen for mouse events $("#canvas").mousedown(function(e){handleMouseDown(e);}); function handleMouseDown(e){ // tell the browser we're handling this event e.preventDefault(); e.stopPropagation(); // get the mouse position mx=parseInt(e.clientX-offsetX); my=parseInt(e.clientY-offsetY); // test if the mouse is inside box1 if(mx>=box1.x && mx<=box1.right && my>=box1.y && my<=box1.bottom){ alert("ESPS"); } } 
 body{ background-color: ivory; } #canvas{border:1px solid red;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <canvas id="canvas" width=300 height=300></canvas> 

Canvas is just an image canvas, it does not detect any mouse events / coordinates, it's just pixels. 画布只是图像画布,它不会检测到任何鼠标事件/坐标,而只是像素。 So unlike HTML+JavaScript, to implement what you're asking requires a lot of code in Canvas because you have to manually program every step. 因此,与HTML + JavaScript不同,要实现您要的内容,需要在Canvas中使用大量代码,因为您必须手动编程每个步骤。

A library like http://www.zebkit.com/ could maybe help. http://www.zebkit.com/这样的库可能会有所帮助。

Or you could switch to HTML or SVG, which don't have this problem, where you can just add an onclick handler to an element and then display some other element, without having to draw pixels manually. 或者,您也可以切换到没有问题的HTML或SVG,您可以在其中添加onclick处理程序,然后显示其他元素,而无需手动绘制像素。

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

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