Bouncing Ball Animation with the W3C DOM
The bouncerw3cdom.html file simulates the world’s best superball—it bounces forever. Of course you may change that by pressing a key. The ball will bounce lower and lower until coming to a rest. The variables are:
- x is the horizontal coordinate
- y is the vertical coordinate
- h is the horizontal increment value (+ or -); initial value is 10
- v is the vertical increment value (+ or -); initial value is 2
- g is the force of gravity; initial value is 2
- r and q are variables for saving the y value from one move ago
- gg is a flag to remind gravity variable (g) to get a lot more powerful (change from 2 to 5) since a key has been pressed
Here's the bouncing ball div and the CSS styles for it from the head section:
<div id="ball" class="b"><img src="ball.gif" width="20" height="20"></div>
<style type="text/css">
.b {position:absolute; left:0px; top:0px; width:20px; height:20px;}
</style>
Check out the keypress event capturing code that Netscape requires just to begin the process of keypress monitoring:
var Netscape=(navigator.appName.indexOf("Netscape") != -1);
if(Netscape){document.captureEvents(Event.KEYPRESS);document.onkeypress = testKey;}
Below is the body tag. Note that IE can turn on keypress monitoring from here:
<body onLoad="moveball();" onKeyPress="testKey();">
Here is the only response to a keypress, but since gg is a global variable—declared outside of functions—the moveball() function will feel the effects immediately and slowly stop the ball:
function testKey(){gg=5;}
Below is the moveball() function. First we get the ball object properties into the e variable. Next we add gravity g to the vertical increment value, which will give the ball a tendency to move downward and increase/decrease the value of v greatly as the ball moves. Next we save the old y variable using variables q and r. Now we check to see if the ball seems to have ceased moving vertically—bouncing—by seeing if y equals r. But we also must check to see if y is at the screen bottom (y>394) and gravity has been increased by a keypress (g equals 5). If all these are true we exit the function. If not, add the h and v increment (or decrement) values to the x and y coordinate values respectively.
Whenever the ball hits a screen edge, bounce off it by reversing the sign of the h or v increment/decrement value, say the next three lines. The ball hasn’t the inertia to bounce back up to the ceiling, so no test for y hitting 0 was needed. Note that Netscape has its own idea about where the screen edges are. You may actually need to change these parameters a bit to compensate for the way your screen window is set up.
Next we need to determine where to have the keypress take effect. Make sure that your mouse cursor isn't outside the window or the keypress won't always register, at least in Netscape. Our experiments showed that when the value of the vertical increment v is –26 (and the testKey() function has set the gg flag due to a keypress), the gravity increase should kick in. Values like –10 and –22 stunk. If you're so inclined, investigate why some values are lousy and –26 makes for a stellar performance. Next we update the e object properties with regard to screen coordinates, and then use the setTimeout method to keep the function going. You may experiment with values other than 0 here if the spirit moves you. Or figure out why IE code makes for a slower ball. Or figure out who really whacked JFK.
function moveball(){
var e = document.getElementById('ball');
v=v+g;r=q;q=y;if(y==r&&y>394&&g==5)return;
x=x+h;y=y+v;
if(x>752+(Netscape)*28){x=752+(Netscape)*28;h=h*-1;}
if(y>408-(Netscape)*12){y=408-(Netscape)*12;v=v*-1;}
if(x<0){x=0;h=h*-1;}
if(v==-26&&gg==5){gg=0;g=5;}
e.style.top=y + 'px';
e.style.left=x + 'px';
t=setTimeout("moveball()",0);
}