How to disable uniform scaling on a canvas using FabricJS?

FabricJSJavascriptHTML5 Canvas

In this article, we are going to learn about how to disable uniform scaling in canvas using FabricJS. In FabricJS, an object gets transformed proportionally when dragged from the corners. However, we can disable this behavior by using the uniformScaling property

Syntax

new fabric.Canvas(element: HTMLElement|String, { uniformScaling: Boolean }: Object)

Parameters

  • element − This parameter is the <canvas> element itself which can be derived using document.getElementById() or the id of the <canvas> element itself. The FabricJS canvas will be initialized on this element.

  • options (optional) − This parameter is an Object which provides additional customizations to our canvas. Using this parameter, properties such as color, cursor, border width, and a lot of other properties can be changed related to the canvas, of which uniformScaling is a property. It accepts a Boolean value which determines if the object should be scaled proportionally or not. It's default value is True.

Example 1

Passing the uniformScaling as a key with the value as False

Let's see a code example of how an object gets scaled non-uniformly when uniformScaling is set to False. Since FabricJS version 4, the property uniScaleTransform has been removed and replaced by uniformScaling.

<!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>Disabling uniform scaling in canvas using FabricJS</h2>
   <p>Select the object and resize it. The object will scale non-uniformly as we have set the <b>uniformScaling</b> property to False. </p>
   <canvas id="canvas"></canvas>
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas", {
         // UniformScaling is disabled
         uniformScaling: false
      });
      // Creating an instance of the fabric.Rect class
      var circle = new fabric.Circle({
         left: 70,
         top: 90,
         radius: 40,
         fill: "#006400",
      });
      // Adding it to the canvas
      canvas.add(circle);
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
   </script>
</body>
</html>

Example 2

Passing uniformScaling key to the class with the value as True

Now that we have seen how scaling occurs non-uniformly when uniformScaling is False, we can enable uniformScaling to avoid that by passing uniformScaling key to the class as True. Let's see how the code looks like −

<!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>Disabling uniform scaling in canvas using FabricJS</h2>
   <p>Select the object and resize it by dragging its corners. The object will scale uniformly as we have set the <b>uniformScaling</b> property to True. </p>
   <canvas id="canvas"></canvas>
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas", {
         // UniformScaling is enabled
         uniformScaling: true
      });
      // Creating an instance of the fabric.Rect class
      var circle = new fabric.Circle({
         left: 70,
         top: 90,
         radius: 40,
         fill: "#006400",
      });
      // Adding it to the canvas
      canvas.add(circle);
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
   </script>
</body>
</html>
raja
Updated on 20-May-2022 06:22:32

Advertisements