- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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>
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 which inturn 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>
Conclusion
In this tutorial, we used two simple examples to demonstrate how to find the rotation matrix of a Polygon object using FabricJS.