Animation: eyes : JavaScript DHTML examples (example source code) » GUI Components » Animation

JavaScript DHTML










Java Products
Java Articles
JavaScript DHTML Home  »   GUI Components   » [  Animation  ]   

 
Animation: eyes

Please note that some example is only working under IE or Firefox.

/*


Paste this link to where you want the eyes to appear in your page.
MAKE SURE YOU *DO NOT* PLACE IT INSIDE ANY <DIV>.

<script type="text/javascript" src="staticeyes.js"></script>


Make sure that these files are in/uploaded to the same directory/folder
as the web page using the script:

staticeyes.js
eye.gif
pupils.gif


You can paste 
<script type="text/javascript" src="staticeyes.js"></script>
as many times as you like in to the same page.


However, if you want each set to follow a different style sheet,
open and study "more.zip".






*/

//Static Eyes - http://www.btinternet.com/~kurt.grigg/javascript 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

<title>Static Eyes</title>

<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="content-script-type" content="text/javascript">
<meta http-equiv="content-style-type" content="text/css">

</head>
<body>




<p>DON'T FORGET - DO NOT PUT INSIDE ANY OTHER DIV!</p>
<script type="text/javascript" >
//Static Eyes - http://www.btinternet.com/~kurt.grigg/javascript 
if  ((document.getElementById&& 
window.addEventListener || window.attachEvent){

(function(){
var e_img = new Image();
e_img.src = "eye.gif"
var p_img = new Image();
p_img.src = "pupils.gif";

var d = document;

var pix = "px";

var idx = document.images.length;
if (document.getElementById("cont"+idx)) idx++;

var eyeballs = "";
var pupil1 = "";
var pupil2 = "";

d.write('<div id="cont'+idx+'" class="eyestyle" style="height:34px;width:69px">'
+'<div id="eyblls'+idx+'" style="position:relative;width:69px;height:34px"><img src="'+e_img.src+'" alt=""/>'
+'<img id="ppl1'+idx+'" src="'+p_img.src+'" alt="" style="position:absolute;top:10px;left:11px;width:13px;height:13px"/>'
+'<img id="ppl2'+idx+'" src="'+p_img.src+'" alt="" style="position:absolute;top:10px;left:46px;width:13px;height:13px"/>'
+'<\/div><\/div>');

function watchTheMouse(y,x){
var osy = eyeballs.offsetTop;
var osx = eyeballs.offsetLeft;
var c1y = osy + 17;
var c1x = osx + 17;
var c2y = osy + 17;
var c2x = osx + 52;
var dy1 = y - c1y;
var dx1 = x - c1x;
var d1 = Math.sqrt(dy1*dy1 + dx1*dx1);
var dy2 = y - c2y;
var dx2 = x - c2x;
var d2 = Math.sqrt(dy2*dy2 + dx2*dx2);
var ay1 = y - c1y;
var ax1 = x - c1x;
var angle1 = Math.atan2(ay1,ax1)*180/Math.PI;
var ay2 = y - c2y;
var ax2 = x - c2x;
var angle2 = Math.atan2(ay2,ax2)*180/Math.PI;
var dv = 1.7;
var onEyeBall1 = (d1 < 17)?d1/dv:10;
var onEyeBall2 = (d2 < 17)?d2/dv:10;
pupil1.top = c1y-6+onEyeBall1 * Math.sin(angle1*Math.PI/180)-osy+pix;
pupil1.left = c1x-6+onEyeBall1 * Math.cos(angle1*Math.PI/180)-osx+pix;
pupil2.top = c2y-6+onEyeBall2 * Math.sin(angle2*Math.PI/180)-osy+pix;
pupil2.left = c2x-6+onEyeBall2  *Math.cos(angle2*Math.PI/180)-osx+pix;
}

function mouse(e){
var y,x;
if (!ee = window.event;    
 if (typeof e.pageY == 'number'){
  y = e.pageY;
  x = e.pageX;
 }
 else{
 var ref = document.documentElement||document.body;
 y = e.clientY + ref.scrollTop;
 x = e.clientX + ref.scrollLeft;
}
watchTheMouse(y,x);
}

function init(){
eyeballs = d.getElementById("eyblls"+idx);
pupil1 = d.getElementById("ppl1"+idx).style;
pupil2 = d.getElementById("ppl2"+idx).style;
}

if (window.addEventListener){
 window.addEventListener("load",init,false);
 document.addEventListener("mousemove",mouse,false);
}  
else if (window.attachEvent){
 window.attachEvent("onload",init);
 document.attachEvent("onmousemove",mouse);


})();
}//End.
</script>




</body>
</html>
Download: staticeyes.zip   (8  K)  
Related examples in the same category
1.  Circle Animation
2.  Right to left animation
3.  Flash animation in Javascript
4.  Flash animation in JavaScript: Changing style Properties
5.  Animation along Straight Line
6.  Animation along a Circle
7.  Dancing Text (IE)
8.  Type Writer effect (IE)
9.  Type Writer Effect 1.1 (IE)
10.  JavaScript Ticker 1.2 (IE)
11.  Animation: Lottery Number Picker and Statistics
12.  Animation: wriggly
13.  Animation: welcome message
14.  Animation: trio
15.  Auto lotto dip
16.  Animation: snow
17.  Animation: star
18.  Animation: mouse doodle
19.  Animation: fireworks
20.  Animation: pretty
21.  Animation: Random Movement
22.  Lotto number draw
23.  Following eyes
24.  Animation: three eyes
25.  Spot light
26.  Big static eyes
27.  Animation based on DIV with color flash
28.  Framework for creating CSS-based animations
29.  Animate dynamic element h1 tag
30.  Popup window animation (fly across screen)
31.  Animation on several images
32.  Using the onFilterChange Event Handler
33.  JavaScript Animation
34.  Periodically Updating the Text Displayed by an HTML Element
35.  Moving an Airplane Across a Web Page
36.  Link Hint Scroller 2.0 (IE)








Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.