0

我尝试了除解决方案之外的所有方法,同时试图弄清楚为什么我无法正确检索位于页面中心且具有自动水平边距的画布上的鼠标按下事件的 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>
4

1 回答 1

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>
于 2013-03-15T22:40:43.980 回答