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 Triangle using FabricJS?
In this tutorial, we are going to learn how to disable the centered rotation of Triangle using FabricJS. Triangle is one of the various shapes provided by FabricJS. In order to create a triangle, we will have to create an instance of fabric.Triangle 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.Triangle({ centeredRotation: Boolean }: Object)
Parameters
-
Options (optional) ? This parameter is an Object which provides additional customizations to our triangle. 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 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 of Triangle Rotation
Let's see a code example that depicts the default behaviour of a triangle object. Since centeredRotation property is set to true by default, the triangle 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 Triangle in FabricJS</h2>
<p>Rotate the triangle to see the default behaviour 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 triangle object
var triangle = new fabric.Triangle({
left: 105,
top: 60,
width: 100,
height: 70,
fill: "#deb887",
});
// Add it to the canvas
canvas.add(triangle);
</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 triangle will rotate around one of its corners 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 as key with the value "false"</h2>
<p>Rotate the triangle and notice that now its center of rotation has changed. The triangle rotates around one of its corners instead of its center.</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 triangle object
var triangle = new fabric.Triangle({
left: 105,
top: 60,
width: 100,
height: 70,
fill: "#deb887",
centeredRotation: false,
});
// Add it to the canvas
canvas.add(triangle);
</script>
</body>
</html>
Key Points
- The
centeredRotationproperty istrueby default - Setting it to
falsechanges the rotation origin from center to corner - This affects only the visual rotation behavior during user interaction
- The property works with all FabricJS objects, not just triangles
Conclusion
The centeredRotation property in FabricJS provides control over rotation behavior. Setting it to false disables center-based rotation, making objects rotate around their corner instead of center.
