Skip to content

Instantly share code, notes, and snippets.

Forked from anonymous/index.html
Last active Jan 5, 2018
What would you like to do?
JS Bin// source
<!DOCTYPE html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<span id="pos"></span>
<canvas id="myCanvas" width="1024" height="728"></canvas>
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 -
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);
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment