FabricJS – How to set Polygon objects properties using function instead of constructor?


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

set(key: String, value: String | Boolean | Number | Object | Function)

Parameters

  • key − This parameter accepts an String which specifies the property we want to set.

  • value − This parameter accepts the value to be set for the property.

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. It can be seen that we have added the properties of "top" and "left" by using the constructor.

<!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: Using a Function to Set the Properties of Polygon Object

Let’s see a code example to see how the Polygon object looks like when a function is used to set the properties. Here we have initialized a function called addProperties which adds properties of "stroke", "left", "fill", "top" and "selectable" using the set method. As we call the function, these properties will be added to the 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>Using a function to set the properties of Polygon object</h2>
   <p>You can see that the properties have 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 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 },
      ]);
      
      // Initializing addProperties function
      function addProperties(obj) {
         obj.set("stroke", "red");
         obj.set("left", 100);
         obj.set("fill", "black");
         obj.set("top", 70);
         obj.set("selectable", true);
      }
      
      // Calling addProperties function 
      addProperties(polygon);
      
      // Adding it to the canvas
      canvas.add(polygon);
   </script>
</body>
</html>

Conclusion

In this tutorial, we used two simple examples to demonstrate how to set Polygon objects properties using a function instead of constructor using FabricJS.

Updated on: 02-Jan-2023

82 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements