JQuery tooltip in base of canvas coordinates

2019-07-09 03:37发布

i'm trying to draw a graph in a canvas, every vertex has its own coordinates (in center of the circle - 6px radius - that represent it).

I want to show a tooltip when i am over a vertex with mouse...and hide this tooltip when i am not on a vertex.

Now the tooltip is showing (only after the second pass on canvas with mouse) with right data, but when i am no more on vertex, tooltip is still here.

Here is the code of canvas.addEventListener (only here is tooltip)

canvas.addEventListener('mousemove', function(evt) {
            var mX = evt.clientX;
            var mY = evt.clientY;
            mX -= canvas.offsetLeft;
            mY -= canvas.offsetTop;
            $("canvas").tooltip();
            for (i=0; i<points.length; i++) {
                if (mX<points[i].x+6 && mX>points[i].x-6) {
                    if (mY<points[i].y+6 && mY>points[i].y-6) {
                        var str = getNodeRelations(evt);
                        x1 = points[i].x-6;
                        x2 = points[i].x+6;
                        y1 = points[i].y-6;
                        y2 = points[i].y+6;
                        /*if ($("canvas").tooltip("instance") != undefined && $("canvas").tooltip("option", "disabled") == true) {
                        $("canvas").tooltip("option", "disabled", false);
                        }*/
                        $("canvas").tooltip({
                            content: str,
                            effect: "fade",
                            track: true
                        });
                    }
                }
            }
            /*if ($("canvas").tooltip("instance") != undefined && ((mX<x1 || mX>x2) && (mY<y1 || mY>y2))) {
                $("canvas").tooltip("option", "disabled", true);
            }*/
        }, false);
    }

In comment block are not working codelines

Thank you for help in advance!

1条回答
虎瘦雄心在
2楼-- · 2019-07-09 04:13

The jQueryUI Tooltip appears when the mouse is over any part of the target element. That's why the tooltip won't fade -- because the mouse is still over your canvas element.

Therefore, jqueryUI Tooltip is not very useful to show tips on individual canvas drawings like your vertices. Yes, you can force it to show/hide in ways unintended by its API, but that risks unintended failures too.

A simple alternative might be:

  • Show/hide a div element containing your tip text.
  • Use CSS to position the tip-div.
  • Hit-test each vertex in mousemove to show/hide the tip-div.

Example starting code:

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(); }
window.onresize=function(e){ reOffset(); }

var rects=[];
rects.push({x:50,y:50,w:40,h:25,fill:'red',tip:'I am the red box'});
rects.push({x:50,y:150,w:50,h:75,fill:'blue',tip:'I am the blue box'});

for(var i=0;i<rects.length;i++){
  var r=rects[i];
  ctx.fillStyle=r.fill;
  ctx.fillRect(r.x,r.y,r.w,r.h);
  ctx.stroke();
}


$tip=$('#tip');
$tip.hide();

$("#canvas").mousemove(function(e){handleMouseMove(e);});


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

  mouseX=parseInt(e.clientX-offsetX);
  mouseY=parseInt(e.clientY-offsetY);

  $tip.hide();
  for(var i=0;i<rects.length;i++){
    var r=rects[i];
    ctx.beginPath();
    ctx.moveTo(r.x,r.y);
    ctx.lineTo(r.x+r.w,r.y);
    ctx.lineTo(r.x+r.w,r.y+r.h);
    ctx.lineTo(r.x,r.y+r.h);
    ctx.closePath();
    if(ctx.isPointInPath(mouseX,mouseY)){
      $tip.text(r.tip);
      $tip.css({left:e.clientX+3,top:e.clientY-18}).show();                 
    }
  }
}
body{ background-color: ivory; }
#canvas{border:1px solid red; margin:0 auto; }
#tip{position:absolute;background:white;border:1px solid blue;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<h4>Hover over rectangle to show tooltip.</h4>
<canvas id="canvas" width=300 height=300></canvas>
<div id=tip>Tooltip</div>

查看更多
登录 后发表回答