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



In this tutorial, we are going to learn how to set the angle of skew on Y-axis of a Circle using FabricJS. Circle is one of the various shapes provided by FabricJS. In order to create a circle, we will have to create an instance of fabric.Circle class and add it to the canvas.Our circle object can be customized in various ways like changing its dimensions, adding a background color or by changing the angle of skew on Y-axis. We can do this by using the skewY property.

Syntax

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

Parameters

  • options (optional) − This parameter is an Object which provides additional ustomizations to our circle. 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 an example to understand how our circle object appears when the skewY property is not applied. In this case, there will be no distortion by any angle in our circle 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>Setting the angle of skew on the Y-axis of a circle using FabricJS</h2>
      <p>Here we have not applied the <b>skewY</b> property, hence there is no distortion in the object. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 50,
            top: 90,
            radius: 50,
            fill: "#ccccff",
            stroke: "#7b68ee",
            strokeWidth: 5
         });
         canvas.add(circle);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </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 distortion along the Y-axis. Since we have assigned the skewY property a value of 30, the effect will be as if the circle object has been pinched across the corner vertically to make a 30-degree angle.

<!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>Setting the angle of skew on Y-axis of circle using FabricJS</h2>
      <p>Notice the circle here has skewed on the Y-axis at an angle of 30 degrees, as we have set <b>skewY</b> at 30. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 50,
            top: 90,
            radius: 50,
            fill: "#ccccff",
            stroke: "#7b68ee",
            strokeWidth: 5,
            skewY: 30,
         });
         canvas.add(circle);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

Advertisements