首頁  >  文章  >  web前端  >  如何使用FabricJS取得文字的縮放高度?

如何使用FabricJS取得文字的縮放高度?

WBOY
WBOY轉載
2023-08-23 21:37:081286瀏覽

如何使用FabricJS取得文字的縮放高度?

在本教程中,我們將學習如何使用FabricJS來取得文字的縮放高度。我們可以透過新增fabric.Text的實例來在畫布上顯示文字。它不僅允許我們移動、縮放和改變文字的尺寸,還提供了其他功能,如文字對齊、文字裝飾、行高,可以透過屬性textAlign、underline和lineHeight分別獲得。我們也可以使用getScaledHeight方法來找到物件的縮放高度。

文法

getScaledHeight()

Example 1

的中文翻譯為:

範例1

使用getScaledHeight方法

讓我們看一個程式碼範例,以查看使用getScaledHeight方法時記錄的輸出。在這種情況下,將會傳回物件的高度。

<!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 getScaledHeight method</h2>
   <p>You can open console from dev tools and see that the height 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 a text object
      var text = new fabric.Text("Add sampletext here", {
         width: 300,
         fill: "green",
         fontWeight: "bold",
      });

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

      // Using getScaledHeight method
      console.log("The scaled height is", text.getScaledHeight());
   </script>
</body>
</html>

Example 2

的翻譯為:

範例2

使用getScaledHeight方法並傳遞scaleY屬性

Let’s see a code example to see the logged output when the getScaledHeight method is used in conjunction with the scaleY property. In this case, final scaled height will be displayed in#sole.

<!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 getScaledHeight method and passing the scaleY property</h2>
   <p>You can open console from dev tools and see that the height value is being displayed in the console has increased </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 a text object
      var text = new fabric.Text("Add sampletext here", {
         width: 300,
         fill: "green",
         fontWeight: "bold",
         scaleY: 2,
      });
      
      // Add it to the canvas
      canvas.add(text);
      
      // Using getScaledHeight method
      console.log("The scaled height is", text.getScaledHeight());
   </script>
</body>
</html>

以上是如何使用FabricJS取得文字的縮放高度?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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