FabricJS – Finding the Transform Matrix that Represents the Current Transformations for a Polygon Object?

We can create a Polygon object by creating an instance of fabric.Polygon. A polygon object can be characterized by any closed shape consisting of a set of connected straight line segments. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity etc. In order to find the transform matrix which represents current transformations, we use the calcOwnMatrix method.

Syntax

calcOwnMatrix(): Array

Understanding Transform Matrix

The transform matrix is a 6-element array that represents all transformations applied to an object: [scaleX, skewY, skewX, scaleY, translateX, translateY]. This matrix format is used in 2D graphics to efficiently combine multiple transformations.

Example 1: Using the calcOwnMatrix Method

Let's see a code example of how we can find the transform matrix which represents current transformations of a polygon by using the calcOwnMatrix method. You can open the console from dev tools to see that the array value is being displayed.

<!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 calcOwnMatrix method</h2>
   <p>
      You can open console from dev tools and see that the logged output contains the transform matrix of the polygon instance
   </p>
   <canvas id="canvas"></canvas>
   <script> 
      
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiating a polygon object
      var polygon = new fabric.Polygon(
         [
            { x: 200, y: 10 },
            { x: 250, y: 50 },
            { x: 250, y: 180 },
            { x: 150, y: 180 },
            { x: 150, y: 50 },
            { x: 200, y: 10 },
         ],
         {
            fill: "green",
            stroke: "blue",
            strokeWidth: 20,
            skewX: 15,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using calcOwnMatrix method
      console.log(
         "The transform matrix which represents current transformation of the polygon instance is: ",
         polygon.calcOwnMatrix()
      );
   </script>
</body>
</html>
The transform matrix which represents current transformation of the polygon instance is: [1, 0, 0.2679491924311228, 1, 0, 0]

Example 2: Using the calcOwnMatrix Method with ScaleX Property

Let's see a code example to understand how the values of the returned array are affected when we apply horizontal scaling to the polygon object. Here, we have passed the scaleX property a value of 2. This ensures that our polygon object is scaled by 2 in the horizontal direction. We can also see in the console that the 0th index value of the returned array has changed. This is because the 0th index signifies the scaleX value.

<!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 calcOwnMatrix method along with scaleX property</h2>
   <p>
      You can open console from dev tools and see that the logged output has changed
   </p>
   <canvas id="canvas"></canvas>
   <script>
      
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiating a polygon object
      var polygon = new fabric.Polygon(
         [
            { x: 200, y: 10 },
            { x: 250, y: 50 },
            { x: 250, y: 180 },
            { x: 150, y: 180 },
            { x: 150, y: 50 },
            { x: 200, y: 10 },
         ],
         {
            fill: "green",
            stroke: "blue",
            strokeWidth: 20,
            skewX: 15,
            scaleX: 2,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using calcOwnMatrix method
      console.log(
         "The transform matrix which represents current transformation of the polygon instance is: ",
         polygon.calcOwnMatrix()
      );
   </script>
</body>
</html>
The transform matrix which represents current transformation of the polygon instance is: [2, 0, 0.2679491924311228, 1, 0, 0]

Matrix Values Explanation

The returned array represents the transformation matrix in the format [a, b, c, d, e, f], where:

  • a (index 0): Horizontal scaling (scaleX)
  • b (index 1): Vertical skewing
  • c (index 2): Horizontal skewing (skewX)
  • d (index 3): Vertical scaling (scaleY)
  • e (index 4): Horizontal translation
  • f (index 5): Vertical translation

Conclusion

The calcOwnMatrix method provides a convenient way to retrieve the current transformation matrix of a FabricJS polygon object. This matrix is essential for understanding and manipulating object transformations programmatically.

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

616 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements