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

如何使用 FabricJS 檢查 IText 物件是否具有特定的樣式屬性?

王林
發布: 2023-08-24 23:09:10
轉載
1396 人瀏覽過

如何使用 FabricJS 检查 IText 对象是否具有特定的样式属性?

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

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

文法

styleHas(property: String, lineIndex: Number): Boolean
登入後複製

參數

  • property - 此參數接受一個String,表示必須進行檢查的屬性。

  • ineIndex - 此參數接受一個Number,表示要檢查樣式的索引。

範例 1

使用 styleHas 方法

讓我們來看一個程式碼範例,看看使用 styleHas 方法時記錄的輸出。在本例中,我們檢查第 0 行索引中的字元是否存在填充屬性。由於該值存在,因此控制台中會傳回真值。

<!DOCTYPE html>
<html>
<head>
   <!-- Adding the Fabric JS Library-->
   <scrip src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
</head>
<body>
   <h2>Using the styleHas method</h2>
   <p>You can open console from dev tools and see that a true 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 ", {
         width: 300,
         left: 50,
         top: 70,
         fontSize: 30,
         fill: "red",
         backgroundColor: "#fedad2",
         angle: 25,
         styles: {
            0: {
               1: {
                  fill: "blue",
                  fontStyle: "bold",
               },
            },
         },
      });

      // Using the styleHas method
      console.log("Is fill property present? : ", itext.styleHas("fill", 0));

      // Add it to the canvas
      canvas.add(itext);
   </script>
</body>
</html>
登入後複製

範例 2

使用 styleHas 方法檢查不存在的值

#讓我們來看一個程式碼範例,看看使用 styleHas 方法檢查不存在的值時記錄的輸出是什麼樣的。在本例中,我們檢查第 0 行中的字元是否存在 fontSize 屬性。由於它不存在,因此傳回錯誤值。

<!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 styleHas method to check for a value that is not present</h2>
   <p>You can open console from dev tools and see that a false 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 ", {
         width: 300,
         left: 50,
         top: 70,
         fontSize: 30,
         fill: "red",
         backgroundColor: "#fedad2",
         angle: 25,
         styles: {
            0: {
               1: {
                  fill: "blue",
                  fontStyle: "bold",
               },
            },
         },
      });

      // Using the styleHas method
      console.log(
         "Is font size property present? : ",
          itext.styleHas("fontSize", 0)
      );

      // Add it to the canvas
      canvas.add(itext);
   </script>
</body>
</html>
登入後複製

以上是如何使用 FabricJS 檢查 IText 物件是否具有特定的樣式屬性?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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