How to add blur-in and blur-out animation to a Polyline using FabricJS?


We can create a Polyline object by creating an instance of fabric.Polyline. A polyline object can be characterised by 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.

Since blur is not available in FabricJS itself, we will use CSS to add blur-in and blur-out animation to our Polyline.

Syntax

filter: blur(Xpx)

Here, "X" is the property that accepts a Number value which determines the amount of blur to apply.

Example 1: Adding blur-in animation to a polyline

Let’s see a code example to see how we can add blur-in animation by using the filter property. In order to create a blur-in effect, we need to create an animation which starts from blur value as 0px and goes to blur value of 5px, we have added a duration of 5 seconds as you can see in the code example below.

<!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 blur-in animation to the polyline</h2>
   <p>You can see the blur-in effect has been added to the polyline</p>
   <canvas id="canvas"></canvas>
   <style>
      @-webkit-keyframes blur {
         0% { filter: blur(0px);}
         100% { filter: blur(5px);}
      } 
      #canvas {
         animation: blur 5s;
      }
   </style>
   <script>
      
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiate a polyline object
      var polyline = new fabric.Polyline(
         [
            { x: 50, y: 0 },
            { x: 25, y: 43.30},
            { x: -25, y: 43.301 },
            { x: -50, y: 0},
            { x: -25, y: -43.301},
            { x: 25, y: -43.301 },
            { x: 50, y: 0 },

         ],
         {
            fill: "skyblue",
            stroke: "orange",
            strokeWidth: 5,
            top: 50,
            left: 300,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polyline);
   </script>
</body>
</html> 

Example 2: Adding blur-out animation to the polyline

Let’s see a code example to see how we can add blur-out animation by using the filter property. In order to create a blur-out effect, we need to create an animation which starts from blur value as 5px and goes to blur value of 0px, we have added a duration of 5 seconds as you can see in the code example below.

<!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 blur-out animation to the polyline</h2>
   <p>You can see the blur-out effect has been added to the polyline</p>
   <canvas id="canvas"></canvas>
   <style>
      @-webkit-keyframes blur {
         0% { filter: blur(5px);}
         100% { filter: blur(0px);}
      }
      #canvas {
         animation: blur 5s;
      }
   </style>
   <script>
      
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiate a polyline object
      var polyline = new fabric.Polyline(
         [
            { x: 50, y: 0 },
            { x: 25, y: 43.30},
            { x: -25, y: 43.301 },
            { x: -50, y: 0},
            { x: -25, y: -43.301},
            { x: 25, y: -43.301 },
            { x: 50, y: 0 },

         ],
         {
            fill: "skyblue",
            stroke: "orange",
            strokeWidth: 5,
            top: 50,
            left: 300,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polyline);
   </script>
</body>
</html> 

Updated on: 16-Feb-2023

210 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements