<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Draw Anywhere</title>
  <style>
    body {
      margin: 0;
      padding: 0;
      height: 100vh;
      overflow: hidden;
      position: relative;
      background-color: #f0f0f0;
    }
    #drawing-canvas {
      position: absolute;
      top: 0;
      left: 0;
      width: 100vw;
      height: 100vh;
    }
    .controls {
      position: absolute;
      top: 10px;
      left: 10px;
      z-index: 10;
    }
    .controls button {
      padding: 10px 20px;
      font-size: 1rem;
      cursor: pointer;
    }
  </style>
</head>
<body>
  <div class="controls">
    <button onclick="clearCanvas()">Eraser</button>
  </div>
  <canvas id="drawing-canvas"></canvas>

  <script>
    // Select the canvas and set up the context
    const canvas = document.getElementById('drawing-canvas');
    const ctx = canvas.getContext('2d');

    // Set the canvas size to cover the entire viewport
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    // Drawing variables
    let isDrawing = false;

    // Start drawing
    canvas.addEventListener('mousedown', (e) => {
      isDrawing = true;
      ctx.beginPath();
      ctx.moveTo(e.clientX, e.clientY); // Use clientX and clientY for full-page drawing
    });

    // Draw on the canvas
    canvas.addEventListener('mousemove', (e) => {
      if (isDrawing) {
        ctx.lineTo(e.clientX, e.clientY);
        ctx.strokeStyle = 'blue'; // Set the drawing color
        ctx.lineWidth = 3; // Set the line width
        ctx.stroke();
      }
    });

    // Stop drawing
    canvas.addEventListener('mouseup', () => {
      isDrawing = false;
      ctx.closePath();
    });

    canvas.addEventListener('mouseleave', () => {
      isDrawing = false;
      ctx.closePath();
    });

    // Eraser functionality
    function clearCanvas() {
      ctx.clearRect(0, 0, canvas.width, canvas.height);
    }

    // Handle window resize
    window.addEventListener('resize', () => {
      const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); // Save the current drawing
      canvas.width = window.innerWidth;
      canvas.height = window.innerHeight;
      ctx.putImageData(imageData, 0, 0); // Restore the drawing after resizing
    });
  </script>
</body>
</html>
