How to set the horizontal origin of transformation of IText using FabricJS?

In this tutorial, we are going to learn how to set the horizontal origin of transformation of 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. Similarly, we can also set the horizontal origin of transformation by using the originX property.

Syntax

new fabric.IText(text: String , { originX : String }: 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 originX is a property.

Options Keys

  • originX ? This property accepts a String value which allows us to set the horizontal origin of transformation. The possible values are "left", "right", and "center". Its default value is "left".

Example 1: Default Appearance of IText Object

Let's see a code example to see how our IText object looks when the originX property is not used. In this case, the horizontal origin of transformation will be left which is also the default.

<!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 the horizontal origin of transformation is towards left</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.\nLorem ipsum dolor sit amet",{
            width: 300,
            left: 210,
            top: 70,
            fill: "#e75c22",
         }
      );

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

Example 2: Using originX Property with Value

In this example, we will see how assigning a value to the originX property changes the horizontal origin of transformation. We have used two IText objects in this example to show the difference. In our first IText object, since we have passed the value as "right", the horizontal origin of transformation now lies towards the right. Same left property of 310 is applied to both IText objects but still they are in different horizontal position because of change in the horizontal origin of transformation.

<!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 originX property as key with a value</h2>
   <p>You can see that the first IText object(itext1) has drifted further away while IText2 maintains the default horizontal origin of transformation </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 itext1 = new fabric.IText("First IText object", {
         width: 300,
         left: 310,
         top: 70,
         fill: "#e75c22",
         originX: "right",
      });

      // Initiate another itext object
      var itext2 = new fabric.IText("Second IText object", {
         width: 300,
         left: 310,
         top: 70,
         fill: "#22e75c",
      });

      // Add both to the canvas
      canvas.add(itext1);
      canvas.add(itext2);
   </script>
</body>
</html>

How originX Works

The originX property determines the horizontal reference point for positioning and transformations. When set to "left" (default), the left edge of the text serves as the origin. When set to "right", the right edge becomes the reference point, and "center" uses the middle of the text as the origin.

Conclusion

The originX property in FabricJS IText objects provides precise control over horizontal positioning and transformation origins. Use "left", "right", or "center" values to achieve the desired text alignment and positioning behavior in your canvas applications.

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

252 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements