Doc Dingle's Website
Brent M. Dingle, Ph.D.

Mouse over the below area

The Code:

<!DOCTYPE HTML>
<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
  </head>
  <body>
    <canvas id="myCanvas" width="640" height="480"></canvas>
    <script>

      function writeMessage(canvas, message) 
      {
        var context = canvas.getContext('2d');
        // Could just clear the canvas, but to see when you leave the area it needs to be more visible
        // context.clearRect(0, 0, canvas.width, canvas.height);
        context.globalCompositionOperation = "source-over";
        context.fillStyle = "cyan";
        context.fillRect(0, 0, canvas.width, canvas.height);
        context.font = '18pt Calibri';
        context.fillStyle = 'black';
        context.fillText(message, 10, 25);
      }
      function getMousePos(canvas, evt) 
      {
        var rect = canvas.getBoundingClientRect();
        return {
          x: Math.round( (evt.clientX-rect.left)/(rect.right-rect.left)*canvas.width ),
          y: Math.round( (evt.clientY-rect.top)/(rect.bottom-rect.top)*canvas.height )
        };
      }
      var canvas = document.getElementById('myCanvas');
      var context = canvas.getContext('2d');

      canvas.addEventListener('mousemove', function(evt) 
      {
        var mousePos = getMousePos(canvas, evt);
        var message = 'Mouse position: ' + mousePos.x + ',' + mousePos.y;
        writeMessage(canvas, message);
      }, false);

    </script>
  </body>
</html>