Skip to content

Instantly share code, notes, and snippets.

@herschel666
Created April 3, 2012 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save herschel666/2292219 to your computer and use it in GitHub Desktop.
Save herschel666/2292219 to your computer and use it in GitHub Desktop.
Follow the Cursor
canvas {
margin: 40px;
border: 1px solid #999;
}​
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title>Follow the Cursor</title>
<link rel="stylesheet" href="follow_the_cursor.css" />
</head>
<body>
<canvas id="cnvs" width="400" height="400"></canvas>
<script src="follow_the_cursor.js"></script>
</body>
</html>
var cnvs = document.getElementById('cnvs');
var pos_x = 0;
var pos_y = 0;
var ctx = cnvs.getContext('2d');
function draw(x, y) {
ctx.fillStyle = 'rgba(255,255,255,.3)';
ctx.fillRect(0,0,399,399);
ctx.fillStyle = '#111';
ctx.beginPath();
ctx.moveTo(x, y);
ctx.arc(x,y,10,0,Math.PI*2,true);
ctx.closePath();
ctx.fill();
}
function animate() {
setTimeout(animate, 1000/60);
cnvs.addEventListener('mousemove', function (evnt) {
pos_x = evnt.offsetX;
pos_y = evnt.offsetY;
}, true);
draw(pos_x, pos_y);
}
animate();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment