How to crop the height in a cloned image using FabricJS?

In this tutorial, we are going to learn how to crop the height in a cloned image using FabricJS. We can create an Image object by creating an instance of fabric.Image. 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 crop the height in a cloned image, we use the height property.

Syntax

cloneAsImage(callback: function, {height: Number}: Object): fabric.Object

Parameters

  • callback (optional) ? This parameter is a function which is to be invoked with a cloned image instance as the first argument.

  • options (optional) ? This parameter is an optional Object which provides additional customizations to our clone image. Using this parameter we can set a multiplier, crop the clone image, remove the current object transform or a lot of other properties can be changed of which height is a property.

Options Keys

  • height ? This property accepts a Number value which denotes the cropping height. This property is optional.

Without using the height property

Let's see a code example of how the cloned Image object appears when the height property is not used. In this case, the cloned image will not be cropped.

<!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>Without using the height property</h2>
   <p>You can see that no cropping has been applied to the clone image</p>
   <canvas id="canvas"></canvas>
   <img src="https://www.tutorialspoint.com/images/logo.png" id="img1" style="display: none" />
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiating the image element
      var imageElement = document.getElementById("img1");
      
      // Initiate a shadow object
      var shadow = new fabric.Shadow({
         color: "#308080",
         blur: 3,
      });
      
      // Initiate an Image object
      var image = new fabric.Image(imageElement, {
         top: 50,
         left: 110,
         skewX: 20,
         shadow: shadow,
      });
      
      // Add original image to canvas
      canvas.add(image);
      
      // Using cloneAsImage method
      image.cloneAsImage(function(Img) {
         Img.set("top", 150);
         Img.set("left", 150);
         canvas.add(Img);
      });
   </script>
</body>
</html>

Using the height property

In this example, we have used the height property and passed it a value 50 which is the cropping height. Therefore, the height will be cropped.

<!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 height property</h2>
   <p>You can see that cropping has been applied to the clone image</p>
   <canvas id="canvas"></canvas>
   <img src="https://www.tutorialspoint.com/images/logo.png" id="img1" style="display: none" />
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
     
      // Initiating the image element
      var imageElement = document.getElementById("img1");
     
      // Initiate a shadow object
      var shadow = new fabric.Shadow({
         color: "#308080",
         blur: 3,
      });
     
      // Initiate an Image object
      var image = new fabric.Image(imageElement, {
         top: 50,
         left: 110,
         skewX: 20,
         shadow: shadow,
      });
      
      // Add original image to canvas
      canvas.add(image);
     
      // Using cloneAsImage method with height property
      image.cloneAsImage(
         function(Img) {
            Img.set("top", 150);
            Img.set("left", 150);
            canvas.add(Img);
         }, {
            height: 50,
         }
      );
   </script>
</body>
</html>

Key Points

  • The cloneAsImage() method creates a copy of the image object as a new image instance

  • The height property in the options parameter crops the cloned image to the specified height

  • Without the height property, the cloned image retains its original dimensions

  • The cropping is applied only to the cloned image, not the original

Conclusion

In this tutorial, we used two examples to demonstrate how you can crop the height in a clone image using FabricJS. The height property in the options parameter allows you to control the vertical dimensions of the cloned image effectively.

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

553 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements