Fabric.js中的RemoveListener

时间:2015-01-28 23:53:28

标签: fabricjs

我有一个允许用户捕捉多边形的功能。当选择ESC时,多边形将关闭并显示一个表单,允许用户设置颜色,透明度等。放置多边形后我的监听器内部禁用了监听器,但每次按ESC键时,我都会得到一个新的多边形。这是我的代码:

        fabric.util.addListener(fabric.document, 'keyup', function (e) {
            if (e.keyCode === 27) {
                for (var i = 0; i < lineCounter; i++) {
                    mapCanvas.remove(lines[i]);
                }
                //polyPoints.push(new Point(polyPoints[0].x, polyPoints[0].y));
                var poly = buildPoly(polyPoints);
                console.log(poly.get('points'));
                console.log(mapCanvas);

                mapCanvas.add(poly);
                mapCanvas.setActiveObject(poly);
                openObjectPropertiesWindow(winReturn, propertiesOpen, previousSelectedObj, mapCanvas.getActiveObject());
                previousSelectedObj = 'geometry';
                //mapCanvas.renderAll();

                mapCanvas.off('mouse:down');
                mapCanvas.off('mouse:move');
                mapCanvas.off('mouse:up');
                poly = null;
                fabric.util.removeListener(fabric.document, 'keyup', mapCanvas._keyup);
                drawing = false;
            }
        });

我尝试了几种不同的变体。如何禁用ESC键的侦听器?

2 个答案:

答案 0 :(得分:1)

fabric.util.removeListener(fabric.document, 'keyup', this.keyup);

更多信息: https://github.com/kangax/fabric.js/wiki/Working-with-events

我认为你可能错在哪里就是试图引用你的mapCanvas._keyup。您正在扩展对象,因此请使用this。另外,我没有看到使用最新版本的_keyup方法。

答案 1 :(得分:1)

这对我来说非常适合:

//This is my listener
canvas.on('object:selected', myFunction);
.
.
//remove the handler to listen to my event
canvas.off('object:selected', myFunction);