How to create a Rectangle with background colour using FabricJS?


In this tutorial, we are going to create a Rectangle with background colour 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.

The backgroundColor property allows us to assign a colour to our object’s background. It is the colour of the container where the rectangle lives and is rectangular in shape for the rectangle.

Syntax

new fabric.Rect({ backgroundColor: String }: Object)

Parameters

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

Options Keys

  • backgroundColor − This property accepts a value of type String which will determine the background colour.

Example 1

Passing backgroundColor property as key with a hexadecimal value

Let’s see a code example to assign a background colour to our Rectangle object using a hexadecimal value. In this example, we have used the hex colour code #ffb347 which is an orange colour.

<!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 backgroundColor property as key with a hexadecimal value</h2>
   <p>You can see the new background colour on the rectangle</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: 115,
         top: 50,
         width: 100,
         height: 70,
         fill: "rgba(255,255,255, 0.1)",
         borderColor: "black",
         backgroundColor: "#ffb347",
      });

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

Example 2

Passing backgroundColor property as key with an RGBA value

We can use an RGBA value, instead of a hexadecimal colour code. The alpha parameter specifies the opacity of a colour. In this example, we have used the rgba value (207,16,32,0.9) which is the colour red with 0.9 opacity.

<!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 backgroundColor property as key with an RGBA value</h2>
   <p>You can see the new background colour on the rectangle</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: 125,
         top: 90,
         width: 170,
         height: 70,
         fill: "rgba(255,255,255, 0.1)",
         borderColor: "black",
         backgroundColor: "rgba(207,16,32,0.9)",
      });

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

Updated on: 28-Jun-2022

362 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements