Fabric.js - Grouped iText not editable

前端 未结 5 1018
暗喜
暗喜 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:25

    I might be too late to answer this, but surely many of you like me searching for this answer shall meet the solution. You can find a perfectly working solution of iText where user can edit the text of iText which is a part of a fabric.group Working solution

    Approach followed here is:

    1. When user double clicks the desired group, we ungroup all the elements
    2. set focus on iText element
    3. Enters the editing mode of iText & select all text so that user can directly start editing..
    4. Once user exits edit mode, the elements are again grouped together.

      // ungroup objects in group
      var items
      var ungroup = function (group) {
          items = group._objects;
          group._restoreObjectsState();
          canvas.remove(group);
          for (var i = 0; i < items.length; i++) {
              canvas.add(items[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 = [];
          canvas.forEachObject(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();
          }));
      });
      

提交回复
热议问题