Skip to content

Instantly share code, notes, and snippets.

@yratof
Created December 7, 2014 01:15
Show Gist options
  • Save yratof/ac6a060a1121ba608073 to your computer and use it in GitHub Desktop.
Save yratof/ac6a060a1121ba608073 to your computer and use it in GitHub Desktop.
JS - Eye follows mouse
$( document ).on( "mousemove", function( event ) {
var mousePosition = {};
mousePosition.x = event.pageX;
mousePosition.y = event.pageY;
$(".eye").each(function(index,element){
eyePosition = $(this).offset();
angle = Math.atan2(mousePosition.y - eyePosition.top, mousePosition.x - eyePosition.left);
translateX = 10 * Math.cos(angle);
translateY = 10 * Math.sin(angle);
$(this).attr("transform", "translate("+ translateX +" "+ translateY+")");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment