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

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

WBOY
發布: 2023-08-23 21:37:08
轉載
1288 人瀏覽過

如何使用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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!