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

In this tutorial, we are going to learn how to set the style of controlling corners of a 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.

The controlling corners of an object allow us to scale, stretch or change its position. We can customize our controlling corners in many ways such as adding a specific colour to it, changing its size etc. We can change the style by using the cornerStyle property.

Syntax

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

cornerStyle Property

  • cornerStyle ? This property accepts a String that allows us to specify the style of controlling corners. The available values are:

    • "rect" - Square/rectangular corners (default)
    • "circle" - Circular corners

Example 1: Default Corner Style

Let's see a code example that shows the default style of controlling corners (rectangular).

<!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 style of controlling corners</h2>
   <p>Click on the rectangle to see the default style 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: "#3b7a57"
      });

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

Example 2: Circular Corner Style

We can specify the style or appearance of the controlling corners by passing the cornerStyle property with value "circle". This will make the controlling corners appear circular instead of the default rectangular shape.

<!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>Circular corner style</h2>
   <p>Click on the rectangle to see the circular style 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: "#3b7a57",
         cornerStyle: "circle"
      });

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

Comparison of Corner Styles

Corner Style Value Description
Rectangle "rect" Default square/rectangular corners
Circle "circle" Circular corners for a softer appearance

Conclusion

The cornerStyle property in FabricJS allows you to customize the appearance of controlling corners on selected objects. Use "rect" for default rectangular corners or "circle" for a more modern circular appearance.

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

291 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements