Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  <div>
            <canvas id="drawCanvas" width="550" height="200"></canvas>
            <script type="text/javascript">
                window.onload = function (e) {
                    var drawCanvas = document.getElementById("drawCanvas");
                    var ctx = drawCanvas.getContext('2d');
                    ctx.font = 'bold 19pt Courier New';
                    ctx.fillStyle = 'black';
                    ctx.fillText('Mouse coordinates:', 20, 180);
                    drawCanvas.onmousemove = function (e) {
                        var drawCanvas = e.target;
                        var ctx = drawCanvas.getContext('2d');
                        var mousePosX = e.offsetX;
                        var mousePosY = e.offsetY;
                        if (mousePosX && mousePosY) {
                            // Reset the canvas by setting the width to itself
                            drawCanvas.width = drawCanvas.width;
                            // Now add some text to it
                            ctx.font = 'bold 19pt Courier New';
                            ctx.fillText('Mouse coordinates: x: ' + Math.floor(mousePosX) + ', y: ' + Math.floor(mousePosY), 20, 180);
                        }
                        else {
                            alert("No support for firefox");
                        }
                    }
                }
            </script>
        </div>
</body>
</html>
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
igurugyanpro
0viewers