FabricJS - Finding the dimensions of a Polygon object converted to an HTMLCanvasElement?


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. We use the width and height properties of HTMLCanvasElement that it inherits from its parent, HTMLElement to find the dimensions of a Polygon object converted to HTMLCanvasElement.

Syntax

HTMLCanvasElement.height
HTMLCanvasElement.width

Example 1: Using the toCanvasElement Method and Using the Width Property

Let’s see a code example to see how the Polygon object looks like when the toCanvasElement method is used along with the width property. The width is a positive integer that denotes the number of pixels going across one row of the canvas. We can open the console from dev tools to see that the width value is 200.

<!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 and using the width property</h2>
   <p> 
      You can open console from dev tools to see that the width value is being displayed as 200
   </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
      var polygonCanvas = polygon.toCanvasElement({
         width: 200,
      });
      
      // Using the width property
      console.log("The width is as follows:", polygonCanvas.width);
   </script>
</body>
</html> 

Example 2: Using the toCanvasElement Method and Using the Height Property

Let’s see a code example to see the logged output when the toCanvasElement method is used along with the height property. The height is a positive integer that denotes the number of pixels going down one column of the canvas. In this case, we can open the console from dev tools to see that the height value is 200.

<!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 and using the height property</h2>
   <p>
      You can open console from dev tools to see that the  height value is being displayed as 200
   </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
      var polygonCanvas = polygon.toCanvasElement({
         height: 200,
      });
      
      // Using the height property
      console.log("The height is as follows:", polygonCanvas.height);
   </script>
</body>
</html> 

Conclusion

In this tutorial, we used two simple examples to demonstrate how you can find the dimensions of a Polygon object converted to HTMLCanvasElement using FabricJS.

Updated on: 29-Dec-2022

143 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements