首頁 > web前端 > js教程 > 主體

如何使用 FabricJS 建立圖像物件的字串表示形式?

WBOY
發布: 2023-08-23 18:21:05
轉載
748 人瀏覽過

如何使用 FabricJS 创建图像对象的字符串表示形式?

在本教程中,我們將向您展示如何建立字串表示形式 使用 FabricJS 的圖像物件。我們可以透過建立一個實例來建立一個 Image 對象 織物.圖片。由於它是FabricJS的基本元素之一,我們也可以輕鬆地 透過應用角度、不透明度等屬性來自訂它。為了創建一個字串 Image 物件的表示,我們使用 toString 方法。

文法

toString(): String
登入後複製

使用toString方法

範例

讓我們來看一個程式碼範例,看看使用 toString 方法時記錄的輸出。在 在這種情況下,將傳回圖像實例的字串表示形式。

<!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 toString method</h2>
   <p>
      You can open console from dev tools and see that the logged output contains the String representation of the image instance
   </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 toString method
      console.log(
         "String representation of the Image instance is: ",
         image.toString()
      );
   </script>
</body>
</html>
登入後複製

使用toString方法比較兩個不同的元素

範例

讓我們看一個程式碼範例,看看如何透過查看兩個物件來比較它們 各自的字串表示。在這裡,我們初始化了一個 Image 實例和一個 矩形實例。在對每個物件應用 toString 方法時,我們可以看到它們的 控制台中各自的字串表示形式。

<!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 toString method to compare two different elements</h2>
   <p>
      You can open console from dev tools and see that the logged output contains the String representation of the Image instance and the rectangle instance
   </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,
      });
      
      // Initiate a Rectangle object
      var rect = new fabric.Rect({
         stroke: "red",
         strokeWidth: 20,
         width: 20,
         height: 50,
         left: 460,
         top: 55,
      });
      
      // Add them to the canvas
      canvas.add(image);
      canvas.add(rect);
      
      // Using the toString method
      console.log(
         "String representation of the Image instance is: ",
         image.toString()
      );
      console.log(
         "String representation of the Rectangle instance is: ",
         rect.toString()
      );
   </script>
</body>
</html>
登入後複製

結論

在本教程中,我們使用兩個範例來示範如何建立字串 使用 FabricJS 表示 Image 物件。

以上是如何使用 FabricJS 建立圖像物件的字串表示形式?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!