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

FabricJS – 如何更改 Line 物件的 URL 字串的格式?

王林
發布: 2023-08-30 10:13:05
轉載
661 人瀏覽過

FabricJS – 如何更改 Line 对象的 URL 字符串的格式?

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

文法

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

參數

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

選項鍵

  • format - 這個屬性接受一個String值,它允許我們定義輸出映像的格式。接受的值為“jpeg”或“png”。預設值為“png”。

不使用format屬性的預設值

範例

讓我們看一個程式碼範例,以查看使用 toDataURL 方法而不使用 format 屬性時記錄的輸出。一旦我們從開發工具開啟控制台,我們就可以看到 Line 物件的 URL 表示。我們可以複製該 URL 並將其貼上到新分頁的網址列中以查看最終輸出。由於我們沒有指定圖像的格式,因此它將根據設定的預設值“png”。

<!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>Default value without using the format property</h2>
   <p>
   You can open console from dev tools and see that the URL representation of the Line object has a "png" format by default
   </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>
登入後複製

使用 toDataURL 方法和 format 屬性

範例

讓我們看一個程式碼範例,看看當 toDataURL 方法與 format 屬性一起使用時 Line 物件是什麼樣子。在這種情況下,我們將能夠指定最終影像的格式。由於此處指派的值為“jpeg”,因此最終圖片將為“jpeg”格式。

<!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 toDataURL method along with format property</h2>
   <p>
   You can open console from dev tools and see that the URL representation of the Line object has a "jpeg" format now
   </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>
登入後複製

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

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