
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Disable Uniform Scaling in Canvas Using Fabric.js
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: 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> <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.Rect 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
Example: 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. 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.Rect 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>