简体   繁体   English

fabric.js:对象:在PathGroup中选择

[英]fabric.js: object:selected in PathGroup

excuse me, I encounter a problem when using PathGroup in fabric.js. 对不起,在fabric.js中使用PathGroup时遇到问题。 I add three objects to a PathGroup and add this PathGroup to canvas. 我将三个对象添加到PathGroup并将此PathGroup添加到画布。 then I observe object:selected event. 然后我观察object:selected事件。 When user select one of the three objects, I cannot tell which one is selected by e.memo.target(it refer to this PathGroup object). 当用户选择三个对象之一时,我无法分辨e.memo.target选择哪个对象(它引用此PathGroup对象)。 I use PathGroup because it is more convenient for moving the objects. 我使用PathGroup是因为它更便于移动对象。 My sample code is as below: 我的示例代码如下:

canvas.observe('object:selected', function(e) {

              var objs = e.memo.target.getObjects();

              for(var i=0; i<objs.length; i++){
                  ...                    
                }
              }
            });  

thanks for your help! 谢谢你的帮助! html5starter html5starter

I thought something like this might work (if you're using >=0.7.1), but it doesn't :/ 我以为这样的事情可能会起作用(如果您使用的是> = 0.7.1),但是它不是:/

This would work if you operated with regular objects, not those inside the PathGroup (as they follow slightly different rules — being rendered relative to group itself, and have their coordinates relative to group as well). 如果您使用常规对象而不是PathGroup内部的对象(因为它们遵循略有不同的规则-相对于组本身进行渲染,并且也具有相对于组的坐标),则这将起作用。

canvas.observe('object:selected', function(e) {
  for (var objects = e.memo.target.getObjects(), i = objects.length; i--; ) {
    objects[i].setCoords();
    if (canvas.containsPoint(e.memo.e, objects[i])) {
      console.log(objects[i])  
    }
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM