首頁  >  文章  >  web前端  >  如何使用 FabricJS 檢查 IText 物件是否已填入?

如何使用 FabricJS 檢查 IText 物件是否已填入?

王林
王林轉載
2023-09-12 18:13:021144瀏覽

如何使用 FabricJS 检查 IText 对象是否已填充?

在本教學中,我們將學習如何使用 FabricJS 檢查 IText 物件是否已填入。 IText 類別是在 FabricJS 版本 1.4 中引入的,它擴展了 Fabric.Text 並用於建立 IText 實例。 IText 實例使我們可以自由選擇、剪下、貼上或新增文本,而無需額外配置。還有各種支援的按鍵組合和滑鼠/觸控組合使文字具有互動性,而 Text 中未提供這些組合。

然而,基於 IText 的 Textbox 允許我們調整文字矩形的大小並自動換行。對於 IText 來說並非如此,因為高度不會根據換行進行調整。我們可以透過使用各種屬性來操作 IText 物件。同樣,我們可以使用 hasFill 方法檢查 IText 物件是否已填入。

文法

hasFill()

範例 1

使用透明填充時使用hasFill方法

讓我們看一個程式碼範例,以查看當 hasFill 方法與透明填充一起使用時記錄的輸出。如果物件具有填滿顏色,則 hasFill 方法傳回真值。在本例中,我們將填滿顏色設定為“透明”。因此,記錄的輸出將為 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 hasFill method when transparent fill is used</h2>
   <p>You can open console from dev tools and see that the logged output is false</p>
   <canvas id="canvas"></canvas>
   
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);

      // Initiate an itext object
      var itext = new fabric.IText(
         "Add sample text here.Lorem ipsum dolor sit amet",{
            width: 300,
            left: 60,
            top: 70,
            fill: "transparent",
            fontStyle: "bold",
            backgroundColor: "#f8f4ff",
            stroke: "black",
         }
      );

      // Add it to the canvas
      canvas.add(itext);

      // Using the hasFill method
      console.log("The value is: ", itext.hasFill());
   </script>
</body>
</html>

範例 2

使用 hasFill 方法

讓我們看一個程式碼範例,以查看使用 hasFill 方法時記錄的輸出。在這種情況下,由於 IText 物件具有填充顏色,因此將傳回真值。

<!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 hasFill method</h2>
   <p>You can open console from dev tools and see that the value is being displayed in the console</p>
   <canvas id="canvas"></canvas>
   
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);

      // Initiate an itext object
         var itext = new fabric.IText(
         "Add Sample Text HereLorem ipsum dolor sit amet",{
            width: 300,
            left: 60,
            top: 70,
            fill: "#b666d2",
            fontStyle: "bold",
            backgroundColor: "#f8f4ff",
            stroke: "black",
         }
      );

      // Using the hasFill method
      console.log("The value is: ", itext.hasFill());

      // Add it to the canvas
      canvas.add(itext);
   </script>
</body>
</html>

以上是如何使用 FabricJS 檢查 IText 物件是否已填入?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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