Fabric.js - Grouped iText not editable

前端 未结 5 1019
暗喜
暗喜 2021-01-04 13:11

The title says all. When a fabric.ITextis part of a fabric.Group, it no longer reacts like a editable text.

Here is a use-case: http://jsfi

5条回答
  •  無奈伤痛
    2021-01-04 13:29

    Thanks for the answer, However it doesn't make sure only the items in the group are regrouped, we must change 'canvas.forEachObject' to 'groupItems' as it will try to put back all items in the canvas only those belonged to the group. See the following code

    // ungroup objects in group
    var groupItems = []
    var ungroup = function (group) {
        groupItems = group._objects;
        group._restoreObjectsState();
        canvas.remove(group);
        for (var i = 0; i < groupItems.length; i++) {
            canvas.add(groupItems[i]);
        }
        // if you have disabled render on addition
        canvas.renderAll();
    };
    
    // Re-group when text editing finishes
    var dimensionText = new fabric.IText("Dimension Text", {
        fontFamily: 'Comic Sans',
        fontSize: 14,
        stroke: '#000',
        strokeWidth: 1,
        fill: "#000",
        left: 170,
        top: 60
    });
    dimensionText.on('editing:exited', function () {
        var items = [];
        groupItems.forEach(function (obj) {
            items.push(obj);
            canvas.remove(obj);
        });
        var grp = new fabric.Group(items.reverse(), {});
        canvas.add(grp);
        grp.on('mousedown', fabricDblClick(grp, function (obj) {
            ungroup(grp);
            canvas.setActiveObject(dimensionText);
            dimensionText.enterEditing();
            dimensionText.selectAll();
        }));
    });
    

提交回复
热议问题