获取鼠标coords canvas,并以间隔调用函数

时间:2013-06-02 00:51:29

标签: javascript jquery canvas

所以我希望游戏能够实时检查要点击的画布区域。所以我猜我需要一个每秒.1秒调用的函数?这是我到目前为止所提出的

function init()
{

setInterval('CheckMouseCoords', 60000);
SetUpLevel1();

}


function CheckMouseCoords()
{

var dot = getCursorPosition(e);
if (dot.x < 100 & dot.x < 200)
 {
  if (dot.y < 100 && dot.y < 200)

 {
   HUDOn();
 }  
 }


function HUDOn()
{

drawBackground();
drawBars();
drawPlayer();
drawEnemy();
drawHUD();

}

或者是否会以更加资源友好的方式点击它来检查区域(按钮)是否已被点击?

这样更好吗?

function CheckMouseCoords()
{


var mouseX = Math.floor((e.pageX-$("#canvas").offset().left) / 20);
var mouseY = Math.floor((e.pageY-$("#canvas").offset().top) / 20);

if (mouseX > 100 & mouseY < 500)
 {
 if ((mouseY+100) > 100 && (mouseY+100)< 500)

 {
   HUDOn();
 }  
 }

1 个答案:

答案 0 :(得分:0)

以下是如果鼠标事件发生时浏览器通知您的方法

首先,一些人设置。记录画布在浏览器窗口中的坐标。我们需要这个,因为浏览器将报告相对于整个窗口的鼠标位置,我们需要相对于画布的坐标。

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

然后你可以请求jQuery让你知道mousedown事件发生的时间:

    $("#canvas").mousedown(function(e){handleMouseDown(e);});

最后,当jQuery实际检测到mousedown时,jQuery将调用你的“mousedown handler function”。这是我们计算鼠标位置的位置,以及在mousedown发生时你要放置任何代码的位置。

    function handleMouseDown(e){
      mouseX=parseInt(e.clientX-offsetX);
      mouseY=parseInt(e.clientY-offsetY);
      $("#downlog").html("Down: "+ mouseX + " / " + mouseY);

      // Put your mousedown stuff here

    }

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

<!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; }
    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;

    function handleMouseDown(e){
      mouseX=parseInt(e.clientX-offsetX);
      mouseY=parseInt(e.clientY-offsetY);
      $("#downlog").html("Down: "+ mouseX + " / " + mouseY);

      // Put your mousedown stuff here

    }

    function handleMouseUp(e){
      mouseX=parseInt(e.clientX-offsetX);
      mouseY=parseInt(e.clientY-offsetY);
      $("#uplog").html("Up: "+ mouseX + " / " + mouseY);

      // Put your mouseup stuff here
    }

    function handleMouseOut(e){
      mouseX=parseInt(e.clientX-offsetX);
      mouseY=parseInt(e.clientY-offsetY);
      $("#outlog").html("Out: "+ mouseX + " / " + mouseY);

      // Put your mouseOut stuff here
    }

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

      // Put your mousemove stuff here

    }

    $("#canvas").mousedown(function(e){handleMouseDown(e);});
    $("#canvas").mousemove(function(e){handleMouseMove(e);});
    $("#canvas").mouseup(function(e){handleMouseUp(e);});
    $("#canvas").mouseout(function(e){handleMouseOut(e);});

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

</head>

<body>

    <p id="downlog">Down</p>
    <p id="movelog">Move</p>
    <p id="uplog">Up</p>
    <p id="outlog">Out</p>
    <canvas id="canvas" width=300 height=300></canvas>

</body>
</html>