Home > Web Front-end > JS Tutorial > body text

How to check if an image has cropping applied using FabricJS?

PHPz
Release: 2023-08-24 16:29:02
forward
1003 people have browsed it

如何使用 FabricJS 检查图像是否已应用裁剪?

In this tutorial we will show you how to check if an image has cropping applied Use 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 easily customize it as well Apply properties such as angle, opacity, etc. to find if the image has been cropped When applying, we use the hasCrop method. This method returns false if the crop is not Applied, or the applied crop value if applied.

grammar

hasCrop(): Boolean | Number
Copy after login

UsehasCropMethod

Example

In this example, we use the hasCrop method to find whether the Image object Applied crops. In this case our image object does not have any image cropping The logged output is false.

<!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 hasCrop method</h2>
   <p>
      You can open the console from dev tools to see that the logged output is false
   </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 an Image object
      var image = new fabric.Image(imageElement, {
         top: 50,
         left: 110,
      });
      
      // Add it to the canvas
      canvas.add(image);
      
      // Using the hasCrop method
      console.log("Is crop applied for the Image object?: ", image.hasCrop());
   </script>
</body>
</html>
Copy after login

Using the hasCrop method and the cropY attribute

Example

Let's look at a code example of the output logged when using the hasCrop method with the cropY attribute. We have passed the value 2 to the cropY property which will Make sure the image object has a 2px image crop in the Y direction. in this case, The clipping value is returned in the console.

<!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 hasCrop method along with cropY property</h2>
   <p>
      You can open the console from dev tools to see that the logged output is 2
   </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 an Image object
      var image = new fabric.Image(imageElement, {
         top: 50,
         left: 110,
         cropY: 2,
      });
      
      // Add it to the canvas
      canvas.add(image);
      
      // Using the hasCrop method
      console.log(
         "Crop value applied for the Image object is: ",
         image.hasCrop()
      );
   </script>
</body>
</html>
Copy after login

in conclusion

In this tutorial, we use two examples to demonstrate how to check whether an image Cropping was applied using FabricJS.

The above is the detailed content of How to check if an image has cropping applied using FabricJS?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template