- 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 shrink and expand 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 shrink and expand animation, we can use the scaleX and scaleY properties 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
scaleX: This property accepts a Number value. The value that is assigned, determines the horizontal object scale factor. Its default value is 1.
scaleY: This property accepts a Number value. The value that is assigned, determines the vertical object scale factor. Its default value is 1.
Example 1: Adding shrink animation to the polygon
Let’s see a code example to see how we can add shrink animation by using animate method. We are passing the scaleX and scaleY properties a value of 0.5 which makes the polygon half of its original size from both horizontal and vertical directions.
<!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 shrink animation to the polygon</h2> <p>You can see that shrink 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: 90, left: 100, } ); // Adding it to the canvas canvas.add(polygon); // Using the animate method and passing scaleX property polygon.animate("scaleX", "0.5", { onChange: canvas.renderAll.bind(canvas), easing: fabric.util.ease.easeInCubic, duration: 1000, }); // Using the animate method and passing scaleY property polygon.animate("scaleY", "0.5", { onChange: canvas.renderAll.bind(canvas), easing: fabric.util.ease.easeInCubic, duration: 1000, }); </script> </body> </html>
Example 2: Adding Expand Animation to the Polygon
In this example, we will see how we can add expand animation by using the animate method. Since we are passing scaleX and scaleY properties a value of 1.5, the polygon object will be scaled by 1.5 times in both horizontal and vertical directions.
<!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 expand animation to the polygon</h2> <p>You can see that expand 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: 90, left: 100, } ); // Adding it to the canvas canvas.add(polygon); // Using the animate method and passing scaleX property polygon.animate("scaleX", "1.5", { onChange: canvas.renderAll.bind(canvas), easing: fabric.util.ease.easeInCubic, duration: 1000, }); // Using the animate method and passing scaleY property polygon.animate("scaleY", "1.5", { onChange: canvas.renderAll.bind(canvas), easing: fabric.util.ease.easeInCubic, duration: 1000, }); </script> </body> </html>
Conclusion
In this tutorial, we used two simple examples to demonstrate how you can add shrink and expand animations to a Polygon object using FabricJS