如何在 HTML5 中建立透明畫布?


globalAlpha 屬性返回繪圖的透明值。此屬性的值 1.0 表示不透明,此屬性的值 0.0 表示完全透明。因此,透過將 globalAlpha 屬性設定為 0.0,我們可以得到一個透明的畫布。

示例

你可以嘗試執行以下程式碼來製作一個透明的畫布 −

<!DOCTYPE html>
<html>
   <body>
      <canvas id = "idCanvas" width = "400" height = "200" style = "border:2px solid #000000;">
         Your browser does not support the HTML5 canvas tag.</canvas>
      <script>
         var c = document.getElementById("idCanvas");
         var ctx = c.getContext("2d");
         ctx.fillStyle = "green";
         ctx.fillRect(20, 20, 75, 50);
         ctx.globalAlpha = 0.0;
         ctx.fillStyle = "yellow";
         ctx.fillRect(50, 50, 75, 50);
         ctx.fillStyle = "red";
         ctx.fillRect(80, 80, 75, 50);
      </script>
   </body>
</html>

更新於:03-Mar-2020

4K+ 檢視次數

開啟你的 事業

完成課程獲得認證

開始
廣告
© . All rights reserved.