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 Rectangle using FabricJS?
In this tutorial, we are going to learn how to disable the centered rotation of Rectangle using FabricJS. Rectangle is one of the various shapes provided by FabricJS. In order to create a rectangle, we will have to create an instance of fabric.Rect 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.Rect({ centeredRotation: Boolean }: Object)
Parameters
-
Options (optional) ? This parameter is an Object which provides additional customizations to our rectangle. 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.
Options Keys
-
centeredRotation ? This property accepts a Boolean value which 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 rectangle object. Since centeredRotation property is set to true by default, the rectangle 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 Rectangle in FabricJS</h2>
<p>Click on the rectangle and rotate it. You will notice that the object rotates around its center, which is the default behaviour.</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 rectangle object
var rect = new fabric.Rect({
left: 125,
top: 90,
width: 170,
height: 70,
fill: "#cf1020",
borderColor: "black",
borderScaleFactor: 3,
});
// Add it to the canvas
canvas.add(rect);
</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. With centered rotation disabled, the rectangle will rotate around a corner point instead of its center.
<!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>Click on the rectangle and rotate it 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 rectangle object
var rect = new fabric.Rect({
left: 125,
top: 90,
width: 170,
height: 70,
fill: "#cf1020",
borderColor: "black",
borderScaleFactor: 3,
centeredRotation: false,
});
// Add it to the canvas
canvas.add(rect);
</script>
</body>
</html>
Key Differences
| Property Value | Rotation Behavior | Use Case |
|---|---|---|
centeredRotation: true (default) |
Rotates around center point | Standard rotation for most objects |
centeredRotation: false |
Rotates around corner/anchor point | Creating specific rotation effects |
Conclusion
The centeredRotation property in FabricJS allows you to control the rotation behavior of rectangle objects. Setting it to false disables center-based rotation, making the object rotate around an anchor point instead.
