自动边距元素之谜的偏移 - 如何检索x& y来自canvas元素 - 鼠标事件

时间:2013-03-15 21:06:03

标签: html canvas

我试图弄清楚为什么我无法正确地检索位于页面中心的自动水平边距的画布上的鼠标按下事件的x和y位置。

目前:

  • 我在画布的包含父级上有相对定位,以及 对于画布本身。
  • 我正在减去偏移量&从鼠标偏移顶部 事件.pageX& .pageY

我的目标是返回用户将strokeRect拖到画布上的点(鼠标向下)。我认为这是一个CSS问题,但我很难过。

body, header, aside, footer, div, canvas{
    display: block;
    position: relative;
}

canvas {
    z-index: 99;
    background: #996;
}

#canvas-contain{
    height: 600px;
    width: 967px;
    margin: 50px auto;
    box-shadow: 0px 0px 5px 1px #222222;
}

<script>
rect.startX = e.pageX - this.offsetLeft;
rect.startY = e.pageY - this.offsetTop;


function mouseMove(e) {
  if (drag) {
    rect.w = (e.pageX - this.offsetLeft) - rect.startX;
    rect.h = (e.pageY - this.offsetTop) - rect.startY ;
    context.clearRect(0,0,canvas.width,canvas.height);
    draw();
  }

}

function draw() {
  context.strokeRect(rect.startX, rect.startY, rect.w, rect.h);
}

</script>

<body>
    <div id="ramRod">
        <div id="canvas-contain">
            <canvas id="canvas" height="600" width="967"></canvas>
        </div>
        <footer id="footer">
            <div id="left-fb" class="foot-box"></div>
            <div id="right-fb" class="foot-box"></div>
         </footer>
     </div>
</body>

1 个答案:

答案 0 :(得分:1)

以下是如何在画布上跟踪鼠标事件

在您的代码中,您将需要使用e.clientX / e.clientY而不是e.pageX / e.pageY

......还有一个小提琴:http://jsfiddle.net/m1erickson/gfzGz/

<!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){
      canMouseX=parseInt(e.clientX-offsetX);
      canMouseY=parseInt(e.clientY-offsetY);
      $("#downlog").html("Down: "+ canMouseX + " / " + canMouseY);

      // Put your mousedown stuff here

    }

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

      // Put your mouseup stuff here
    }

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

      // Put your mouseOut stuff here
    }

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

      // 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>