Adding fade-in and fade-out 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 fade-in and fade-out animation, we can use the opacity 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

  • opacity − This property accepts a Number that allows us to control the opacity of an object. Default value of opacity property is 1.

Example 1: Adding fade-in animation to a polygon

Let’s see a code example to see how we can add fade-in animation by using the animate method along with opacity property. In order to create a fade-in effect, we need to set opacity from 0(transparent) to 1(opaque). We have also added the easing option and passed it the value of easeInCubic which makes the animation start slowly but end quickly.

<!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 fade-in animation to the polygon</h2>
   <p>You can see the fade-in effect 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: 600, y: 310 },
            { x: 650, y: 450 },
            { x: 600, y: 480 },
            { x: 550, y: 480 },
            { x: 450, y: 460 },
            { x: 300, y: 210 },
         ],
         {
            fill: "#778899",
            stroke: "blue",
            strokeWidth: 5,
            top: 50,
            left: 100,
            scaleX: 0.5,
            scaleY: 0.5,
            opacity: 0, 
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using the animate method
      polygon.animate("opacity", "1", {
         onChange: canvas.renderAll.bind(canvas),
         easing: fabric.util.ease.easeInCubic,
         duration: 5000,
      });
   </script>
 </body>
</html> 

Example 2: Adding fade-out animation to a polygon

In this example, we will see how we can create the fade-out animation by using the animate method along with opacity property. In order to create the fade-out effect, we need to set opacity from 1 (opaque) to 0 (transparent).

Since we have passed the duration property a value of 5000, this animation will last for 5 seconds. We have also added the easing option and passed it the value of easeOutCubic which makes the animation start quickly but end slowly.

<!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 fade-out animation to the polygon</h2>
   <p>You can see the fade-out effect 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: 600, y: 310 },
            { x: 650, y: 450 },
            { x: 600, y: 480 },
            { x: 550, y: 480 },
            { x: 450, y: 460 },
            { x: 300, y: 210 },
         ],
         {
            fill: "#778899",
            stroke: "blue",
            strokeWidth: 5,
            top: 50,
            left: 100,
            scaleX: 0.5,
            scaleY: 0.5,
            opacity: 1,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using the animate method
      polygon.animate("opacity", "0", {
         onChange: canvas.renderAll.bind(canvas),
         easing: fabric.util.ease.easeOutCubic,
         duration: 5000, 
      });
   </script>
 </body>
</html>

Conclusion

In this tutorial, we used two simple examples to demonstrate how you can add fade-in and fade-out animation to a Polygon object using FabricJS.

Updated on: 28-Dec-2022

301 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements