首頁  >  文章  >  web前端  >  FabricJS – 如何在 Line 物件的 URL 字串中設定縮放倍數?

FabricJS – 如何在 Line 物件的 URL 字串中設定縮放倍數?

王林
王林轉載
2023-08-28 12:25:11698瀏覽

FabricJS – 如何在 Line 对象的 URL 字符串中设置缩放倍数?

在本教程中,我們將學習如何使用 FabricJS 在 Line 物件的 URL 字串中設定縮放倍數。 Line 元素是 FabricJS 中提供的基本元素之一。它用於創建直線。由於線元素在幾何上是一維的並且不包含內部,因此它們永遠不會被填充。我們可以透過建立 fabric.Line 實例來建立線條對象,指定線條的 x 和 y 座標並將其新增至畫布。為了在 Line 物件的 URL 字串中設定縮放倍數,我們使用 multiplier 屬性。

文法

toDataURL({ multiplier: Number }: Object): String

參數

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

選項鍵

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

不使用multiplier屬性

範例

讓我們來看一個程式碼範例,看看不使用 multiplier 屬性時的輸出映像。一旦我們從開發工具開啟控制台,我們就可以看到 Line 物件的 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 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 Line object
      var line = new fabric.Line([200, 100, 100, 40], {
         stroke: "blue",
         strokeWidth: 20,
         angle: 70,
      });

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

      // Using the toDataURL method
      console.log(line.toDataURL());
   </script>
</body>
</html>

使用乘數屬性

#範例

讓我們來看一個程式碼範例,看看使用 multiplier 屬性時 Line 物件的最終輸出圖像是什麼樣子。在本例中,我們為其傳遞了值 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 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 Line object
      var line = new fabric.Line([200, 100, 100, 40], {
         stroke: "blue",
         strokeWidth: 20,
         angle: 70,
      });

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

      // Using the toDataURL method
      console.log(line.toDataURL({ multiplier: 2 }));
   </script>
</body>
</html>

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

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