【问题标题】:How to differentiate click and mousedown event in JavaScript?如何区分 JavaScript 中的 click 和 mousedown 事件?
【发布时间】:2014-06-21 01:41:15
【问题描述】:

我有一个画布,我想在用户单击时绘制点,并在单击和拖动时绘制一条线。

为了确定当鼠标在画布上移动时是否应该生成一条线,我设置了一个变量“isDrawing”来判断用户在移动画布之前是否点击了它。我将“mousedown”事件绑定到画布,并在触发事件时将“isDrawing”设置为 true。如果是真的,我会开始画一条线,否则我不会对这种行为做任何事情。但问题是当用户点击绘制点时,'isDrawing' 也设置为 true,因为点击触发了 'mousedown' 事件。我的问题是如何区分 click 和 mousedown 事件,以便当用户单击某处时不会触发“mousedown”事件?谢谢。

【问题讨论】:

  • 也许将您的点击更改为 mouseup? (mousedown=startdrawing,mousemove=跟踪线坐标/绘制,mouseup=停止绘制)?

标签: javascript html canvas


【解决方案1】:

这是一个使用纯 javascript 的小而紧凑的示例:http://jsfiddle.net/kychan/2t97S/

function e(id) { return document.getElementById(id); }

var box = e('box'),
    ctx = box.getContext('2d'),
    w   = box.width,
    h   = box.height,
    mx  = 0,
    my  = 0
;

ctx.fillStyle = '#333';
ctx.fillRect(0,0,w,h);
ctx.fillStyle = '#FF0000';
ctx.strokeStyle= '#FF0000';

box.addEventListener('mousedown', function(e) {
    mx = e.pageX - box.offsetLeft,
    my = e.pageY - box.offsetTop;
}, false);

//    reduces dender.
function d(i,c) {
    return (c-10<i && c+10>i);
}
box.addEventListener('mouseup', function(e) {
    var nx = e.pageX - box.offsetLeft,
        ny = e.pageY - box.offsetTop;

    ctx.beginPath();
    if (d(mx,nx) && d(my,ny)) {
        ctx.arc(mx,my,1, 0, Math.PI*2, false);
    }else{
        ctx.moveTo(mx, my);
        ctx.lineTo(nx, ny);
    }
        ctx.closePath();
    ctx.stroke();
    mx=nx, my=ny;
}, false);

【讨论】:

  • 一个小的调整也会给你一些像大多数绘画程序一样的“淡入淡出”效果:jsfiddle.net/kychan/2t97S
  • 太完美了,我希望我能两次支持这个解决方案,非常感谢:D
【解决方案2】:

@Aaron 有了一个好主意...在 mouseup 中添加点而不是 mousedown。

在 mouseup 中,如果鼠标被拖动少于 5 个总像素,则将 mouseup 视为单击而不是拖动。 (5 像素是一个示例——根据您想要的容差进行调整)。

在 mousemove 中,延迟绘制线条,直到鼠标被拖动至少 5 个像素。

这是示例代码和演示:http://jsfiddle.net/m1erickson/ZTuKP/

<!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 $canvas=$("#canvas");
    var canvasOffset=$canvas.offset();
    var offsetX=canvasOffset.left;
    var offsetY=canvasOffset.top;
    var scrollX=$canvas.scrollLeft();
    var scrollY=$canvas.scrollTop();

    var isDown=false;
    var lastX,lastY;
    var dragHash;

    function handleMouseDown(e){
      e.preventDefault();
      e.stopPropagation();

      lastX=parseInt(e.clientX-offsetX);
      lastY=parseInt(e.clientY-offsetY);

      // Put your mousedown stuff here
      dragHash=0;
      isDown=true;
    }

    function handleMouseUp(e){
      e.preventDefault();
      mouseX=parseInt(e.clientX-offsetX);
      mouseY=parseInt(e.clientY-offsetY);

      if(dragHash<5){
          alert("It's a click...add a dot");
      }else{
          alert("You've been dragging");
      }

      // Put your mouseup stuff here
      isDown=false;
    }

    function handleMouseMove(e){
      if(!isDown){return;}
      e.preventDefault();
      mouseX=parseInt(e.clientX-offsetX);
      mouseY=parseInt(e.clientY-offsetY);

      // Put your mousemove stuff here
      var dx=mouseX-lastX;
      var dy=mouseY-lastY;
      lastX=mouseX;
      lastY=mouseY;

      // accumulate the drag distance 
      // (used in mouseup to see if this is a drag or click)
      dragHash+=Math.abs(dx)+Math.abs(dy);

      if(dragHash>4){
          // it's a drag operation, draw the line
      }

    }

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



}); // end $(function(){});
</script>
</head>
<body>
    <canvas id="canvas" width=300 height=300></canvas>
</body>
</html>

【讨论】:

  • 您的解决方案绝对是权威的,感谢您的帮助!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-07
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 2019-10-13
  • 1970-01-01
相关资源
最近更新 更多