Converting a Polygon object into a data-like URL string 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 a data-like URL string we use the toDataURL method. This method converts an object into a data-like URL string.

Syntax

toDataURL(options: Object): String

Parameters

options (optional) − This parameter is an Object which provides additional customizations to the URL representation of the Polygon object. Using this parameter format, quality, multiplier and a lot of other properties can be changed.

Example 1: Default value without using toDataURL method

Let’s see a code example to see how the Polygon object looks like when the toDataURL method is not used. On using the toDataURL method, a URL representation of the Polygon object is returned. In this example, we have created a Polygon object and assigned it various properties like stroke, fill etc. However, since we have not used the toDataURL method, we will not be able to see the URL representation of the object in the console but rather the polygon object’s default value is logged.

<!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 toDataURL method</h2>
   <p>You can open console from dev tools and 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);
      
      // 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);
      
      // Console logging the Polygon object
      console.log("The Polygon object is as follows: ", polygon);
   </script>
</body>
</html>

Example 2: Using the toDataURL method

Let’s see a code example to see the logged output when using the toDataURL method. As soon as we open the console from the dev tools, we can see the URL representation of the Polygon object. We can copy that URL and paste it into the address bar of a new tab to see the final output

<!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 toDataURL method</h2>
   <p>You can open console from dev tools and see the output URL. You can copy that and paste it in the address bar of a new tab to see the final image.  </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 toDataURL method
      console.log(polygon.toDataURL());
   </script>
</body>
</html> 

Conclusion

In this tutorial, we used two simple examples to demonstrate how you can use FabricJS to convert a polygon object into a data-like URL string.

Updated on: 28-Dec-2022

117 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements