How to create a Polygon with Polyline 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. Since Polygon extends fabric.Polyline, we can create a polygon instance by using polyline as well.

Syntax

new fabric.Polyline( points: Array, options: Object )

Parameters

  • points − This parameter accepts an Array which denotes the array of points that make up the polygon object where each point is an object with x and y.

  • options (optional) − This parameter is an Object which provides additional customizations to our object. Using this parameter origin, stroke width and a lot of other properties can be changed related to the Polygon object.

Example 1: Creating an Instance of fabric.Polygon() and Adding it to our Canvas

Let’s see a code example of how we can create a polygon by creating an instance of fabric.Polygon. Since the Polygon class extends fabric.Polyline, it inherits its properties and methods thereby establishing a relationship between them.

<!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>
      Creating an instance of fabric.Polygon() and adding it to our canvas
   </h2> 
   <p>You can see that a Polygon object has been added to the canvas</p>
   <canvas id="canvas"></canvas>
   <script>
      
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiating a polygon object
      var polygon = new fabric.Polygon(
         [
            { x: -20, y: -35 },
            { x: 20, y: -35 },
            { x: 40, y: 0 },
            { x: 20, y: 35 },
            { x: -20, y: 35 },
            { x: -40, y: 0 },
         ],
         {
            top: 50,
            left: 50,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
   </script>
</body>
</html> 

Example 2: Creating an Instance of fabric.Polyline() and Adding it to our Canvas

Let’s see a code example to see how we can create a polygon by creating an instance of fabric.Polyline. We need to specify an Array containing the x and y coordinates of the polygon that we want to create and add any optional properties that we want to include in the options object. In this case, we have created a square which is a polygon having four equal sides and four equal angles.

<!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>
      Creating an instance of fabric.Polyline() and adding it to our canvas
   </h2>
   <p>You can see that a Polygon object has been added to the canvas</p>
   <canvas id="canvas"></canvas>
   <script>
      
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiating a polyline object
      var polygon = new fabric.Polyline(
         [
            { x: 0, y: 0 },
            { x: 50, y: 0 },
            { x: 50, y: 50 },
            { x: 0, y: 50 },
         ],
         {
            top: 50,
            left: 50,
            fill: "green",
         }
      );
      
      // Adding it to the canvas 
      canvas.add(polygon);
   </script>
</body>
</html>

Conclusion

In this tutorial, we used two simple examples to demonstrated how you can create a Polygon with Polyline using FabricJS.

Updated on: 02-Jan-2023

527 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements