FabricJS double click on objects

前端 未结 6 491
孤街浪徒
孤街浪徒 2021-01-02 08:04

I am trying to perform a special action whenever the user double clicks any object located inside the canvas. I have read the docs and not found any mouse:dblclick

相关标签:
6条回答
  • 2021-01-02 08:19

    The Correct way to add custom events to Fabric.js

    window.fabric.util.addListener(canvas.upperCanvasEl, 'dblclick', function (event, self) {
      yourFunction(event);
    });
    

    or use fabric.ext

    0 讨论(0)
  • 2021-01-02 08:23

    The more elegant way is to override fabric.Canvas._initEventListeners to add the dblclick support

    _initEventListeners: function() {
      var self = this;
      self.callSuper('_initEventListeners');
    
      addListener(self.upperCanvasEl, 'dblclick', self._onDoubleClick);
    }
    
    _onDoubleClick: function(e) {
      var self = this;
    
      var target = self.findTarget(e);
      self.fire('mouse:dblclick', {
        target: target,
        e: e
      });
    
      if (target && !self.isDrawingMode) {
        // To unify the behavior, the object's double click event does not fire on drawing mode.
        target.fire('object:dblclick', {
          e: e
        });
      }
    }
    

    I've also developed a library to implement more events missed in fabricjs : https://github.com/mazong1123/fabric.ext

    0 讨论(0)
  • 2021-01-02 08:24

    This is similar to @LeoCreer's answer but actually gets access to the targeted object

    fabric.util.addListener(canvas.upperCanvasEl, 'dblclick', function (e) {
      var target = canvas.findTarget(e);
    });
    
    0 讨论(0)
  • 2021-01-02 08:33

    Here is a quick and easy way to add a double click event handler to Fabric JS -

    Include following code snippet to your html file. Just ensure this is loaded after the main fabric.js library

    <script type="text/javascript">
            fabric = (function(f) { var nativeOn = f.on; var dblClickSubscribers = []; var nativeCanvas = f.Canvas;   f.Canvas = (function(domId, options) { var canvasDomElement = document.getElementById(domId); var c = new nativeCanvas(domId, options);   c.dblclick = function(handler) { dblClickSubscribers.push(handler) };   canvasDomElement.nextSibling.ondblclick = function(ev){ for(var i = 0; i < dblClickSubscribers.length; i++) { console.log(ev); dblClickSubscribers[i]({ e :ev }); } }; return c; });   return f; }(fabric)); 
    </script>
    

    Then add this code to listen a double click event:

    canvas.dblclick(function(e) { 
    
    }); 
    

    To get information about the actual object that is being clicked on the canvas, use following method -

    canvas.getActiveObject();
    

    eg.

    canvas.dblclick(function(e) { 
         activeObject = canvas.getActiveObject();
    }); 
    
    0 讨论(0)
  • 2021-01-02 08:41

    I am late but now fabricjs has mousedblclick event.

    Listed at: http://fabricjs.com/docs/fabric.Object.html

    See all events: http://fabricjs.com/events

    0 讨论(0)
  • 2021-01-02 08:46

    I'm using this workaround:

      var timer = 0;
      canvas.item(0).on('mouseup', function() {
        var d = new Date();
        timer = d.getTime();
      });
      canvas.item(0).on('mousedown', function() {
        var d = new Date();
        if ((d.getTime() - timer) < 300) {
          console.log('double click')
        }
      });
    
    0 讨论(0)
提交回复
热议问题