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

如何使用 FabricJS 在 IText 物件的 URL 字串中設定縮放倍數?

PHPz
發布: 2023-08-24 17:01:16
轉載
1212 人瀏覽過

如何使用 FabricJS 在 IText 对象的 URL 字符串中设置缩放倍数?

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

然而,基於 IText 的 Textbox 允許我們調整文字矩形的大小並自動換行。對於 IText 來說並非如此,因為高度不會根據換行進行調整。我們可以透過使用各種屬性來操作 IText 物件。同樣,我們可以使用 multiplier 屬性在 IText 物件的 URL 字串中設定縮放倍數。

文法

toDataURL({ multiplier: Number }: Object): String
登入後複製

參數

  • 選項(可選) - 此參數是一個物件,它為 IText 物件的 URL 表示形式提供額外的自訂。使用此參數可以變更高度、品質、格式和許多其他屬性,其中乘數是屬性。

選項鍵

  • multiplier - 此屬性接受一個 Number 值,該值表示縮放最終 IText 輸出影像的乘數。預設值為 1。

範例 1

不使用 multiplier 屬性

讓我們來看一個程式碼範例,看看不使用 multiplier 屬性時的輸出圖像。一旦我們從開發工具開啟控制台,我們就可以看到 IText 物件的 URL 表示。我們可以複製該 URL 並將其貼上到新分頁的網址列中以查看最終輸出。由於我們沒有使用 multiplier 屬性,因此將使用預設的 multiplier 值,即 1。

<!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>Without using the multiplier property</h2>
   <p>You can open console from dev tools and see the output URL. You can copy that and paste it in the address bar of a new tab to see that the image</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 shadow object
      var shadow = new fabric.Shadow({
         blur: 25,
         color: "grey",
         offsetX: 12,
         offsetY: 15,
      });

      // Initiate an itext object
      var itext = new fabric.IText(
         "Add sample text here.Lorem ipsum dolor sit amet consectetur adipiscing.",{
            width: 300,
            left: 310,
            top: 70,
            fill: "#c70039",
            backgroundColor: "#c1dfed",
            stroke: "#c70039",
            originX: "center",
            shadow: shadow,
         }
      );

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

      // Using the toDataURL method
      console.log(itext.toDataURL());
   </script>
</body>
</html>
登入後複製

範例

使用乘數屬性

#讓我們來看一個程式碼範例,看看使用 multiplier 屬性時 IText 物件的最終輸出圖像是什麼樣子。在本例中,我們為其傳遞了值 2。因此最終影像將在 x 和 y 方向上縮放兩倍。

<!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 multiplier property</h2>
   <p>You can open console from dev tools and see the output URL. You can copy that and paste it in the address bar of a new tab to see the final image </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 shadow object
      var shadow = new fabric.Shadow({
         blur: 25,
         color: "grey",
         offsetX: 12,
         offsetY: 15,
      });

      // Initiate an itext object
      var itext = new fabric.IText(
         "Add sample text here.Lorem ipsum dolor sit amet consectetur adipiscing.",{
            width: 300,
            left: 310,
            top: 70,
            fill: "#c70039",
            backgroundColor: "#c1dfed",
            stroke: "#c70039",
            originX: "center",
            shadow: shadow,
         }
      );

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

      // Using the toDataURL method
      console.log(itext.toDataURL({ multiplier: 2 }));
   </script>
</body>
</html>
登入後複製

以上是如何使用 FabricJS 在 IText 物件的 URL 字串中設定縮放倍數?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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