How to set the fill colour of a Rectangle using FabricJS?


In this tutorial, we are going to learn how we can change the appearance of a Rectangle object by changing its fill 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.

We can change the fill colour by using the fill property which allows us to specify the colour of the object’s fill.

Syntax

new fabric.Rect({ fill: 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 object of which fill is a property.

Options keys

  • fill − This property accepts a String which allows us to change an object's fill colour. Its default value is rgb(0,0,0) which is the colour black.

Example 1

Default fill colour of a rectangle object

Let’s see a code example that shows us the default fill colour of a rectangle object in FabricJS. If we completely skip the fill property while creating a rectangle object, it will be rendered with the fill colour as black.

<!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 fill colour of a rectangle object</h2>
   <p>You can click on the rectangle and interact with it</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,
         borderColor: "purple",
         borderScaleFactor: 3,
      });

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

Example 2

Passing the fill property as key

We can also assign the fill property any colour name or RGBA value. In this example, we have assigned it a value of “grey” which thus changes the fill colour accordingly.

<!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 fill property as key</h2>
   <p>You can see the new fill colour of rectangle object</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,
         borderColor: "purple",
         borderScaleFactor: 3,
         fill: "grey",
      });

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

Updated on: 30-Jun-2022

375 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements