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 in 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.
Example: Disabling uniform scaling (uniformScaling: 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>
<div style="padding: 10px; font-weight: bold; margin-left: 32px">
How to disable uniform scaling in canvas using FabricJS?
</div>
<canvas
id="canvas"
width="500"
height="300"
style="border: 2px solid #000000">
</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);
</script>
</body>
</html>
Output
.gif)
Example: Enabling uniform scaling (uniformScaling: true)
Now that we have seen how scaling occurs non-uniformly when uniformScaling is False, we can enable uniformScaling to maintain proportional scaling. 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>
<div style="padding: 10px; font-weight: bold; margin-left: 32px">
How to disable uniform scaling in canvas using FabricJS?
</div>
<canvas
id="canvas"
width="500"
height="300"
style="border: 2px solid #000000">
</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);
</script>
</body>
</html>
Output
.gif)
Key Differences
| Property Value | Scaling Behavior | Use Case |
|---|---|---|
uniformScaling: false |
Non-proportional scaling | When you need to stretch objects independently on width/height |
uniformScaling: true |
Proportional scaling (default) | When you want to maintain object aspect ratio |
Conclusion
The uniformScaling property in FabricJS controls whether objects scale proportionally or independently. Set it to false to allow non-uniform scaling, or true to maintain aspect ratios during scaling operations.
