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 a Circle using FabricJS?
In this tutorial, we are going to learn how to disable the centered rotation of Circle using FabricJS. Circle is one of the various shapes provided by FabricJS. In order to create a circle, we will create an instance of fabric.Circle 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.Circle({ centeredRotation: Boolean }: Object)
Parameters
-
options (optional) ? This parameter is an Object which provides additional customizations to our circle. 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 that allows us to control whether an object uses its origin of transformation as center point when rotated via controls. Its default value is True.
Example 1: Default Behavior of Circle Rotation
Let's see an example that depicts the default behaviour of a circle object. Since centeredRotation property is set to True by default, the circle 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 centered rotation of circle using FabricJs</h2>
<p>Select the object and rotate it by holding its controlling corner at the top. The circle will rotate around its center. It is the default behavior. Here we have not used the <b>centeredRotation</b> property but it is by default set to True.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var cir = new fabric.Circle({
left: 215,
top: 100,
fill: "white",
radius: 50,
stroke: "#c154c1",
strokeWidth: 5,
borderColor: "#daa520",
});
// Adding it to the canvas
canvas.add(cir);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
Example 2: Disabling Centered Rotation
Now that we've seen the default behaviour, let us see a code to understand what happens when the centeredRotation property is assigned False. When disabled, the circle 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/fabric.js/510/fabric.min.js"></script>
</head>
<body>
<h2>Disabling the centered rotation of circle using FabricJs</h2>
<p>Select the object and rotate it by holding its controlling corner at the top. Now the circle will not rotate around its center because we have used the <b>centeredRotation</b> property and set it False.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var cir = new fabric.Circle({
left: 215,
top: 100,
fill: "white",
radius: 50,
stroke: "#c154c1",
strokeWidth: 5,
borderColor: "#daa520",
centeredRotation: false
});
// Adding it to the canvas
canvas.add(cir);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
Key Differences
| Property Value | Rotation Behavior | Use Case |
|---|---|---|
centeredRotation: true (default) |
Rotates around object center | Standard rotation behavior |
centeredRotation: false |
Rotates around top-left corner | Custom rotation effects |
Conclusion
The centeredRotation property in FabricJS provides control over how objects rotate. Setting it to false changes the rotation point from the center to the top-left corner, enabling unique visual effects in your canvas applications.
