- 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
Adding rotation animation to 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 add rotation animation, we can use the angle property in conjunction with animate method.
Syntax
animate(property: String | Object, value: Number | Object): fabric.Object | fabric.AnimationContext | Array.<fabric.AnimationContext>
Parameters
property − This property accepts a String or Object value which determines which properties we want to animate.
value − This property accepts a Number or Object value which determines the values to animate properties.
Options Keys
angle − This property accepts a Number that determines the angle of rotation of an object in degrees.
Example 1: Adding rotation animation to the polygon
Let’s see a code example to see how we can add rotation animation to a polygon by using animate method along with angle property. Since we have passed angle property a value of 60 degrees, the polygon will rotate by that angle. The animation will take place for 2 seconds since the duration is set to 2000.
<!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>Adding rotation animation to the polygon</h2> <p>You can see the rotation animation has been added to the Polygon</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 polygon object var polygon = new fabric.Polygon( [ { x: 60, y: 0 }, { x: 60, y: 60 }, { x: 0, y: 60 }, { x: 0, y: 0 }, ], { fill: "#ffe4e1", stroke: "green", strokeWidth: 5, top: 50, left: 100, } ); // Adding it to the canvas canvas.add(polygon); // Using the animate method polygon.animate("angle", "60", { onChange: canvas.renderAll.bind(canvas), duration: 2000, }); </script> </body> </html>
Example 2: Adding full rotation animation to the polygon
In this example, we will see how we can create a full rotation animation by using the animate method and angle property. A complete or full rotation is one in which an object turns around for 360 degrees. We can pass angle as 360 in order to create that animation. Here, we have added the easing as easeOutBounce which creates an exponentially decreasing parabolic bounce.
<!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>Adding full rotation animation to the polygon</h2> <p>You can see that the polygon completes a full 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 polygon object var polygon = new fabric.Polygon( [ { x: 60, y: 0 }, { x: 60, y: 60 }, { x: 0, y: 60 }, { x: 0, y: 0 }, ], { fill: "#ffe4e1", stroke: "green", strokeWidth: 5, top: 90, left: 100, } ); // Adding it to the canvas canvas.add(polygon); // Using the animate method polygon.animate("angle", "360", { onChange: canvas.renderAll.bind(canvas), easing: fabric.util.ease.easeOutBounce, duration: 5000, }); </script> </body> </html>
Conclusion
In this tutorial, we used two simple examples to demonstrate how you can add rotation animation to a Polygon object using FabricJS.