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>

Key Differences Between Polygon and Polyline

Aspect fabric.Polygon fabric.Polyline
Shape Automatically closes the shape Creates an open path by default
Fill Can be filled by default Typically used for strokes only
Use Case Best for closed shapes Best for lines and open paths

Conclusion

In this tutorial, we demonstrated how to create polygons using both fabric.Polygon and fabric.Polyline in FabricJS. While both can create polygon shapes, fabric.Polygon is preferred for closed shapes as it automatically connects the last point to the first.

Updated on: 2026-03-15T23:19:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements