简体   繁体   English

应该'stopPropagation'阻止canvas在Fabricjs对象上接收鼠标事件吗?

[英]Should 'stopPropagation' prevent canvas from receiving mouse events on Fabricjs objects?

This is a pretty simple question but I can't seem to find an answer. 这是一个非常简单的问题,但我似乎无法找到答案。

I have a fabricjs canvas instance with a mouse eventListener attached. 我有一个fabricjs了鼠标eventListener的fabricjs画布实例。 I also have a bunch of objects with mouse eventListeners. 我还有一堆带有鼠标eventListeners的对象。 I do not want mouse events on the objects to get passed through to the canvas. 我不希望对象上的鼠标事件传递到画布。 stopPropagation and/or stopImmediatePropagation don't stop the events. stopPropagation和/或stopImmediatePropagation不会停止事件。

Am I using these methods wrong? 我使用这些方法错了吗? Or is there a Fabricjs way to handle this? 或者是否有Fabricjs方法来处理这个问题?


Method on a class wrapping a Fabricjs canvas instance 包装Fabricjs画布实例的类的方法

this.canvas.on("mouse:down", this.handleMouseDown);

Method on a Fabricjs subclass: Fabricjs子类的方法:

testMouseDownHandler: function (event) {            
        event.e.stopPropagation();
        event.e.stopImmediatePropagation();
        event.e.preventDefault();
        if (event.e.shiftKey) {
            this.canvas.setActiveObject(this);
        }
    },

As @traktor53 said in his comment there is no such thing as bubbling on a canvas object. 正如@ traktor53在他的评论中所说的那样,在画布对象上没有冒泡。 In Konva.js they actually added their own event handling on object level where bubbling is simulated exactly like how you would expect in a normal DOM (more on that great feature of Konva in the docs for Konva.js here: https://konvajs.org/docs/events/Cancel_Propagation.html ). 在Konva.js中,他们实际上在对象级别添加了自己的事件处理,其中冒泡的模拟方式与您在正常DOM中的预期完全相同(更多关于Konva.js文档中Konva的强大功能: https:// konvajs .org / docs / events / Cancel_Propagation.html )。

As far as I know it doesn't work like that for Fabric.js, but that doesn't mean you cannot prevent the execution of the event handler of the canvas when clicking on an object. 据我所知,它不像Fabric.js那样工作,但这并不意味着你不能阻止在点击一个对象时执行画布的事件处理程序。

The main issue is that the canvas event fires before the object event meaning when you try to prevent "bubbling" inside the event handler of the object you are too late (in that sense it is indeed not "bubbling" towards the canvas). 主要问题是canvas事件在对象事件之前触发,这意味着当你试图阻止在对象的事件处理程序中“冒泡”时你已经太晚了(在这种意义上它确实没有“冒泡”到画布)。

I prevent the canvas event from executing simply by ignoring all canvas events that do have a target, it means it will only run when we clicked on nothing else but the canvas: 我只是通过忽略所有具有目标的画布事件来阻止canvas事件的执行,这意味着它只会在我们点击除画布之外的任何其他内容时运行:

canvas.on('mouse:down', (event) => {
    if(event.target !== null){
        //Ignore this event, we are clicking on an object (event.target is clicked object)
        return;
    }
    //... your canvas event handling code...
}

object.on('mousedown', (event) => {
    //... your object event handling code...
    let object = event.target;
}

Find all the other event names here in the docs: http://fabricjs.com/events 在文档中找到所有其他事件名称: http//fabricjs.com/events

Hope this is helpful. 希望这是有帮助的。 If there are other (better solutions) please comment. 如果还有其他(更好的解决方案)请评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM