Converting a polygon object into an HTMLCanvasElement 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.

In order to convert a polygon object into HTMLCanvasElement we use the toCanvasElement method. It returns the DOM element of type HTMLCanvasElement, an interface which inherits its properties and methods from the HTMLElement interface.

Syntax

toCanvasElement( options: Object ): HTMLCanvasElement

Parameters

options (optional) ? This parameter accepts an Object which provides additional customizations to our HTMLCanvasElement. Using this parameter width, height and a lot of other properties can be changed related to the HTMLCanvasElement.

Example 1: Default Polygon Object Without Conversion

Let's see a code example to see how the Polygon object looks like when the toCanvasElement method is not used. Here, we have console logged the polygon object and hence we will only be able to see the default value of the polygon object in the console.

<!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 value without using toCanvasElement method</h2>
   <p>You can open console from dev tools to see that the logged output contains the default value of the polygon object</p>
   <canvas id="canvas"></canvas>
   <script>
      
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiate a polygon object
      var polygon = new fabric.Polygon(
         [
            { x: 600, y: 310 },
            { x: 650, y: 450 },
            { x: 600, y: 480 },
            { x: 550, y: 480 },
            { x: 450, y: 460 },
            { x: 300, y: 210 },
         ],
         {
            fill: "#778899",
            stroke: "blue",
            strokeWidth: 5,
            top: 50,
            left: 100,
            scaleX: 0.5,
            scaleY: 0.5,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Console logging the polygon object
      console.log("The polygon object is as follows:", polygon);
   </script>
</body>
</html>
The polygon object is as follows: fabric.Polygon {type: "polygon", version: "5.3.0", ...}

Example 2: Using the toCanvasElement Method

Let's see a code example to see the logged output when the toCanvasElement method is used. You can open the console from dev tools to see that the DOM element of type HTMLCanvasElement is being 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 toCanvasElement method</h2>
   <p>You can open console from dev tools to see the logged output</p>
   <canvas id="canvas"></canvas>
   <script>
      
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiate a polygon object
      var polygon = new fabric.Polygon(
         [
            { x: 600, y: 310 },
            { x: 650, y: 450 },
            { x: 600, y: 480 },
            { x: 550, y: 480 },
            { x: 450, y: 460 },
            { x: 300, y: 210 },
         ],
         {
            fill: "#778899",
            stroke: "blue",
            strokeWidth: 5,
            top: 50,
            left: 100,
            scaleX: 0.5,
            scaleY: 0.5,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using toCanvasElement method
      console.log(
         "The output on using toCanvasElement method is:",
         polygon.toCanvasElement()
      );
   </script>
</body>
</html>
The output on using toCanvasElement method is: <canvas width="200" height="200"></canvas>

Key Points

  • The toCanvasElement() method converts FabricJS polygon objects to native HTMLCanvasElement objects
  • The returned HTMLCanvasElement can be manipulated using standard Canvas API methods
  • Optional parameters allow customization of the resulting canvas dimensions and properties
  • This method is useful for exporting FabricJS objects for use in other canvas contexts

Conclusion

The toCanvasElement() method provides a straightforward way to convert FabricJS polygon objects into standard HTMLCanvasElement objects. This conversion enables integration with native Canvas API functionality and facilitates object export workflows.

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

252 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements