How to flip an Ellipse vertically using FabricJS?


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

Syntax

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

Options Keys

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

Example 1

Passing flipY as key with a 'false' value

Let's see an example, that shows us the default orientation of an ellipse object in FabricJS. Since we are passing the flipY property a "false" value, the ellipse object will not flip vertically.

<!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 vertically using FabricJS?</h2>
      <p>Select the object and try to flip it vertically.</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,
            flipY: false,
         });

         // Create gradient fill
         ellipse.set("fill", new fabric.Gradient({
            type: "linear",
            coords: {
               x1: 0,
               y1: 0,
               x2: 0,
               y2: 50
            },
            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 flipY 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 vertical linear gradient fill. As we apply the flipY property to the ellipse object, it flips vertically 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 vertically using FabricJS?</h2>
      <p>Select the object and try to flip it vertically. Here we have set the <b>flipY</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,
            flipY: true,
         });

         // Create gradient fill
         ellipse.set("fill", new fabric.Gradient({
            type: "linear",
            coords: {
               x1: 0,
               y1: 0,
               x2: 0,
               y2: 50
            },
            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

89 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements