Home >Web Front-end >JS Tutorial >Add shrink and expand animations to Polygon objects using FabricJS
We can create a Polygon object by creating an instance of fabric.Polygon. A polygon object can be characterized as 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 such as angle, opacity, etc. To add shrink and expand animations we can use the scaleX and scaleY properties in conjunction with the animate method.
animate(property: String | Object, value: Number | Object): fabric.Object | fabric.AnimationContext | Array.<fabric.AnimationContext>
property - This property accepts a String or Object value to determine which properties we want to animate.
value - This property accepts a Number or Object value that determines the value property to animate.
scaleX: This property accepts a Number value. The assigned value determines the horizontal object scale factor. Its default value is 1.
scaleY: This property accepts a Number value. The assigned value determines the vertical object scale factor. Its default value is 1.
Let's look at a code example to see how to add a shrink animation using the animate method. We pass a value of 0.5 to the scaleX and scaleY properties, which makes the polygon half its original size both horizontally and vertically.
<!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>
In this example we will see how to add expand animation using the animate method. Since we passed a value of 1.5 to the scaleX and scaleY properties, the polygon object will be scaled 1.5 times horizontally and vertically.
<!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>
In this tutorial, we use two simple examples to demonstrate how to add shrink and expand animations to Polygon objects using FabricJS
The above is the detailed content of Add shrink and expand animations to Polygon objects using FabricJS. For more information, please follow other related articles on the PHP Chinese website!