【问题标题】:How to set the draggable event on mouse click?如何在鼠标点击时设置可拖动事件?
【发布时间】:2013-11-17 05:21:05
【问题描述】:

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

【问题讨论】:

    标签: javascript html html5-canvas kineticjs


    【解决方案1】:

    如果您想在按下左右按钮之前阻止拖动,您可以设置形状的 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
    • 不太确定你在问什么。如果您想在按下左右按钮之前阻止拖动,您可以设置形状的 dragBoundFunc 以限制所有拖动,直到您说可以拖动(当您看到两个按钮都按下时)。
    猜你喜欢
    • 2014-06-09
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    • 2011-08-27
    相关资源
    最近更新 更多