使用javascript显示元素的转换鼠标坐标 [英] Show converted mouse coordinates of an element with javascript

查看:115
本文介绍了使用javascript显示元素的转换鼠标坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个这样的鼠标工具提示:

I'd like to show a mouse tooltip like this:

相对于其图像的坐标系。

with a coordinate system relative to its image.

每当鼠标悬停在其中一个75x75单元格上时,该位置将以文本形式显示。我只能显示鼠标的原始坐标,但无法弄清楚数学显示它就像它在图片中一样。

Whenever the mouse is hovered over one of the 75x75 cells, the position is displayed in text. I can only show the mouse's raw coordinates, but can't figure out the math to display it like it is in the picture.

我对HTML5实现开放了好吧。

I'm open to HTML5 implementations as well.

推荐答案

以下是如何将鼠标坐标转换为单元格坐标并显示工具提示

这个数学计算你的鼠标在哪个75x75单元格内:

This math calculates which 75x75 cell your mouse is inside:

      var col=parseInt(mouseX/75);
      var row=parseInt(mouseY/75);

这里是计算该单元格右上角的尖端矩形的算术:

And here is the math to calculate a tip rectangle in the upper-right of that cell:

      var tipX=tipCol*75+75-tipWidth;
      var tipY=tipRow*75;

您可以使用画布以计算的坐标在单元格内绘制提示:

You can use canvas to draw the tip inside the cell at your calculated coordinates:

    function tip(x,y){
      var tipX=tipCol*75+75-tipWidth;
      var tipY=tipRow*75;
      ctx.beginPath();
      ctx.rect(tipX,tipY,tipWidth,tipHeight);
      ctx.fillStyle="ivory";
      ctx.fill();
      ctx.fillStyle="blue";
      ctx.fillText(tipCol+","+tipRow,tipX+2,tipY+17);
    }

这是代码和小提琴: http://jsfiddle.net/m1erickson/9V5QK/

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

<style>
    body{ background-color: ivory; padding:25px;}
    #canvas{border:1px solid red;}
</style>

<script>
$(function(){

    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");

    var canvasOffset=$("#canvas").offset();
    var offsetX=canvasOffset.left;
    var offsetY=canvasOffset.top;

    var startX;
    var startY;
    var isDown=false;

    var tipWidth=35;
    var tipHeight=22;
    var tipRow;
    var tipCol;

    ctx.font="14pt verdana";

    draw();

    function draw(){

        // you would just draw your image here
        // ctx.drawImage(0,0,image.width,image.height);

        // but for illustration, this just recreates your image
        ctx.beginPath();
        ctx.rect(0,0,375,225);
        for(var x=1;x<5;x++){ ctx.moveTo(x*75,0); ctx.lineTo(x*75,canvas.height); }
        for(var y=1;y<3;y++){ ctx.moveTo(0,y*75); ctx.lineTo(canvas.width,y*75); }
        ctx.fillStyle="black";
        ctx.fill();
        ctx.strokeStyle="gray";
        ctx.lineWidth=2;
        ctx.stroke();
    }

    function tip(x,y){
      var tipX=tipCol*75+75-tipWidth;
      var tipY=tipRow*75;
      ctx.beginPath();
      ctx.rect(tipX,tipY,tipWidth,tipHeight);
      ctx.fillStyle="ivory";
      ctx.fill();
      ctx.fillStyle="blue";
      ctx.fillText(tipCol+","+tipRow,tipX+2,tipY+17);
    }


    function handleMouseMove(e){
      mouseX=parseInt(e.clientX-offsetX);
      mouseY=parseInt(e.clientY-offsetY);
      $("#movelog").html("Move: "+ mouseX + " / " + mouseY);

      // Put your mousemove stuff here
      var col=parseInt(mouseX/75);
      var row=parseInt(mouseY/75);
      if(!(row==tipRow && col==tipCol)){
          tipCol=col;
          tipRow=row;
          draw();
          tip();
      }


    }

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

}); // end $(function(){});
</script>

</head>

<body>
    <p>Move mouse over grid to display current cell</p>
    <p id="movelog">Move</p>
    <canvas id="canvas" width=375 height=225></canvas>
</body>
</html>

这篇关于使用javascript显示元素的转换鼠标坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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