首頁  >  文章  >  web前端  >  如何使用 FabricJS 檢查影像是否已套用裁剪?

如何使用 FabricJS 檢查影像是否已套用裁剪?

PHPz
PHPz轉載
2023-08-24 16:29:02978瀏覽

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

在本教程中,我們將向您展示如何檢查圖像是否已套用裁剪 使用 FabricJS。我們可以透過建立fabric.Image的實例來建立一個Image物件。 由於它是FabricJS的基本元素之一,我們也可以輕鬆地自訂它 應用角度、不透明度等屬性,以查找影像是否有裁剪 應用時,我們使用 hasCrop 方法。如果作物不是,此方法傳回 false 已套用,或套用的裁切值(如果已套用)。

文法

hasCrop(): Boolean | Number

使用hasCrop方法

範例

在這個例子中,我們使用了hasCrop方法來尋找Image物件是否 已施用作物。在這種情況下,我們的圖像物件沒有任何圖像裁剪 記錄的輸出為 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>

使用 hasCrop 方法和 cropY 屬性

範例

讓我們來看看使用 hasCrop 方法時記錄的輸出的程式碼範例 與 cropY 屬性。我們已將值 2 傳遞給 cropY 屬性,這將 確保影像物件在 Y 方向上有 2px 影像裁切。在這種情況下, 裁剪值在控制台中傳回。

<!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>

結論

在本教程中,我們使用兩個範例來示範如何檢查圖像是否 使用 FabricJS 應用了裁剪。

以上是如何使用 FabricJS 檢查影像是否已套用裁剪?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除