聆听KineticJS中的事件

时间:2014-04-17 16:43:25

标签: kineticjs

在我的应用程序中,当用户想要锁定特定矩形的位置时(通过点击其右上角的小圆圈),用户可以切换到锁定模式。

因此,删除事件监听器:group.off('click dblclick');

之后,他可以切换回“自由模式”,这样他就可以拖动,调整大小,做任何事情。是否有一些代码与上述行相反?我的意思是允许收听特定鼠标事件的一行。像group.on('click dblclick');这样的东西(当然这个不起作用)。

我尝试了group.listening(true);和false,但问题是圈子被添加到该组中。所以这是我要删除的特定事件,然后听取它。

1 个答案:

答案 0 :(得分:0)

您可以在舞台上收听点击事件,并启用用户点击群组外的群组收听:

免责声明 - 这是一些示例代码,但我还没有对其进行测试:

<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

$(stage.getContent()).on('click', function (event) {

    // if the group is already listening, just exit

    if(group.listening){return;}

    // get the current mouse position

    var pos=stage.getPointerPosition();
    var mouseX=parseInt(pos.x);
    var mouseY=parseInt(pos.y);

    // get the current group position

    var left=group.x();
    var right=left+group.width();
    var top=group.y();
    var bottom=top+group.height();

    // if the click is outside the group,
    // turn the groups listening on 

    if(!(mouseX>left && mouseX<right && mouseY>top && mouseY<bottom)){
        group.listening(true);
    }

});

[更新:非jQuery替代]

您可以使用stage.on(&#39; contentClick&#39;,function(){})在没有jQuery的情况下收听鼠标点击次数:

stage.on('contentClick',function(){
    var pos=stage.getPointerPosition();
    var mouseX=parseInt(pos.x);
    var mouseY=parseInt(pos.y);
    console.log("contentclick",mouseX,mouseY);
});