JavaScript eyes that follow the cursor.
I remember one of the first tutorials I wrote back on my old thybag website was on how to get eyes that follow the cursor using Flash. I’d initially worked out how to do it in order to animate the alien in my banner a little and although the effect can be a little disconcerting at times, I kinda liked it.
For that reason I decided to set myself the challenge of getting the functionality back, only this time using JavaScript instead of flash. Rather than waste space with extra images I also decided I may as well draw the eyes with canvas while I was at it.
The aim of this little tutorial will be to show you how to create something like this.
(Though you will more likely want to use your own image.)
If your not really intested in how its done and just want the script, feel free to grab it from here. You can create eye’s by calling
//Variables are just used to make it obvious what each parameter is, //you can call the functions with the param's directly if you prefer. var positionFromLeft = 200; var positionFromTop = 50; var eyeSize = 30; var appendEyetoDivNamed = 'mydiv'; new alienEye(positionFromLeft,positionFromTop,eyeSize,appendEyetoDivNamed);
If your interested to see how the eyes following the cursor script works, keep reading after the jump.