Skip to content
html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    html, 
    body {
      width: 100vw;
      height: 100vh;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }

    div {
      width: 50vw;
      height: 33vh;
    }

    .glasspane {
      width: 150px;
      height: 75px;
      background: rgba(0, 0, 0, 0.3);
      border: 1px solid red;
    }
  </style>
</head>
<body>
  <div>
    <h3>
      玻璃窗格位于 canvas 元素之前,玻璃窗格开启绝对定位,脱离文档流,使得玻璃窗格在 canvas
      元素上。
    </h2>
    <div class="glasspane" style="position: absolute;"></div>
    <canvas id="canvas"></canvas> 
  </div>

  <div>
    <h3>
      玻璃窗格位于 canvas 元素之后,玻璃窗格和 canvas 开启绝对定位,脱离文档流,使得玻璃窗格
      在 canvas 元素上。
    </h2>
    <canvas id="canvas" style="position: absolute;"></canvas> 
    <div class="glasspane" style="position: absolute;"></div>
  </div>

  <div>
    <h3>
      玻璃窗格位于 canvas 元素之前,玻璃窗格和 canvas 开启绝对定位,脱离文档流,通过对玻璃窗格
      设置 z-index 使得玻璃窗格在 canvas 元素上。
    </h2>
    <div class="glasspane" style="position: absolute; z-index: 10;"></div>
    <canvas id="canvas" style="position: absolute;"></canvas> 
  </div>

  <script>
    const canvasList = document.querySelectorAll('canvas')

    for (let i = 0, l = canvasList.length; i < l; i++) {
      const canvas = canvasList[i]
      const context = canvas.getContext('2d')

      canvas.style.backgroundColor = '#ddd'

      context.font = '32px Arial'
      context.fillStyle = 'red'
      context.fillText('Hello Canvas', 0, 100)
    }
  </script>
</body>
</html>