简体   繁体   中英

fabric.js: object:selected in PathGroup

excuse me, I encounter a problem when using PathGroup in fabric.js. I add three objects to a PathGroup and add this PathGroup to canvas. then I observe object:selected event. 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). I use PathGroup because it is more convenient for moving the objects. 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

I thought something like this might work (if you're using >=0.7.1), but it doesn't :/

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).

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])  
    }
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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