Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to disable uniform scaling on a canvas using FabricJS?
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. Its default value is True.
Understanding Uniform Scaling
When uniform scaling is enabled (default behavior), objects maintain their aspect ratio when resized. When disabled, objects can be stretched or compressed independently in width and height directions.
Example 1: Disabling Uniform Scaling
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.Circle 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: Enabling Uniform Scaling
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 maintain aspect ratio 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>Enabling 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.Circle 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>
Key Differences
| uniformScaling Value | Behavior | Use Case |
|---|---|---|
false |
Non-uniform scaling (can distort aspect ratio) | When you need flexible resizing |
true (default) |
Uniform scaling (maintains aspect ratio) | When preserving shape proportions is important |
Conclusion
The uniformScaling property in FabricJS controls how objects resize when dragged from corners. Set it to false for flexible resizing or true to maintain aspect ratios. This property gives you fine control over canvas object behavior.
