FabricJS – Setting the cropping offset of an HTMLCanvasElement of a Polygon object


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 left and top properties to set the cropping offset of the HTMLCanvasElement of a polygon object.

Syntax

toCanvasElement({ left: Number | top: Number }: Object):
HTMLCanvasElement

Parameters

  • options (optional) βˆ’ This parameter accepts an Object which provides additional customizations to our HTMLCanvasElement. Using this parameter height, left crop offset and a lot of other properties can be changed related to the HTMLCanvasElement of which left and top are properties.

Options Keys

  • left βˆ’ This property accepts a Number value which denotes the cropping left offset. This property is optional.

  • top βˆ’ This property accepts a Number value which denotes the cropping top offset. This property is optional.

Example 1: Using the left Property

Let’s see a code example to see the logged output when the toCanvasElement method is used along with the left property. Since we are passing it a value of 100, it will be used as the left crop offset. We have also used the toDataURL method in order to display how the left property would actually affect the polygon object. We can open the console from dev tools and open the url string in a new tab to see the output image.

<!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 left property</h2>
   <p>
      You can open console from dev tools, copy the url string and paste it in a new tab to see that the polygon object has been cropped by 100px offset from the left
   </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 with left key
      var leftCropOffset = polygon.toCanvasElement({
         left: 100,
      });
      
      // Using toDataURL method
      console.log(leftCropOffset.toDataURL());
   </script>
</body>
</html> 

Example 2: Using the top Property

Let’s see a code example to see the logged output when the toCanvasElement method is used along with the top property. Since we have passed a value of 100, it will be used as the top cropping offset for the polygon.

<!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 top property</h2>
   <p>
      You can open console from dev tools, copy the url string and paste it in a new tab to see that the polygon object has been cropped by 100px offset from the top
   </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 with top key
      var topCropOffset = polygon.toCanvasElement({
         top: 100, 
      });
      
      // Using toDataURL method
      console.log(topCropOffset.toDataURL());
   </script>
</body>
</html> 

Conclusion

In this tutorial, we used two simple examples to demonstrate how you can set the cropping offset of an HTMLCanvasElement of a polygon object using FabricJS.

Updated on: 02-Jan-2023

213 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements