How to set the style of individual characters in IText using FabricJS?

In this tutorial, we are going to learn how to set the style of individual characters in IText 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. We can manipulate our IText object by using various properties. Likewise, we can also set the style of individual characters by using the styles property.

Syntax

new fabric.IText(text: String , { styles: Object }: Object)

Parameters

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

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

Options Keys

  • styles ? This property accepts an Object value which allows us to add styles to individual characters. The structure is styles: { lineNumber: { characterNumber: { styleProperties } } }.

Example 1: Default IText Appearance

Let's see a code example to see how our IText object looks when styles property is not used. In this case, all characters maintain uniform styling.

<!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>Default appearance of the IText object</h2>
   <p>You can see that no styles have been added to individual characters</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: 310,
         top: 70,
         fill: "#9455da",
      });

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

Example 2: Using Individual Character Styles

In this example we will see how we can add individual styles to characters by using the styles property. The first level property is the line number and second level property is the character number. Here we are using 0 for both which means first line and first character.

<!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>Passing the styles property as key</h2>
   <p>You can see that the first character looks different now</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: 310,
         top: 70,
         fill: "#9455da",
         styles: {
            0: {
               0: {
                  fontSize: 55,
                  fontWeight: "bold",
                  fontStyle: "oblique",
               },
            },
         },
      });

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

How Character Styling Works

The styles object uses a nested structure where:

  • First level - Line number (0-indexed)
  • Second level - Character position in that line (0-indexed)
  • Third level - Style properties like fontSize, fontWeight, fill, etc.

You can style multiple characters by adding more entries to the styles object. For example, to style the first three characters:

styles: {
   0: {
      0: { fontSize: 30, fill: "red" },
      1: { fontWeight: "bold", fill: "blue" },
      2: { fontStyle: "italic", fill: "green" }
   }
}

Conclusion

The styles property in FabricJS IText allows precise character-level formatting using a nested object structure. This enables creating rich text effects by applying different styles to individual characters within the same text object.

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

404 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements