How to set the colour of controlling corners of a Rectangle using FabricJS?

In this tutorial, we are going to set the colour of the controlling corners of Rectangle using FabricJS. The cornerColor property allows us to manipulate the colour of the controlling corners when the object is active.

Syntax

new fabric.Rect({ cornerColor: 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 the cornerColor is a property.

Options keys

  • cornerColor ? This property accepts a String which allows us to assign a color to the controlling corners when the object is actively selected. The default value is rgb(178,204,255).

Example 1: Using Color Name

Passing cornerColor as key with a color name as value

Let's see a code example to change the colour by using the cornerColor property. In this case, we have assigned the value "green" to the key which thus makes the controlling corners appear green.

<!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 cornerColor as key with a color name as value</h2>
   <p>Select the rectangle to see the colour of its controlling corners</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: "#cf1020",
         borderColor: "black",
         borderScaleFactor: 3,
         cornerColor: "green",
      });

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

Example 2: Using RGBA Value

Assigning an RGBA value to the cornerColor property

Instead of passing a simple colour name as the String value to the key, we can also assign an RGBA value. RGBA stands for red, green, blue and alpha where alpha is the opacity. Let us see a code example of how we can do that:

<!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>Assigning an RGBA value to the cornerColor property</h2>
   <p>Select the rectangle to see the colour of its controlling corners</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: "#cf1020",
         borderColor: "black",
         borderScaleFactor: 3,
         cornerColor: "rgba(34,139,34,0.9)",
      });

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

Key Points

  • The cornerColor property only affects corners when the object is actively selected
  • You can use color names, hex values, RGB, or RGBA values
  • The controlling corners are the small squares that appear around selected objects for resizing and rotation
  • This property helps improve visual feedback and user interface customization

Conclusion

The cornerColor property in FabricJS provides an easy way to customize the appearance of rectangle controlling corners. Use color names for simplicity or RGBA values for transparency effects to match your application's design.

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

256 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements