【问题标题】:Avoid dragging unintentionally in Raphael避免在 Raphael 中无意拖动
【发布时间】:2019-12-05 19:47:36
【问题描述】:

在这个jsFiddle 中,我有一个Raphael 画布,它带有一个可以单击或拖动的矩形。

如果您将鼠标移动到矩形然后单击它,您可能会稍微移动它(文本会告诉您)。我需要在我的应用程序中处理单击并在用户打算单击时忽略拖动移动。例如,只有当它大于 5 像素时,我才需要启用拖动。如果意图只是点击,有没有办法让拉斐尔不移动矩形?

var paper = Raphael("canvas", 600, 600); 
var title = this.paper.text(50, 10, 'click on the square');
var rect = this.paper.rect(20, 20, 30, 30);
rect.attr('fill', 'black')

var start = function () {
      this.odx = 0;
      this.ody = 0;
},
move = function (dx, dy) {
      title.attr('text', 'mouse moved')
      this.translate(dx - this.odx, dy - this.ody);
      this.odx = dx;
      this.ody = dy;
},
up = function () {};

rect.drag(move, start, up);

rect.mousedown(function(e){
      title.attr('text', 'mouse down')      
})

【问题讨论】:

  • 您是否尝试过使用 click 而不是 mousedown ?
  • 在 mousedown 和 mouseup 事件之后触发 click 事件,所以问题并没有消失,矩形会在 mouseup 之前被无意拖动。

标签: javascript raphael


【解决方案1】:

您可以在移动开始时存储初始坐标,当当前坐标初始坐标之间的距离小于@987654323时忽略@像素。

var paper = Raphael("canvas", 600, 600); 
var title = this.paper.text(50, 10, 'click on the square');
var rect = this.paper.rect(20, 20, 30, 30);
var PX_TO_IGNORE = 5; // pixels to ignore

rect.attr('fill', 'black')

var start = function () {
      this.odx = 0;
      this.ody = 0;
      this.initialCor = null;
},
move = function (dx, dy) {
  // check if not initialized(if it`s first click)
  if(this.initialCor == null) {
    this.initialCor = {x: dx, y: dy};
    return;
  }

  // ignore PX_TO_IGNORE pixels(basically circle with radius PX_TO_IGNORE)
  if(distance(this.initialCor.x, dx, this.initialCor.y, dy) < PX_TO_IGNORE ) {
    return;
  }

  title.attr('text', 'mouse moved')

  this.translate(dx - this.odx, dy - this.ody);
  this.odx = dx;
  this.ody = dy; 
},
up = function () {
  title.attr('text', 'mouse up')
};

rect.drag(move, start, up);

rect.mousedown(function(e){
  title.attr('text', 'mouse down')      
})

// helper function to calcuate distance between two coordinates
function distance(x1,x2,y1,y2) {
    return Math.sqrt( Math.pow((x1-x2), 2) + Math.pow((y1-y2), 2) );
}

http://jsfiddle.net/htLn819y/4/

【讨论】:

  • 你能看看this
  • @ps0604 抱歉,链接已损坏
猜你喜欢
  • 1970-01-01
  • 2020-05-04
  • 1970-01-01
  • 2015-09-22
  • 1970-01-01
  • 2015-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多