how to remove event listener from fabricjs canvas

久未见 提交于 2019-12-01 17:43:10

问题


I am using fabricjs to build an interactive map in html5.

When the DOM loads, I add my mouseover listener to my fabricjs canvas instance by calling: *my_event_setter( true )*.

Then for the sake of testing, I want to remove the listener once I do my first mouseover by calling: *my_event_setter( false )*. That should thus remove the mouseover listener, but it does not.

my_event_setter = function( toggle )
  { var lvo =  { 'object:over' : function(e){ mouseover_handler( e ) } } ;
    toggle ? my_fabric_canvas.on( lvo ) : my_fabric_canvas.off( lvo ) ;
  } 
mouseover_handler = function( e )
  { my_event_setter( false ) ;
  } 

回答1:


I solved this by using:

var canvas = fabric.Canvas.activeInstance;
canvas.__eventListeners["mouse:down"] = [];

The events are in an Array so it makes it easy to handle these.




回答2:


This example shows how to remove the mousemove when mouseup is fired:

canvas.on('mouse:up', function () {
    canvas.off('mouse:move', eventHandler);
});



回答3:


Bind event with angular js like

$scope.closeCurve = function(){
    canvas.isDrawingMode = true;
    canvas.on('mouse:up', function() {
      canvas.getObjects().forEach(o => {
        o.fill = 'blue'
      });
      canvas.renderAll();
    });
  };

Remove event like

$scope.selectTool = function(){
    canvas.isDrawingMode = false;
    canvas.off('mouse:up');
  };

bind with on method and remove from off method...:)



来源:https://stackoverflow.com/questions/18737058/how-to-remove-event-listener-from-fabricjs-canvas

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