How to add the coordinates in a Polygon 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. We can add the coordinates in a polygon by using points property.

Syntax

new fabric.Polygon( points: Array, { points: Array }: Object )

Parameters

  • points − This parameter accepts an Array which denotes the array of points that make up the polygon object.

  • 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 of which points is a property.

Options Keys

  • points − This property accepts an Array which allows us to set the points array.

Example 1: Default Appearance of Polygon Object

Let’s see a code example of how we can add a polygon object to our canvas. In this case we have not used the points property. We have specified the points array as the first argument itself which denotes the coordinate values to be used.

<!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>Default appearance of Polygon object</h2>
   <p>You can see the 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: Using the Points Property

In this example, we have used the points property and assigned it an Array that consists of the coordinate values of the polygon where each point is an object with "x" and "y" values. As it can be seen, although we have already specified the points of the Polygon object, as soon as we use the points property, it overrides those values and the new coordinates are used.

<!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>Using the points property</h2>
   <p>You can see the Polygon object's coordinates have changed</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 points array
      var points = [
         { x: 0, y: 30 },
         { x: 30, y: 30 },
         { x: 30, y: 0 },
         { x: 0, y: 0 },
      ];
      
      // Initiating a polygon object
      var polygon = new fabric.Polygon(points, {
         top: 50,
         left: 50,
         points: [
            { x: 0, y: 70 },
            { x: 70, y: 70 },
            { x: 70, y: 0 },
            { x: 0, y: 0 },
         ],
      });
      
      // Adding it to the canvas
      canvas.add(polygon);
   </script>
</body>
</html>

Conclusion

In this tutorial, we used two simple examples to demonstrate how you can add the coordinates in a Polygon using FabricJS.

Updated on: 02-Jan-2023

417 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements