How to set a dash pattern for the controlling corners of a Rectangle using FabricJS?

In this tutorial, we are going to learn how we can implement the dash pattern of controlling corners of Rectangle using FabricJS. 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 also specify a dash pattern for the controlling corners by using the cornerDashArray property.

Syntax

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

Options keys

  • cornerDashArray ? This property accepts an Array which allows us to specify a dash pattern for the controlling corners. For example, if we pass an array with values [2,3], it means a dash pattern of 2px dash and 3px gap and repeating this pattern infinitely.

Example 1: Default appearance of controlling corners

Let's see a code example that depicts the default appearance of the controlling corners of a rectangle object. Since we have not used the cornerDashArray property, there is no dash pattern being displayed.

<!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 controlling corners</h2>
   <p>Select the rectangle to see the default appearance</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: Passing cornerDashArray property as key

In this example we are passing the cornerDashArray property a value of [1,2,1]. This means that a dash pattern will be created such that there is a 1px long line, followed by a 2px gap, then again a 1px long line will be drawn and after which a 1px gap will be made, and so on.

<!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 cornerDashArray property as key</h2>
   <p>Select the rectangle to see the appearance of the controlling corners with dash pattern</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",
         cornerDashArray: [1, 2, 1],
      });

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

Key Points

  • The cornerDashArray property only affects the visual appearance of controlling corners when the object is selected

  • The dash pattern follows the SVG dash array format: [dash length, gap length, dash length, gap length...]

  • If an odd number of values is provided, the pattern is repeated to make it even

  • Corner styling works best when combined with other corner properties like cornerColor and cornerSize

Conclusion

The cornerDashArray property in FabricJS allows you to create custom dash patterns for rectangle controlling corners. This enhances the visual appearance and provides better customization options for interactive canvas elements.

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

253 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements