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>
Object representation of the Polygon instance is: 
{
  type: "polygon",
  version: "5.1.0",
  originX: "left",
  originY: "top",
  left: 100,
  top: 50,
  width: 80,
  height: 70,
  fill: "black",
  stroke: "red",
  strokeWidth: 2,
  strokeLineJoin: "bevil",
  points: [
    { x: -20, y: -35 },
    { x: 20, y: -35 },
    { x: 40, y: 0 },
    { x: 20, y: 35 },
    { x: -20, y: 35 },
    { x: -40, y: 0 }
  ]
}

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>
Object representation of the Polygon instance is: 
{
  type: "polygon",
  version: "5.1.0",
  originX: "left",
  originY: "top",
  left: 100,
  top: 50,
  width: 80,
  height: 70,
  fill: "black",
  stroke: "red",
  strokeWidth: 2,
  strokeLineJoin: "bevil",
  PropertyName: "property",
  points: [
    { x: -20, y: -35 },
    { x: 20, y: -35 },
    { x: 40, y: 0 },
    { x: 20, y: 35 },
    { x: -20, y: 35 },
    { x: -40, y: 0 }
  ]
}

Key Points

  • The toObject() method converts a FabricJS Polygon object into a plain JavaScript object
  • The serialized object includes all default properties like coordinates, styling, and positioning
  • Custom properties can be included by passing their names in an array to the method
  • This serialized format is useful for saving polygon data or transferring it over a network

Conclusion

The toObject() method provides an easy way to serialize Polygon objects in FabricJS. Use it to convert polygon instances into transferable object representations that preserve all styling and positioning data.

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

324 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements