首頁  >  文章  >  web前端  >  如何使用 FabricJS 來尋找影像的原始大小?

如何使用 FabricJS 來尋找影像的原始大小?

WBOY
WBOY轉載
2023-09-06 18:13:09764瀏覽

如何使用 FabricJS 查找图像的原始大小?

在本教程中,我們將學習如何使用 FabricJS 來尋找圖像的原始大小。我們可以透過建立fabric.Image的實例來建立一個Image物件。由於它是 FabricJS 的基本元素之一,我們也可以透過應用角度、不透明度等屬性輕鬆自訂它。為了找到圖像的原始大小,我們使用 getOriginalSize 方法。

文法

getOriginalSize(): Object 

使用getOriginalSize方法

範例

在這個範例中,我們使用了getOriginalSize方法來取得影像的寬度和高度值。這裡,傳回的寬度和高度值分別是 311 和 82。

<!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 getOriginalSize method</h2>
   <p>
      You can open the console from dev tools to see that the logged output contains the height and width of the 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 an Image object
      var image = new fabric.Image(imageElement, {
         top: 50,
         left: 110,
         skewX: 15,
      });
      
      // Add it to the canvas
      canvas.add(image);
      
      // Using the getOriginalSize method
      console.log(
         "The original size of the Image object is: ",
         image.getOriginalSize()
      );
   </script>
</body>
</html> 

使用 getOriginalSize 方法和 cropX 屬性

範例

讓我們來看看當 getOriginalSize 方法與 cropX 屬性結合使用時記錄的輸出的程式碼範例。在這裡,我們將值 50 傳遞給cropX。因此,我們的 Image 物件將在 x 方向上從原始影像大小裁剪 50px。然而,當我們使用getOriginalSize方法時,我們傳回的寬度和高度值分別為311和82,這進一步證明getOriginalSize只會傳回原始大小圖。

<!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 getOriginalSize method along with cropX property</h2>
   <p>
      You can open the console from dev tools to see that the original size of the image will be returned regardless of having applied image cropping in xdirection
   </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,
         skewX: 15,
         cropX: 50,
      });
      
      // Add it to the canvas
      canvas.add(image);
      
      // Using the getOriginalSize method
      console.log(
         "The original size of the Image object is: ",
         image.getOriginalSize()
      );
   </script>
</body>
</html> 

以上是如何使用 FabricJS 來尋找影像的原始大小?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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