Grouping and Ungrouping Fabric.js objects

后端 未结 3 1119
庸人自扰
庸人自扰 2021-02-06 07:37

I\'ve created a kind of \'polygon selector\' or \'polygon maker\' using fabric.js. Each click creates a corner of the polygon, which can be selected, moved, etc... double clicki

3条回答
  •  被撕碎了的回忆
    2021-02-06 07:55

    if getActiveGroup() is not available then you can use this to group (after mouse selecting multiple objects):

    toGroup() is only available if multiple objects are selected

    var activeObj = canvas.getActiveObject();
    var activegroup = activeObj.toGroup();
    var objectsInGroup = activegroup.getObjects();
    activegroup.clone(function(newgroup) {
        canvas.remove(activegroup);
        objectsInGroup.forEach(function(object) {
            canvas.remove(object);  
        });
        canvas.add(newgroup);
    });

    changes http://fabricjs.com/v2-breaking-changes-2

提交回复
热议问题