How to set the angle of skew on x-axis of Rectangle using FabricJS?

In this tutorial, we are going to learn how to set the angle of skew on the x-axis of a Rectangle using FabricJS. Rectangle is one of the various shapes provided by FabricJS. In order to create a rectangle, we will have to create an instance of fabric.Rect class and add it to the canvas.

Our rectangle object can be customized in various ways like changing its dimensions, adding a background color or by changing the angle of skew on the x-axis. We can do this by using the skewX property.

Syntax

new fabric.Rect({ skewX: Number })

Parameters

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

Options Keys

  • skewX ? This property accepts a Number which determines the angle of skew on X-axis of an object in degrees.

Example 1: Default Rectangle (No Skew)

Let's see a code example to understand how our rectangle object appears when the skewX property is not applied. In this case, there will be no skew by any angle in our rectangle 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>Rectangle without skewX property</h2>
   <p>You can see there is no skew by any angle on the rectangle by default</p>
   <canvas id="canvas"></canvas>
   
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(400);
      canvas.setHeight(250);

      // Initiate a rectangle object
      var rect = new fabric.Rect({
         left: 105,
         top: 70,
         width: 170,
         height: 70,
         fill: "#8f9779",
         stroke: "#8fbc8f",
         strokeWidth: 9,
      });

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

Example 2: Rectangle with X-Axis Skew

In this example, we will see how we can assign a numerical value to the skewX property. The value that is being passed will determine the skew along the X-axis in degrees.

<!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>Rectangle with skewX property set to 50 degrees</h2>
   <p>You can see the rectangle has skewed at a 50 degree angle on X-axis</p>
   <canvas id="canvas"></canvas>
   
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(400);
      canvas.setHeight(250);

      // Initiate a rectangle object
      var rect = new fabric.Rect({
         left: 105,
         top: 70,
         width: 170,
         height: 70,
         fill: "#8f9779",
         stroke: "#8fbc8f",
         strokeWidth: 9,
         skewX: 50,
      });

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

How It Works

The skewX property transforms the rectangle by slanting it along the horizontal axis. Positive values skew the shape to the right, while negative values would skew it to the left. The transformation preserves the rectangle's height but changes its appearance by creating a parallelogram-like shape.

Common Use Cases

  • Creating isometric or 3D-like effects in 2D graphics
  • Adding visual perspective to rectangular elements
  • Creating dynamic animations with skew transformations
  • Designing logos or graphics with slanted text backgrounds

Conclusion

The skewX property in FabricJS provides an easy way to transform rectangles along the X-axis. By adjusting the skew angle, you can create various visual effects and perspectives for your canvas elements.

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

249 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements