首頁  >  文章  >  web前端  >  如何使用 FabricJS 取得 IText 中單字的正確邊界?

如何使用 FabricJS 取得 IText 中單字的正確邊界?

WBOY
WBOY轉載
2023-08-30 20:57:02963瀏覽

如何使用 FabricJS 获取 IText 中单词的正确边界?

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

然而,基於 IText 的 Textbox 允許我們調整文字矩形的大小並自動換行。對於 IText 來說並非如此,因為高度不會根據換行進行調整。我們可以透過使用各種屬性來操作 IText 物件。同樣,我們也可以使用findWordBoundaryRight方法來找出單字的右邊界。

文法

findWordBoundaryRight(startFrom: Number): Number

參數

  • startFrom - 此參數接受一個Number,表示目前選擇索引,將為其傳回右邊界值的新選擇索引。

範例 1

使用 findWordBoundaryRight 方法

#讓我們看一個程式碼範例,以查看使用 findWordBoundaryRight 方法時記錄的輸出。在這裡,我們傳遞的值為 5,這意味著我們要檢查第二個單字的右邊界。因此傳回值將為 10。

<!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 findWordBoundaryRight method</h2>
   <p>You can open console from dev tools and see that the right boundary 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 here.", {
         width: 300,
         left: 60,
         top: 70,
         fill: "red",
      });
      // Add it to the canvas
      canvas.add(itext);

      // Using findWordBoundaryRight method
      console.log("The new selection index is: ",
      itext.findWordBoundaryRight(5));
   </script>
</body>
</html>

範例 2

使用findWordBoundaryRight方法計算第一個單字的單字邊界

讓我們來看一個程式碼範例,了解如何使用 findWordBoundaryRight 方法找到第一個單字的新選擇索引。在本例中,我們向 startFrom 參數傳遞了值 1,該值將出現在第一個單字中。因此,傳回的新選擇索引值為3,即右邊界。

<!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 findWordBoundaryRight method to calculate the word boundary for the first word</h2>
   <p> You can open console from dev tools and see that the right boundary 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 here.", {
         width: 300,
         left: 60,
         top: 70,
         fill: "red",
      });

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

      // Using findWordBoundaryRight method
      console.log("The new selection index is: ",
      itext.findWordBoundaryRight(1));
   </script>
</body>
</html>

以上是如何使用 FabricJS 取得 IText 中單字的正確邊界?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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