How to create a canvas with IText using FabricJS?

In this tutorial, we are going to learn about how to create a canvas with an IText object using FabricJS. The IText class was introduced in FabricJS version 1.4, extends fabric.Text and is used to create IText instances. An IText instance gives us the freedom to select, cut, paste or add new text without additional configurations. There are also various supported key combinations and mouse/touch combinations which make text interactive which are not provided in Text.

Textbox, however, which is based on IText allows us to resize the text rectangle and wraps lines automatically. This is not true for IText as height is not adjusted based on the wrapping of lines.

Syntax

new fabric.IText( text: String , options: Object)

Parameters

  • text ? This parameter accepts a String which is the text string that we want to display as our text.

  • options (optional) ? This parameter is an Object which provides additional customizations to our IText object. Using this parameter colour, cursor, stroke width and a lot of other properties can be changed related to the IText object.

Example 1: Creating an IText Instance

Let's see a code example of how we can add an IText object to our canvas. The only required parameter is the actual text string whereas the second argument is the optional options object which allows us to assign different properties to the IText object.

<!DOCTYPE html>
<html>
<head>
   <!-- Adding the Fabric JS Library-->
   <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
</head>
<body>
   <h2>Creating an instance of fabric.IText() and adding it to our canvas</h2>
   <p>You can see an itext object has been created</p>
   <canvas id="canvas"></canvas>

   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);

      // Initiate an itext object
      var itext = new fabric.IText("Add sample text here.", {
         width: 300,
         left: 50,
         top: 70,
         fill: "red",
      });

      // Add it to the canvas
      canvas.add(itext);
   </script>
</body>
</html>

Example 2: Using the Set Method

In this example, we have assigned the properties to the IText object by using the set method which is a setter for values. Any property related to stroke, strokeWidth, angle, scaling, rotation, etc., can be mutated by using this method.

<!DOCTYPE html>
<html>
<head>
   <!-- Adding the Fabric JS Library-->
   <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
</head>
<body>
   <h2>Manipulating the IText object by using the set method</h2>
   <p>You can see the itext in the canvas now with set values</p>
   <canvas id="canvas"></canvas>

   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);

      // Initiate an itext object
      var itext = new fabric.IText("Add sample text here.");
      
      // Set the properties
      itext.set("top", 70);
      itext.set("left", 65);
      itext.set("fill", "white");
      itext.set("fontWeight", "bold");
      itext.set("backgroundColor", "#bf94e4");

      // Add it to the canvas
      canvas.add(itext);
   </script>
</body>
</html>

Key Features of IText

The IText object provides several interactive features that make it different from regular Text objects:

  • Editable text: Users can double-click to enter edit mode
  • Text selection: Supports text selection with mouse or keyboard
  • Copy/Paste: Standard copy and paste operations work
  • Keyboard shortcuts: Ctrl+A (select all), Ctrl+C (copy), Ctrl+V (paste)

Conclusion

FabricJS IText provides an interactive text editing experience on canvas. Use the constructor with text and options, or the set method to customize properties like font, color, and positioning.

Updated on: 2026-03-15T23:19:00+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements