How to set the minimum allowed scale value of Rectangle using FabricJS?

In this tutorial, we are going to learn how to set the minimum allowed scale of 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.

We can customize a rectangle object by adding a fill colour to it, eliminate its borders or even make changes in its dimensions. Similarly, we can also set its minimum allowed scale by using the minScaleLimit property.

Syntax

new fabric.Rect({ minScaleLimit : Number }: Object)

Parameters

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

Options Keys

  • minScaleLimit ? This property allows us to control the minimum allowed scale value of a rectangle. It accepts a Number as a value and prevents the object from being scaled below this limit.

Example 1: Default Rectangle Without Scale Limit

Let's see a code example to see how our rectangle object looks like when the minScaleLimit property is not used. In this case, we will be able to freely scale our object since there is no minimum limit set.

<!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 the rectangle object</h2>
   <p>You can try scaling the rectangle to see that there is no minimum allowed scale value.</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: 155,
         top: 90,
         width: 170,
         height: 70,
         fill: "#6f2da8",
         padding: 9,
         stroke: "#b666d2",
         strokeWidth: 5,
      });

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

Example 2: Using minScaleLimit Property

In this example, we will see how assigning a value to the minScaleLimit property changes the minimum allowed scale value of the rectangle object in our canvas. Here we have used 0.8 as value which means we will not be able to scale down our object lesser than a width of 136px and a height of 56px which is calculated by original dimension * limit (0.8 * 170 = 136px, 0.8 * 70 = 56px).

<!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 minScaleLimit property as key with a custom value</h2>
   <p>You can try scaling the rectangle and observe that it isn't possible to scale down the rectangle lesser than a width of 136px and height of 56px.</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: 155,
         top: 90,
         width: 170,
         height: 70,
         fill: "#6f2da8",
         padding: 9,
         stroke: "#b666d2",
         strokeWidth: 5,
         minScaleLimit: 0.8,
      });

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

How It Works

The minScaleLimit property acts as a multiplier that prevents users from scaling the rectangle below the specified threshold. When you try to scale the object smaller than the minimum limit, FabricJS automatically stops the scaling operation at that point.

Conclusion

The minScaleLimit property in FabricJS provides essential control over rectangle scaling behavior. Use values between 0 and 1 to set appropriate minimum scale limits for your interactive canvas applications.

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

223 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements