FabricJS – 如何移除轉換為HTMLCanvasElement的多邊形的陰影?


我們可以透過建立fabric.Polygon的例項來建立一個Polygon物件。多邊形物件可以由任何由一組連線的直線段組成的封閉形狀來表示。由於它是FabricJS的基本元素之一,我們也可以透過應用角度、不透明度等屬性來輕鬆自定義它。

為了將多邊形物件轉換為HTMLCanvasElement,我們使用toCanvasElement方法。它返回型別為HTMLCanvasElement的DOM元素,這是一個介面,它繼承了HTMLElement介面的屬性和方法。我們使用withoutShadow屬性來移除轉換為HTMLCanvasElement的多邊形的陰影。

語法

toCanvasElement({ withoutShadow: Boolean }: Object):
HTMLCanvasElement

引數

  • options (可選) − 此引數接受一個物件,它為我們的HTMLCanvasElement提供了額外的自定義選項。使用此引數,可以更改與HTMLCanvasElement相關的許多屬性,例如高度、左側裁剪偏移量,其中withoutShadow就是一個屬性。

選項鍵

  • withoutShadow − 此屬性接受一個布林值,用於確定是否移除當前物件的陰影。此屬性是可選的。

示例 1:使用 withoutShadow 屬性並傳入“false”值

讓我們來看一個程式碼示例,看看在使用toCanvasElement方法和withoutShadow屬性時記錄的輸出。當傳入“false”值時,withoutShadow屬性會保留任何存在的物件陰影。因此,在這種情況下,其輸出影像中將存在陰影。我們同時使用了toDataURL方法和toCanvasElement方法來顯示輸出影像的實際外觀。

<!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, copy the url string and paste it in a new tab to see that the polygon object's image contains 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({
         color: "black",
         blur: 12,
      });
      
      // Initiate a polygon object
      var polygon = new fabric.Polygon(
         [
            { x: 600, y: 310 },
            { x: 650, y: 450 },
            { x: 600, y: 480 },
            { x: 550, y: 480 },
            { x: 450, y: 460 },
            { x: 300, y: 210 },
         ],
         {
            fill: "#778899",
            stroke: "blue",
            strokeWidth: 5,
            top: 50,
            left: 100,
            scaleX: 0.5,
            scaleY: 0.5,
            shadow: shadow,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using toCanvasElement method
      var withoutShadow = polygon.toCanvasElement({
         withoutShadow: false,
      });
      
      // Using toDataURL method
      console.log(withoutShadow.toDataURL());
   </script>
</body>
</html> 

示例 2:使用 withoutShadow 屬性並傳入“true”值

讓我們來看一個程式碼示例,看看在使用toCanvasElement方法和withoutShadow屬性時記錄的輸出。當傳入“true”值時,withoutShadow屬性將移除陰影。因此,在這種情況下,其輸出影像中將不會存在陰影。

<!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, copy the url string and paste it in a new tab to see that the polygon object's image does not contain 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({
         color: "black",
         blur: 12,
      });
      
      // Initiate a polygon object
      var polygon = new fabric.Polygon(
         [
            { x: 600, y: 310 },
            { x: 650, y: 450 },
            { x: 600, y: 480 },
            { x: 550, y: 480 },
            { x: 450, y: 460 },
            { x: 300, y: 210 },
         ],
         {
            fill: "#778899",
            stroke: "blue",
            strokeWidth: 5,
            top: 50, 
            left: 100,
            scaleX: 0.5,
            scaleY: 0.5,
            shadow: shadow,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using toCanvasElement method
      var withoutShadow = polygon.toCanvasElement({
         withoutShadow: true,
      });
      
      // Using toDataURL method
      console.log(withoutShadow.toDataURL());
   </script>
</body>
</html> 

結論

在本教程中,我們使用兩個簡單的示例演示瞭如何使用FabricJS移除轉換為HTMLCanvasElement的多邊形的陰影。

更新於:2023年1月2日

140 次瀏覽

啟動你的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.