How to set the opacity of a Rectangle using FabricJS?

In this tutorial, we are going to learn how to set the opacity of Rectangle using FabricJS. Rectangle is one of the various shapes provided by FabricJS. In order to create a rectangle, we will have to create an instance of fabric.Rect class and add it to the canvas.

We can customize a rectangle object by adding a fill colour to it, eliminate its borders or even make changes in its dimensions. Similarly, we can also change its opacity by using the opacity property.

Syntax

new fabric.Rect({ opacity: Number }: Object)

Parameters

  • Options (optional) ? This parameter is an Object which provides additional customizations to our rectangle. Using this parameter, properties such as colour, cursor, border width and a lot of other properties can be changed related to the object of which opacity is a property.

Options Keys

  • opacity ? This property accepts a Number that allows us to control the opacity of an object. The value ranges from 0 (completely transparent) to 1 (completely opaque). Default value of opacity property is 1.

Example 1: Default Appearance of Rectangle

Let's see a code example to see how our rectangle object looks like with the default value of opacity property. We will not pass any opacity key to the class in this example as given below:

<!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>Default appearance of a rectangle object</h2>
   <p>You can see our rectangle is fully opaque</p>
   <canvas id="canvas"></canvas>
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);

      // Initiate a rectangle object
      var rect = new fabric.Rect({
         left: 55,
         top: 90,
         width: 170,
         height: 70,
         fill: "#ffb347",
         padding: 9,
         stroke: "#191970",
         strokeWidth: 5,
      });

      // Add it to the canvas
      canvas.add(rect);
   </script>
</body>
</html>

Example 2: Setting Custom Opacity

In this example, we will see how assigning a value to the opacity property changes the opacity of the rectangle object in our canvas. Here we have used 0.3 as opacity which makes our rectangle object look translucent instead of fully opaque.

<!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>Passing the opacity property as key</h2>
   <p>You can see our rectangle is not fully opaque</p>
   <canvas id="canvas"></canvas>
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);

      // Initiate a rectangle object
      var rect = new fabric.Rect({
         left: 55,
         top: 90,
         width: 170,
         height: 70,
         fill: "#ffb347",
         padding: 9,
         stroke: "#191970",
         strokeWidth: 5,
         opacity: 0.3,
      });

      // Add it to the canvas
      canvas.add(rect);
   </script>
</body>
</html>

Key Points

  • The opacity property accepts values from 0 to 1, where 0 is completely transparent and 1 is completely opaque.

  • Default opacity value is 1, making objects fully visible by default.

  • Opacity affects both the fill color and stroke of the rectangle.

Conclusion

The opacity property in FabricJS provides an easy way to control the transparency of rectangle objects. By adjusting values between 0 and 1, you can create various visual effects from completely transparent to fully opaque rectangles.

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

550 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements