Konvajs取消拖动事件

问题描述 投票:0回答:1

我正在使用dragBoundFunc来限制拖动运动但是在某个距离我想要取消拖动,想法是当用户尝试移动到远处位置时对象会自动掉落。

我尝试使用qazxsw大便,但它不起作用。

我怎样才能做到这一点?

event.preventDefault()
javascript konvajs
1个回答
0
投票

你可以使用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; } }); 方法:

node.stopDrag()

但是最好使用if(Math.abs(prevPos.x - newPos.x) > 200){ image1.stopDrag(); } 事件来检查那个条件并停止拖动它。

© www.soinside.com 2019 - 2024. All rights reserved.