-
-
Save binux/4ca56695f2072e565ed26bd6580f995e to your computer and use it in GitHub Desktop.
JS Bin// source http://jsbin.com/jaqavetema
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<span id="pos"></span> | |
<canvas id="myCanvas" width="1024" height="728"></canvas> | |
<script> | |
var span = document.getElementById('pos'); | |
var canvas = document.getElementById('myCanvas'); | |
var context = canvas.getContext('2d'); | |
function getMousePos(canvas, evt) { | |
var rect = canvas.getBoundingClientRect(); | |
return { | |
x: evt.clientX - rect.left, | |
y: evt.clientY - rect.top | |
}; | |
} | |
canvas.addEventListener('mousemove', function(evt) { | |
var mousePos = getMousePos(canvas, evt); | |
span.textContent = JSON.stringify(mousePos); | |
context.lineTo(mousePos.x, mousePos.y); | |
context.fillStyle = 'red'; | |
context.fillRect(mousePos.x - 2, mousePos.y - 2, 5, 5); | |
context.stroke(); | |
}, false); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment