How to serialize a Polygon object 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.

Serialization is the process of converting an object into a format which is suitable for transfer over a network, which in this case is the object representation. In order to create an Object representation of a Polygon object, we use the toObject method. This method returns the object representation of an instance.

Syntax

toObject(propertiesToInclude: Array): Object

Parameters

propertiesToInclude − This parameter accepts an Array which contains any properties we might want to additionally include in the output. This parameter is optional.

Example 1: Using the toObject Method

Let’s see a code example to see the logged output when the toObject method is used. In this case, an Object representation of the Polygon instance will be returned.

<!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 toObject method</h2>
   <p>
      You can open console from dev tools and see that the logged output contains the Object representation of the polygon instance
   </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 },
         ],
         {
            stroke: "red",
            left: 100,
            top: 50,
            fill: "black",
            strokeWidth: 2,
            strokeLineJoin: "bevil",
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using the toObject method
      console.log(
         "Object representation of the Polygon instance is: ",
         polygon.toObject()
      );
   </script>
</body>
</html> 

Example 2: Using toObject Method to Add Additional Properties

Let’s see a code example to see how we can include additional properties by using the toObject method. In this case, we have added a custom property called “PropertyName”. We can pass the specific property to the fabric.Polygon instance as second argument in options object and pass the same key to the toObject 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>Using toObject method to add additional properties</h2>
   <p>
      You can open console from dev tools and see that the logged output contains added property called PropertyName
   </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 with PropertyName key
      
      // and passed in options 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 },
         ],
         {
            stroke: "red",
            left: 100,
            top: 50,
            fill: "black",
            strokeWidth: 2,
            strokeLineJoin: "bevil",
            PropertyName: "property",
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using the toObject method
      console.log(
         "Object representation of the Polygon instance is: ",
         polygon.toObject(["PropertyName"])
      );
   </script>
</body>
</html> 

Conclusion

In this tutorial, we used two simple examples to demonstrate how you can serialize a Polygon object using FabricJS.

Updated on: 02-Jan-2023

164 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements