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 find the rotation matrix of a Polygon object using FabricJS?
We can create a Polygon object by creating an instance of fabric.Polygon. A polygon object can be characterized by any closed shape consisting of a set of connected straight line segments. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity etc.
In order to find the rotation matrix, we use the _calcRotateMatrix() method. This method returns an Array with given values [cosA, sinA, -sinA, cosA, 0, 0] where A is the angle of rotation in degrees.
Syntax
_calcRotateMatrix(): Array
Return Value
Returns a 6-element array representing a 2D transformation matrix in the format [cosA, sinA, -sinA, cosA, 0, 0], where A is the rotation angle in degrees.
Example 1: Using the _calcRotateMatrix Method
Let's see a code example of how we can find the rotation matrix of a polygon by using the _calcRotateMatrix method. You can open the console from dev tools to see that the array consists of only 0's and 1's because it is the sin or cosine values of the angle.
<!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>Using the _calcRotateMatrix method</h2>
<p>
You can open console from dev tools and see that the logged output contains the rotation matrix of the polygon instance
</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiating a polygon object
var polygon = new fabric.Polygon(
[
{ x: -20, y: -35 },
{ x: 20, y: -35 },
{ x: 40, y: 0 },
{ x: 20, y: 35 },
{ x: -20, y: 35 },
{ x: -40, y: 0 },
],
{
top: 60,
left: 140,
fill: "red",
}
);
// Adding it to the canvas
canvas.add(polygon);
// Using _calcRotateMatrix method
console.log(
"The rotation matrix of the polygon instance is: ", polygon._calcRotateMatrix()
);
</script>
</body>
</html>
The rotation matrix of the polygon instance is: [1, 0, -0, 1, 0, 0]
Example 2: Using the _calcRotateMatrix Method along with Rotate Method
Let's see a code example to understand how the values of the returned array are affected when we apply transformations to our polygon object. In this case, we have used the rotate method which allows us to set the angle of an instance. Thus, our polygon object has been rotated by an angle of 60 degrees which affects the values of the rotation matrix as cosine and sine value of angle 60.
<!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>Using the _calcRotateMatrix method along with rotate method</h2>
<p>
You can open console from dev tools and see that the logged output contains the rotation matrix of the polygon instance
</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiating a polygon object
var polygon = new fabric.Polygon(
[
{ x: -20, y: -35 },
{ x: 20, y: -35 },
{ x: 40, y: 0 },
{ x: 20, y: 35 },
{ x: -20, y: 35 },
{ x: -40, y: 0 },
],
{
top: 60,
left: 140,
fill: "red",
}
);
// Adding it to the canvas
canvas.add(polygon);
// Using rotate method
polygon.rotate(60);
// Using _calcRotateMatrix method
console.log(
"The rotation matrix of the polygon instance is: ", polygon._calcRotateMatrix()
);
</script>
</body>
</html>
The rotation matrix of the polygon instance is: [0.5, 0.8660254037844387, -0.8660254037844387, 0.5, 0, 0]
Key Points
- The
_calcRotateMatrix()method returns a 6-element transformation matrix - For no rotation (0°), the matrix is [1, 0, 0, 1, 0, 0]
- The first four values represent cosine and sine values of the rotation angle
- The last two values (0, 0) represent translation, which is always zero for rotation-only matrices
Conclusion
The _calcRotateMatrix() method provides access to the internal rotation transformation matrix of FabricJS polygon objects. This matrix is essential for understanding how rotations are applied mathematically in 2D graphics transformations.
