How to create a canvas with 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.

Syntax

new fabric.Polygon(points: Array, options: 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.

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

Let’s see a code example of how we can add a polygon object to our canvas. The only required parameter is the points Array whereas the second argument is the optional options object.

<!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 the polygon object has been added</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: 30, y: 50 },
         { x: 0, y: 0 },
         { x: 60, y: 0 },
      ];
      
      // Initiating a polygon object
      var polygon = new fabric.Polygon(points, {
         left: 100,
         top: 40,
         fill: "#1e90ff",
         strokeWidth: 4,
         stroke: "green",
         flipY: true,
         scaleX: 2,
         scaleY: 2,
      });
      
      // Adding it to the canvas
      canvas.add(polygon);
   </script>
</body>
</html> 

Example 2: Manipulating the Polygon Object by using the Set Method

In this example, we have assigned the properties to the Polygon object by using the set method which is a setter for values. Any property related to stroke, strokeWidth, angle, scaling, rotation, etc, can be mutated by using this method.

<!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>Manipulating the Polygon object by using the set method</h2>
   <p>You can see the Polygon object in canvas now with set values</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: 30, y: 50 },
         { x: 0, y: 0 },
         { x: 60, y: 0 },
      ];
      
      // Initiating a polygon object
      var polygon = new fabric.Polygon(points);
      
      // Set the properties
      polygon.set("stroke", "blue");
      polygon.set("strokeWidth", 10);
      polygon.set("flipY", true);
      polygon.set("top", 50);
      polygon.set("left", 100);
      polygon.set("scaleX", 2);
      polygon.set("scaleY", 2);
      
      // 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 create a canvas with Polygon using FabricJS.

Updated on: 02-Jan-2023

672 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements