首頁  >  文章  >  web前端  >  FabricJS – 如何設定 Line 物件的 URL 字串中的品質等級?

FabricJS – 如何設定 Line 物件的 URL 字串中的品質等級?

王林
王林轉載
2023-08-31 21:21:02479瀏覽

FabricJS – 如何设置 Line 对象的 URL 字符串中的质量级别?

在本教程中,我們將學習如何使用 FabricJS 設定 Line 物件的 URL 字串中的品質等級。 Line 元素是 FabricJS 中提供的基本元素之一。它用於創建直線。由於線元素在幾何上是一維的並且不包含內部,因此它們永遠不會被填充。

我們可以透過建立fabric.Line的實例來建立線條對象,指定線條的x和y座標並將其添加到畫布上。為了設定 Line 物件的 URL 字串中的品質級別,我們使用 quality 屬性。

文法

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

參數

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

選項鍵

  • quality - 此屬性接受一個 Number 值,表示最終輸出影像的品質等級。可接受的值介於 0 和 1 之間,不包括 0。0.1 表示品質最差,1 表示品質最好。此屬性只能用於 jpeg 格式。預設值為 1。

不使用quality屬性

範例

讓我們看一個程式碼範例,看看不使用 quality 屬性時的輸出圖像。一旦我們從開發工具開啟控制台,我們就可以看到 Line 物件的 URL 表示。我們可以複製該 URL 並將其貼上到新分頁的網址列中以查看最終輸出。由於我們沒有使用 quality 屬性,因此將使用預設值,即 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 quality 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({ format: 'jpeg' }));
   </script>
</body>
</html>

使用品質屬性

#範例

讓我們看一個程式碼範例,看看使用 quality 屬性時 Line 物件的最終輸出圖像是什麼樣子。在本例中,我們向其傳遞了值 0.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>Using the quality 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({ format: 'jpeg' ,  quality: 0.1}));
   </script>
</body>
</html>

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

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