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

如何使用FabricJS刪除IText物件的URL字串中的目前物件陰影?

WBOY
發布: 2023-08-24 23:13:08
轉載
1194 人瀏覽過

如何使用FabricJS刪除IText物件的URL字串中的目前物件陰影?

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

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

文法

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

參數

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

選項鍵

  • withoutShadow - 該屬性接受一個布林值,它允許我們擺脫目前物件的陰影。

範例 1

使用 withoutShadow 屬性並向其傳遞一個 false 值

讓我們看一個程式碼範例,看看向 withoutShadow 屬性傳遞 false 值時的輸出圖像。一旦我們從開發工具開啟控制台,我們就可以看到 IText 物件的 URL 表示。我們可以複製該 URL 並將其貼上到新分頁的網址列中以查看最終輸出。在此範例中,我們向 IText 物件傳遞了 Shadow 屬性。由於我們也向 withoutShadow 屬性傳遞了一個 false 值,因此我們的最終輸出影像仍將包含陰影。

<!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 withoutShadow property and passing it a false value</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 final image contains a shadow </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: 50,
            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({ withoutShadow: false }));
   </script>
</body>
</html>
登入後複製

範例 2

使用 withoutShadow 屬性並向其傳遞一個真值

#讓我們看一個程式碼範例,看看當使用 withoutShadow 屬性並向其傳遞 true 值時,IText 物件的最終輸出圖像會是什麼樣子。在這種情況下,我們的最終輸出影像將不包含任何陰影。

<!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 withoutShadow property and passing it a true value</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 final image does not contain a shadow </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: 50,
            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({ withoutShadow: true }));
   </script>
</body>
</html>
登入後複製

以上是如何使用FabricJS刪除IText物件的URL字串中的目前物件陰影?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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