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 Textbox using FabricJS?
In this tutorial, we are going to learn how to disable the centered rotation of Textbox using FabricJS. We can customize, stretch or move around the text written in a textbox. In order to create a textbox, we will have to create an instance of fabric.Textbox class and add it to the canvas. 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.Textbox(text: String, { centeredRotation: Boolean }: Object)
Parameters
-
text ? This parameter accepts a String which is the text string that we want to display inside our textbox.
-
options (optional) ? This parameter is an Object which provides additional customizations to our textbox. Using this parameter, properties such as colour, cursor, stroke width, and a lot of other properties can be changed related to the object of which centeredRotation is a property.
centeredRotation Property
-
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 Behavior
Default behaviour of rotation of Textbox in FabricJS
Let's see a code example that depicts the default behaviour of a textbox object. Since centeredRotation property is set to true by default, the textbox 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 Textbox in FabricJS</h2>
<p>Rotate the textbox 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 textbox object
var textbox = new fabric.Textbox("The past does not equal the future.", {
backgroundColor: "#ffe5b4",
width: 400,
top: 70,
left: 110,
});
// Add it to the canvas
canvas.add(textbox);
</script>
</body>
</html>
Example 2: Disabling Centered Rotation
Passing centeredRotation key with the value as "false"
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. This disables the centered rotation, making the object rotate around a different pivot point.
<!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>Passing centeredRotation key with the value as "false"</h2>
<p>Rotate the textbox 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 textbox object
var textbox = new fabric.Textbox("The past does not equal the future.", {
backgroundColor: "#ffe5b4",
width: 400,
top: 70,
left: 110,
centeredRotation: false,
});
// Add it to the canvas
canvas.add(textbox);
</script>
</body>
</html>
Key Differences
| centeredRotation Value | Rotation Behavior | Use Case |
|---|---|---|
| true (default) | Rotates around object center | Standard rotation behavior |
| false | Rotates around corner/edge point | Custom rotation effects |
Conclusion
The centeredRotation property in FabricJS allows you to control the rotation pivot point of textbox objects. Setting it to false disables centered rotation, providing more flexibility for custom rotation effects and animations.
