How to flip a Circle horizontally using FabricJS?

In this tutorial, we are going to learn how we can flip a Circle object horizontally using FabricJS. Circle is one of the various shapes provided by FabricJS. In order to create a circle, we have to create an instance of fabric.Circle class and add it to the canvas. We can flip a circle object horizontally using the flipX property.

Syntax

new fabric.Circle({ flipX: Boolean }: Object)

Parameters

  • options (optional) ? This parameter is an Object which provides additional customizations 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 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

Let's see an example that shows us the default orientation of a circle object in FabricJS. Since we are passing the flipX property a "False" value, the circle 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 a Circle horizontally using FabricJS?</h2>
      <p>Here the circle has not flipped horizontally as we have set <b>flipX</b> as False. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 215,
            top: 50,
            fill: "green",
            radius: 80,
            stroke: "#228b22",
            strokeWidth: 2,
            flipX: false
         });

         // Create gradient fill
         circle.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(circle);
         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 a circle object of radius 80px with a horizontal linear gradient fill. As we apply the flipX property to the circle 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 a Circle horizontally using FabricJS?</h2>
      <p>Here, notice that the circle has flipped horizontally (see the gradient) because we have set <b>flipX</b> as True. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 215,
            top: 50,
            fill: "green",
            radius: 80,
            stroke: "#228b22",
            strokeWidth: 2,
            flipX: true
         });

         // Create gradient fill
         circle.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(circle);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

Key Points

  • The flipX property accepts a Boolean value (true/false)
  • When flipX is set to true, the circle is flipped horizontally
  • The flip effect is most visible when the circle has asymmetric features like gradients or patterns
  • Regular solid-colored circles appear unchanged when flipped since they are symmetric

Conclusion

The flipX property in FabricJS allows you to horizontally flip circle objects. While regular circles appear unchanged due to symmetry, the effect becomes evident with gradients or patterns, making it useful for creating mirror effects in canvas applications.

Updated on: 2026-03-15T23:19:00+05:30

312 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements