javascriptfabricjsfabricjs2

Select text of IText on FabricJS


I want set the text selection by code in FabricJS like the image. When I create the IText object I need to select all text to facilitate the editing.

I check the documentation, but no success.

enter image description here


Solution

  • I've found it in documentation. And prepared use example for you

    import "./styles.css";
    import { fabric } from "fabric";
    
    const canvas = new fabric.Canvas("c", { width: 600, height: 600 });
    
    const iText = new fabric.IText("Hello world!");
    
    canvas.setActiveObject(iText);
    iText.enterEditing();
    iText.selectAll();
    canvas.add(iText);
    

    https://codesandbox.io/s/fabricjs-playground-forked-jl0ui?file=/src/index.js