如何触发或接收组中对象的事件?

时间:2018-03-18 20:29:10

标签: javascript canvas fabricjs

我使用Fabric.js在线开发ppt编辑器。我有一个问题,A组有B和C,我可以听A的事件,但我也需要听A&B的事件。怎么做?

1 个答案:

答案 0 :(得分:0)

您需要为组对象设置subTargetCheck true,然后您可以从包含subTargets[]数组的事件中访问子目标

var canvas = new fabric.Canvas('c');
var rect = new fabric.Rect({
  width:100,height:100,left:10,top:10,fill:'red'
});
var circle = new fabric.Circle({
  left:150,top:10,fill:'green',radius:50
});
var group = new fabric.Group([rect,circle],{
  left:20,
  top:20,
  subTargetCheck: true
 });
canvas.add(group);
group.on('mousedown',onMouseDown);

function onMouseDown(option){
 option.subTargets[0] && console.log(option.subTargets[0].type)
}
canvas{
 border: 1px solid #000;
}
<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.js"></script>
<canvas id="c" width="400" height="400"></canvas>