How to flip an Ellipse horizontally using FabricJS?


In this tutorial, we are going to learn how we can flip an Ellipse object horizontally 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. We can flip an ellipse object horizontally using the flipX property.

Syntax

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

Options Keys

  • flipX − This property accepts a Boolean value. It allows us to flip an object horizontally.

Example 1

Passing flipX as key with a 'false' value

The following example shows us the default orientation of an ellipse object in FabricJS. Since we are passing the flipX property a "false" value, the ellipse object will not be flipped horizontally.

<!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 flip an Ellipse horizontally using FabricJS?</h2>
      <p>Select the object and flip it horizontally.</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: 215,
            top: 100,
            fill: "green",
            rx: 100,
            ry: 60,
            stroke: "#228b22",
            strokeWidth: 2,
            flipX: false,
         });

         // Create gradient fill
         ellipse.set("fill", new fabric.Gradient({
            type: "linear",
            coords: {
               x1: 0,
               y1: 0,
               x2: 50,
               y2: 0
            },
            colorStops: [{
               offset: 0,
               color: "red"
            }, {
                  offset: 1,
                  color: "green"
            }, ],
         }));

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

Example 2

Passing the flipX property as key with a 'true' value

In this example, we have an ellipse object of horizontal radius 100 and vertical radius 60 with a horizontal linear gradient fill. As we apply the flipX property to the ellipse object, it flips horizontally and thus we see that the gradient has flipped as well.

<!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 flip an Ellipse horizontally using FabricJS?</h2>
      <p>Select the object and try to flip it horizontally. Here we have set the <b>flipX</b> property as True. </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: 215,
            top: 100,
            fill: "green",
            rx: 100,
            ry: 60,
            stroke: "#228b22",
            strokeWidth: 2,
            flipX: true,
         });

         // Create gradient fill
         ellipse.set("fill", new fabric.Gradient({
            type: "linear",
            coords: {
               x1: 0,
               y1: 0,
               x2: 50,
               y2: 0
            },
            colorStops: [{
               offset: 0,
               color: "red"
            }, {
                  offset: 1,
                  color: "green"
            }, ],
         }));

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

Updated on: 24-May-2022

90 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements