How to get the object scale factor of Text using FabricJS?

In this tutorial, we are going to learn how to get the object scale factor of Text using FabricJS. We can display text on canvas by adding an instance of fabric.Text. Not only does it allow us to move, scale and change the dimensions of the text but it also provides additional functionality like text alignment, text decoration, line height which can be obtained by the properties textAlign, underline and lineHeight respectively. We can also find the object scale factor by using the getObjectScaling method.

Syntax

getObjectScaling()

This method returns an object containing the scaleX and scaleY values of the text object.

Example 1: Using the getObjectScaling method

Let's see a code example to see the logged output when the getObjectScaling method is used. In this case, the default scaleX and scaleY values will be displayed in the console which are 1.

<!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>Using the getObjectScaling method</h2>
   <p>You can open console from dev tools and see that the logged value is being displayed in the console</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 a text object
      var text = new fabric.Text("Add sample\ntext here", {
         width: 300,
         fill: "green",
         fontWeight: "bold",
      });

      // Add it to the canvas
      canvas.add(text);

      // Using getObjectScaling method
      console.log("The scale factor is", text.getObjectScaling());
   </script>
</body>
</html>
The scale factor is {scaleX: 1, scaleY: 1}

Example 2: Using the getObjectScaling method with scaleX property

Let's see a code example to see the logged output when the getObjectScaling method is used in conjunction with the scaleX property. In this case, the scaleX value of the text object will be displayed as 2 while the scaleY value will remain the same.

<!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>Using the getObjectScaling method and passing the scaleX property</h2>
   <p>You can open console from dev tools and see that the logged value is being displayed in the console </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 a text object
      var text = new fabric.Text("Add sample\ntext here", {
         width: 300,
         fill: "green",
         fontWeight: "bold",
         scaleX: 2,
      });

      // Add it to the canvas
      canvas.add(text);

      // Using getObjectScaling method
      console.log("The scale factor is", text.getObjectScaling());
   </script>
</body>
</html>
The scale factor is {scaleX: 2, scaleY: 1}

Return Value

The getObjectScaling() method returns an object with two properties:

  • scaleX: The horizontal scaling factor
  • scaleY: The vertical scaling factor

Key Points

  • Default scale values are 1 for both X and Y axes
  • Scale values greater than 1 make the object larger
  • Scale values less than 1 make the object smaller
  • The method is useful for determining current transformation state

Conclusion

The getObjectScaling() method provides an easy way to retrieve the current scale factors of a Fabric.js text object. This is particularly useful when you need to track or manipulate object transformations programmatically.

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

313 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements