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


In this tutorial, we are going to learn how to set the angle of skew on X-axis of an Ellipse using FabricJS. Ellipse is one of the various shapes provided by FabricJS. In order to create an ellipse, we will 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 X-axis. We can do this by using the skewX property.

Syntax

new fabric.Ellipse({ skewX : 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 skewX is a property.

Options Keys

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

Example 1

When the skewX property is not applied

Let's take an example to understand how our ellipse object appears when the skewX property is not applied. In this case, there will be no skew by any angle 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>Setting the angle of skew on the X-axis of an Ellipse using FabricJS</h2>
      <p>Notice that by default the object is not skewed. Here we have not applied the <b>skewX</b> property. However you can elect the object and stretch it horizontally or vertically by pressing the <b>shift</b> key. The object will get skewed.</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 skewX as key and assigning a custom value to it.

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.

<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 x-axis of Ellipse using FabricJS?</h2>
      <p>Notice that the ellipse is skewed on the X-axis by 50 degrees in the clockwise direction because here we have applied the <b>skewX</b> property and given it a value of 50.</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",
            skewX: 50,
         });

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

Updated on: 25-May-2022

74 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements