【问题标题】:Konvajs Cancel Drag eventKonvajs 取消拖动事件
【发布时间】:2019-02-28 12:20:05
【问题描述】:

我正在使用 dragBoundFunc 来限制拖动移动,但在一定距离内我想取消拖动,想法是当用户尝试移动到远处时对象会自动下降。

我尝试使用event.preventDefault(),但它不起作用。

我怎样才能做到这一点?

var image1 = new Konva.Image({
  image: imageObj,
  x: limit.left,
  y: limit.top,
  width: gBoxSize ,
  height: gBoxSize ,
  name: 'gameobj',
  draggable: true,

  dragBoundFunc: function(pos,event) {
      var newPos = {x: pos.x, y: pos.y};

      // if newPos exceeds 200px I want force to cancel the drag
      if(Math.abs(prevPos.x - newPos.x) > 200){
         // ?
      }

      if(newPos.x <= limit.left){
          newPos.x = limit.left;
      }else if(newPos.x >= limit.right){
          newPos.x = limit.right;
      }

      if(newPos.y <= limit.top){
          newPos.y = limit.top;
      }else if(newPos.y >= limit.bottom){
          newPos.y = limit.bottom;
      }

      // do some stuffs

      return newPos;
  }            
});

【问题讨论】:

    标签: javascript konvajs


    【解决方案1】:

    对于这种情况,您可以使用node.stopDrag() 方法:

    if(Math.abs(prevPos.x - newPos.x) > 200){
      image1.stopDrag();
    }
    

    但最好使用dragmove 事件来检查该条件并停止拖动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多