Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@moklick
Created January 28, 2014 15:52
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 moklick/8670204 to your computer and use it in GitHub Desktop.
Save moklick/8670204 to your computer and use it in GitHub Desktop.
A Pen by moklick.
<canvas id="canvas"></canvas>
<div class="google">Google</div>
window.requestAnimFrame = (function() {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
var canvas;
var ctx;
var WIDTH;
var HEIGHT;
var leftEye,rightEye;
var mouse;
Eye = function(pos) {
this.pos = {
x : pos.x,
y : pos.y
};
this.center = {
x : pos.x,
y : pos.y
};
this.translation = {
x : (window.innerWidth / 2 - canvas.width / 2) + this.center.x,
y : this.center.y
};
}
Eye.prototype.draw = function() {
ctx.beginPath();
ctx.arc(this.pos.x, this.pos.y, 9, 0, Math.PI * 2);
ctx.fillStyle = '#333';
ctx.fill();
}
Eye.prototype.update = function() {
var deltaX = mouse.x - this.translation.x;
var deltaY = mouse.y - this.translation.y;
var mag = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
var angleRad = Math.atan2(deltaY, deltaX);
var newPosX = this.center.x + 6 * Math.cos(angleRad);
var newPosY = this.center.y + 11 * Math.sin(angleRad);
this.pos.x += (newPosX - this.pos.x) / 5;
this.pos.y += (newPosY - this.pos.y) / 5;
}
var init = function() {
canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
canvas.width = WIDTH = 300;
canvas.height = HEIGHT = 125;
leftEye = new Eye({
x : WIDTH / 2 - 42,
y : HEIGHT / 2 + 18
});
rightEye = new Eye({
x : WIDTH / 2 + 8,
y : HEIGHT / 2 + 18
});
mouse = {
x : 0,
y : 0
};
bindEventHandlers();
draw();
}
var draw = function() {
ctx.clearRect(0, 0, WIDTH, HEIGHT);
leftEye.update();
rightEye.update();
leftEye.draw();
rightEye.draw();
requestAnimFrame(draw);
}
var bindEventHandlers = function() {
document.onmousemove = function(e) {
mouse.x = e.pageX;
mouse.y = e.pageY;
}
}
init();
@import url(http://fonts.googleapis.com/css?family=EB+Garamond);
* {
margin: 0;
padding: 0;
}
body {
font-family: 'EB Garamond', serif;
color:#333;
}
#canvas {
display: block;
position: absolute;
left: 50%;
margin-left: -150px;
}
.google {
font-size: 100px;
width: 300px;
text-align: center;
margin: 0 auto;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment