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 lock the horizontal scaling of Ellipse using FabricJS?
In this tutorial, we are going to learn how to lock the horizontal scaling of an Ellipse using FabricJS. Just as we can specify the position, color, opacity and dimension of an ellipse object in the canvas, we can also specify whether we want to stop scaling an object horizontally. This can be done by using the lockScalingX property.
Syntax
new fabric.Ellipse({ lockScalingX : Boolean }: Object)
Parameters
options (optional) ? This parameter is an Object which provides additional customizations to our ellipse. Using this parameter color, cursor, stroke width and a lot of other properties can be changed related to the object of which lockScalingX is a property.
Options Keys
lockScalingX ? This property accepts a Boolean value. If we assign it a 'true' value, then the object's horizontal scaling will be locked.
Example 1: Default Behavior
Default behaviour of an Ellipse object in the canvas
Let's take an example to understand the default behaviour of an Ellipse object when lockScalingX property is not used. Scaling the object in both horizontal and vertical directions is feasible.
<!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>How to lock the horizontal scaling of Ellipse using FabricJS?</h2>
<p>Here you can select the object and scale it both horizontally and vertically because we have not used the <b>lockScalingX</b> property. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate an ellipse instance
var ellipse = new fabric.Ellipse({
left: 115,
top: 50,
fill: "white",
rx: 80,
ry: 50,
stroke: "black",
strokeWidth: 5,
});
// Adding it to the canvas
canvas.add(ellipse);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
Example 2: Locking Horizontal Scaling
Passing lockScalingX as key with 'true' value
In this example, we will see how we can cease the ability of an ellipse object to scale horizontally using the lockScalingX property. Although we can scale the ellipse object vertically, we are not allowed to perform the same action horizontally.
<!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>How to lock the horizontal scaling of Ellipse using FabricJS?</h2>
<p>Here you can select the object and scale it vertically, but you can't scale it horizontally because we have set <b>lockScalingX</b> to True. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate an ellipse instance
var ellipse = new fabric.Ellipse({
left: 115,
top: 50,
fill: "white",
rx: 80,
ry: 50,
stroke: "black",
strokeWidth: 5,
lockScalingX: true,
});
// Adding it to the canvas
canvas.add(ellipse);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
Key Points
Setting
lockScalingX: trueprevents horizontal scaling while allowing vertical scalingThe default value is
false, which allows scaling in all directionsThis property is useful when you want to maintain the width of an ellipse while allowing height adjustments
Conclusion
The lockScalingX property provides precise control over ellipse scaling behavior in FabricJS. Use it when you need to restrict horizontal scaling while preserving vertical scaling capabilities.
