FabricJS – Removing the shadows of a Polygon 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 withoutShadow property to remove shadows of the Polygon converted to HTMLCanvasElement.

Syntax

toCanvasElement({ withoutShadow: Boolean }: 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 withoutShadow is a property.

Options Keys

  • withoutShadow − This property accepts a Boolean value which determines whether the current object shadow is to be removed or not. This property is optional.

Example 1: Using the withoutShadow Property and Passing it a ‘false’ Value

Let’s see a code example to see the logged output when the toCanvasElement method is used along with withoutShadow property. On passing it a ‘false’ value, the withoutShadow property retains any object shadow that is present. Therefore, in this case shadow will be present in its output image. We have used the toDataURL method in sync with toCanvasElement method to show how the output image actually looks.

<!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 withoutShadow property and passing it a ‘false’ value</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's image contains shadow
   </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 shadow object
      var shadow = new fabric.Shadow({
         color: "black",
         blur: 12,
      });
      
      // 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,
            shadow: shadow,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using toCanvasElement method
      var withoutShadow = polygon.toCanvasElement({
         withoutShadow: false,
      });
      
      // Using toDataURL method
      console.log(withoutShadow.toDataURL());
   </script>
</body>
</html> 

Example 2: Using the withoutShadow Property and Passing it a ‘true’ Value

Let’s see a code example to see the logged output when the toCanvasElement method is used along with withoutShadow property. On passing it a ‘true’ value, the withoutShadow property will get rid of shadow. Therefore, in this case, shadow will be removed in its 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 withoutShadow property and passing it a ‘true’ value</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's image does not contain shadow
   </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 shadow object
      var shadow = new fabric.Shadow({
         color: "black",
         blur: 12,
      });
      
      // 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,
            shadow: shadow,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using toCanvasElement method
      var withoutShadow = polygon.toCanvasElement({
         withoutShadow: true,
      });
      
      // Using toDataURL method
      console.log(withoutShadow.toDataURL());
   </script>
</body>
</html> 

Conclusion

In this tutorial, we used two simple examples to demonstrate how you can remove the shadows of a Polygon converted to an HTMLCanvasElement using FabricJS.

Updated on: 02-Jan-2023

99 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements