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


In this tutorial, we are going to learn how to set the angle of skew on Y-axis of an Ellipse using FabricJS. Ellipse is one of the various shapes provided by FabricJS. In order to create an ellipse, we will have to create an instance of fabric.Ellipse class and add it to the canvas. Our ellipse 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.Ellipse({ skewY : Number }: Object)

Parameters

  • options (optional) − This parameter is an Object which provides additional customizations to our ellipse. Using this parameter color, 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 to understand how our ellipse object appears when the skewY property is not applied. In this case, there will be no skew in our ellipse 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>How to set the angle of skew on Y-axis of Ellipse using FabricJS?</h2>
      <p>Notice there is no angle of skew here as we have not applied the <b>skewX</b> or <b>skewY</b> property. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");

         // Initiate an ellipse instance
         var ellipse = new fabric.Ellipse({
            left: 115,
            top: 50,
            rx: 80,
            ry: 50,
            fill: "#ff1493",
         });

         // Adding it to the canvas
         canvas.add(ellipse);
         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 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>How to set the angle of skew on y-axis of Ellipse using FabricJS?</h2>
      <p>Here the ellipse is skewed on the Y-axis by 30 degrees, as we have applied the <b>skewY</b> property. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");

         // Initiate an ellipse instance
         var ellipse = new fabric.Ellipse({
            left: 115,
            top: 50,
            rx: 80,
            ry: 50,
            fill: "#ff1493",
            skewY: 30,
         });

         // Adding it to the canvas
         canvas.add(ellipse);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

Updated on: 25-May-2022

80 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements