JQuery工具提示基于画布坐标 [英] JQuery tooltip in base of canvas coordinates

查看:305
本文介绍了JQuery工具提示基于画布坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在画布上绘制一个图形,每个顶点都有自己的坐标(在圆心 - 6px半径 - 表示它)。

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.

.addEventListener(这里只是工具提示)

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

感谢您提前的帮助!

推荐答案

当鼠标悬停在任何部分时,jQueryUI Tooltip目标元素。这是为什么工具提示不会褪色 - 因为鼠标仍然在您的canvas元素。

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.

因此,jqueryUI Tooltip不是非常有用的显示提示在单个画布图纸像你的顶点。是的,您可以强制它以非预期的方式显示/隐藏其API,但也可能导致意外的失败。

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:


  • 显示/隐藏包含提示文本的div元素。

  • 使用CSS定位tip-div。

  • 在mousemove中点击测试每个顶点以显示/隐藏tip-div。

示例开始代码

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>

这篇关于JQuery工具提示基于画布坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆