如何在鼠标单击时设置可拖动事件?

3

当左右鼠标同时点击并拖动时,我能否设置舞台的可拖动事件。

1个回答

2
如果您想要在左右按钮都按下后才能防止拖动,请将形状的dragBoundFunc设置为限制所有拖动,直到您确认可以拖动(当您看到两个按钮都按下时)。
这是一个关于dragBoundFunc的链接:

http://www.html5canvastutorials.com/kineticjs/html5-canvas-drag-and-drop-shapes-horizontally-or-vertically-tutorial/

这是一些开始的代码:
// add properties to tell whether left/right buttons are currently down

myShape.leftIsDown=false;
myShape.rightIsDown=false;

// add mousedown to set the appropriate button flag to true

myShape.on("mousedown",function(event){
    if(event.button==0){this.leftIsDown=true;}
    if(event.button==2){this.rightIsDown=true;}
});

// add mouseup to set the appropriate button flag to false

myShape.on("mouseup",function(event){
    if(event.button==0){this.leftIsDown=false;}
    if(event.button==2){this.rightIsDown=false;}
});

// add a dragBoundFunc to the shape
// If both buttons are pressed, allow dragging
// If both buttons are not pressed, prevent dragging

dragBoundFunc: function(pos) {
    if(this.leftIsDown && this.rightIsDown){
        // both buttons are down, ok to drag
        return { pos }
    }else{
        // both buttons aren't down, refuse to drag
        return {
          x: this.getAbsolutePosition().x,
          y: this.getAbsolutePosition().y
        }
    }
}

我的意思是当我同时点击并拖动时。@markE - VijayB
不太确定你在问什么。如果你想要防止拖动直到左右两个按钮都按下,你可以设置一个形状的dragBoundFunc来限制所有的拖动,直到你说可以拖动为止(当你看到两个按钮都按下时)。 - markE

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接