Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving Eyes with MouseMove event?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Helle Everyone,

I am trying to build a page so when i move the mouse around on the page the set of eyes that i have on the page with follow the mouse around. The eyes are set up so there are pupils inside the eye. The pupils will move inside the eye when the mouse is moved around. Can anyone help me finish the code? I am not sure how to set up xdist, ydist, and the PUPIL_RADIUS correctly.

Thanks... Here is the code.

<html>
<head>
<title>New Page 1</title>
<script = &quot;javascript1.2&quot;>
<!--
function Init()
{
document.onmousemove=MoveEyes;
document.captureEvents(Event.MOUSEMOVE);
}

function MoveEyes()
{
var dist;
var angle;
var radius;
var xoffset
var yoffset
var xdist = offsetx
var ydist = offsety
var PUPIL_RADIUS = 10

// Compute the straight-line distance from the center of the eye to the mouse.
dist = Math.sqrt( (xdist * xdist) + (ydist * ydist) );

// Compute the angle from the center of the eye to the mouse.
angle = Math.atan2(ydist, xdist);

// Don't let the pupil leave the eyeball.
radius = Math.min(dist, PUPIL_RADIUS);

// Figure out how much the pupil should be offset from the
// center of the eyeball.
xoffset = radius * Math.cos(angle);
yoffset = radius * Math.sin(angle);
}
//-->
</script>

</head>
<body>
<p>
<DIV ID = &quot;pupil&quot; STYLE = &quot;position: absolute; left:37.5; top:32;&quot;>
<IMG SRC = &quot;pupil.gif&quot; onmousemove = &quot;MoveEyes()&quot;>
</DIV>
<DIV ID = &quot;pupil&quot; STYLE = &quot;position: absolute; left:62.5; top:32;&quot;>
<IMG SRC = &quot;pupil.gif&quot; onmousemove = &quot;MoveEyes()&quot;>
</DIV>
<DIV ID = &quot;eyes&quot; STYLE = &quot;position: absolute; left:30; top:30;&quot;>
<IMG SRC = &quot;eyes.gif&quot; width=&quot;50&quot; height=&quot;25&quot;>
</DIV>




</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top