如何触发 fabric.js 中的鼠标事件来模拟鼠标操作?

How do i trigger mouse events in fabric.js to simulate mouse actions?

我能够在触发时获得 jquery 响应,但结构 canvas 未对事件起作用。

我希望此 fiddle 取消选择 IText 元素:

fiddle

我知道结构 canvas 有一个触发事件,但它也不起作用。

var canvas = new fabric.Canvas("c");
var test = jQuery("#c");

test.on("mouse:down", function (){
    alert("you clicked me");
  canvas.renderAll();
  debugger;
});

canvas.on({"mousedown": function() {
  alert("you clicked me too");
}});

$("#testClick").click(function() {
  var e = jQuery.Event("mouse:down", {
    pageX: 10,
    pageY: 10
  });
  jQuery(canvas).trigger(e);//Missed - not jquery
  jQuery(jQuery("#c")).trigger(e);
  jQuery(test).trigger(e);
});

/*************** TEXT ****************/
 var text = new fabric.IText('FaBric.js', {
  fontSize: 40,
  textAlign: 'center',
  fontWeight: 'bold',
  left: 128,
  top: 128,
  angle: 30,
  originX: 'center',
  originY: 'center',
  shadow: 'blue -5px 6px 5px',
  styles: {
    0: {
      0: {
        fontSize: 60,
        fontFamily: 'Impact',
        fontWeight: 'normal',
        fill: 'orange'
      }
    }
  }

});

text.setSelectionStyles({
  fontStyle: 'italic',
  fill: '',
  stroke: 'red',
  strokeWidth: 2
}, 1, 5);
canvas.add(text);
canvas.setActiveObject(text);

编辑 1

我尝试了 "mouse:down" 和 "click" 事件,但对象没有取消选择:fiddle 2

这是您的 fiddle 的工作叉:https://jsfiddle.net/y74h38av/

现有代码中的主要问题是您将 jQuery 事件 API 与 Fabric 事件 API 混淆了。 Fabric canvas 对象不接受 jQuery 事件对象。另外,请注意两个 API 之间的句法差异。 jQuery 使用 mousedown 而 Fabric 使用 mouse:down。您可以直接在 Fabric 对象上通过 event 方法访问 Fabric 事件 API。如果您尝试将 Fabric 对象包装在 jQuery 对象中,就像您在此处所做的那样,jQuery(test).trigger(e);,它将不起作用。

希望对您有所帮助!

仅通过 Fabric 的 trigger 触发事件是不够的。 Fabric 对象发出的事件不用于触发内部逻辑,因此除非您想触发自己的事件处理程序,否则触发它们不会有任何好处。

我不熟悉 jQuery 的事件,但您可以使用标准 JS MouseEvent constructor 做同样的事情。只要确保您是:

  • 触发适当的事件类型(即 'mousedown',而不是 'click'
  • 在适当的 DOM 元素上调度它(即 Fabric 的上部 canvas,可通过 canvas.upperCanvasEl 访问)
  • 设置事件的 clientX/clientY 而不是 pageX/pageY

var canvas = new fabric.Canvas("c");

canvas.on({"mouse:down": function(e) {
  console.log("Mouse down", e.e.clientX, e.e.clientY);
  canvas.renderAll();
}});

document.querySelector('#testClick').onclick = function() {
  var evt = new MouseEvent("mousedown", {
    clientX: 15,
    clientY: 15
  });
  canvas.upperCanvasEl.dispatchEvent(evt);
  // same as:
  //document.querySelector(".upper-canvas").dispatchEvent(evt);
}


/*************** TEXT ****************/

var text = new fabric.IText('FaBric.js', {
  fontSize: 40,
  textAlign: 'center',
  fontWeight: 'bold',
  left: 128,
  top: 128,
  angle: 30,
  originX: 'center',
  originY: 'center',
  shadow: 'blue -5px 6px 5px',
  styles: {
    0: {
      0: {
        fontSize: 60,
        fontFamily: 'Impact',
        fontWeight: 'normal',
        fill: 'orange'
      }
    }
  }

});

text.setSelectionStyles({
  fontStyle: 'italic',
  fill: '',
  stroke: 'red',
  strokeWidth: 2
}, 1, 5);
canvas.add(text);
canvas.setActiveObject(text);
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.20/fabric.js"></script>
<canvas id="c" width="300" height="200"></canvas>
<button id="testClick">Deselect</button>