Detecting a Click on Stage but not on Shape in KineticJS

和自甴很熟 提交于 2019-12-06 05:26:06

问题


In KineticJS, how do you detect a Click event where the click occurs outside an object/shape?

I am trying to get a Rect to change its scale to 2 when a user clicks on it, and return back to a scale of 1 when the user clicks anywhere outside it.

JSfiddle: http://jsfiddle.net/ABTAD/8/

Managed to detect a click on stage, but clicking on the Rect also fires the click handler!!! And somehow the .setScale(1) does not do anything, while console.log printes something out. How can I prevent the click handler from firing when the click is made on the Rect instead of the empty stage?

JS Code to detect click on stage

window.stage.getContainer().addEventListener('click', function(e) {
    $.each(window.layer.get('.box'), function(index, box) {
       box.setScale(1);
       console.log('clicked on stage');
    });
});

回答1:


you can access the stage content wrapper with stage.getContent(). From there, you can add an event handler like this:

stage.getContent().addEventListener('click', ...); // regular javascript

or

$(stage.getContent()).on('click', ...); // jquery



来源:https://stackoverflow.com/questions/13902747/detecting-a-click-on-stage-but-not-on-shape-in-kineticjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!