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


In this tutorial, we are going to learn how to set the angle of skew on the y-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 Y-axis. We can do this by using the skewY property.

Syntax

new fabric.Rect({ skewY : Number }: Object)

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 skewY is a property.

Options keys

  • skewY − This property accepts a Number which determines the angle of skew on y-axis of an object.

Example 1

When the skewY property is not applied

Let’s see a code example to understand how our rectangle object appears when the skewY 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>When the skewY property is not applied</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(document.body.scrollWidth);
      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

Passing skewY as key and assigning a custom value to it.

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

<!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 skewY as key and assigning a custom value to it</h2>
   <p>You can see the rectangle has been skewed by 30 degrees along the Yaxis</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 rectangle object
      var rect = new fabric.Rect({
         left: 105,
         top: 70,
         width: 170,
         height: 70,
         fill: "#8f9779",
         stroke: "#8fbc8f",
         strokeWidth: 9,
         skewY: 30,
      });

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

Updated on: 30-Jun-2022

147 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements