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 the centered rotation of Text using FabricJS?
In this tutorial, we are going to learn how to disable the centered rotation of Text using FabricJS. We can display text on canvas by adding an instance of fabric.Text. Not only does it allow us to move, scale and change the dimensions of the text but it also provides additional functionality like text alignment, text decoration, line height which can be obtained by the properties textAlign, underline and lineHeight respectively. By default all objects in FabricJS use their center as the point of rotation. However, we can change this behaviour by using the centeredRotation property.
Syntax
new fabric.Text(text: String, { centeredRotation: Boolean }: Object)
Parameters
text ? This parameter accepts a String which is the text string that we want to display.
options (optional) ? This parameter is an Object which provides additional customizations to our text. Using this parameter colour, cursor, stroke width and a lot of other properties can be changed related to the object of which centeredRotation is a property.
Options Keys
centeredRotation ? This property accepts a Boolean value and allows us to control whether an object uses center point as its origin of transformation when rotated via controls. Its default value is true.
Example 1: Default Centered Rotation Behavior
Let's see a code example that depicts the default behaviour of a text object. Since centeredRotation property is set to true by default, the text object uses its center as the point of rotation.
<!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>Default behaviour of rotation of Text object in FabricJS</h2>
<p>Rotate the text object to see the default value of centeredRotation</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiate a text object
var text = new fabric.Text("Add Sample Text Here", {
width: 200,
top: 70,
left: 50,
});
// Add it to the canvas
canvas.add(text);
</script>
</body>
</html>
Example 2: Disabling Centered Rotation
Now that we've seen the default behaviour, let us see a code example to understand what happens when the centeredRotation property is assigned a false value. When set to false, the text object will rotate around its top-left corner instead of its center.
<!DOCTYPE html>
<html>
<head>
<!-- Adding the Fabric JS Library-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/510/fabric.min.js"></script>
</head>
<body>
<h2>Passing centeredRotation key with the value as "false"</h2>
<p>Rotate the text object to see the changed center of rotation</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiate a text object
var text = new fabric.Text("Add Sample Text Here", {
fill: "green",
width: 200,
top: 70,
left: 50,
centeredRotation: false,
});
// Add it to the canvas
canvas.add(text);
</script>
</body>
</html>
Key Differences
When centeredRotation is set to true (default), the text rotates around its geometric center. When set to false, the rotation point changes to the object's origin point, typically the top-left corner. This behavior is particularly useful when you need precise control over rotation mechanics in your FabricJS applications.
Conclusion
The centeredRotation property in FabricJS provides control over the rotation behavior of text objects. By setting it to false, you can change the rotation point from the center to the origin corner, offering more flexibility in object manipulation.
