Web Page Animation17. Handling Page Resize |
|
Join the DiscussionMore of this FeatureDefine Web Page Object Hide Until Page Loaded Scrolling With the Page Random Start Position Hide Again Now You See It Making it Move Select End Position Moving Vertically Speed of Movement Reversing Direction Perpetual Motion Moving Diagonally Any Angle Random Movement Specific Path One thing that we haven't considered so far is what to do if the person viewing our page resizes their browser window. All of our carefully calculated positions on the screen that were worked out based on the size of their browser window are now all incorrect. Recalculating the current position of our object part way through a move is rather complicated so let's take the easier way our of letting the object finish its current move and just recalculate the positions on the screen that it will move to after that. This will have the effect of having the object adjust the path of its movement on the next move that it makes to bring it into line with the new screen size. To do this the first thing we need to do is to pull the end position calculations out of the start function into a new function of their own.
function setEnd() {
var w = objWidth('myobj'); var h = objHeight('myobj'); eX[0] = eX[2] = eX[4] = eX[6] = (pageWidth()-w)/2; eY[0] = eY[2] = eY[4] = eY[6] = (pageHeight()-h)/2; eX[1] = eX[3] = 20; eY[1] = eY[7] = 10; eY[3] = eY[5] = (pageHeight()-h-10); eX[5] = eX[7] = (pageWidth()-w-20); } Next we amend our start function to call this function to set up its destinations. Our start position which was being set at the same time as the various end positions before was not extracted since it will be constantly updated to record the current position of the object. Instead we still set it in the start function itself making it equal to the last position in the array.
function start() {
setEnd(); yyy = eY[eY.length - 1]; xxx = eX[eX.length - 1]; nextEnd(); moveit(); setObjVis('myobj','visible'); mover(); } The final change is to add an event handler to reset the array of end positions if the browser is resized.
window.onresize = setEnd;
Now when the browser window is resized your object still might disappear out of the window but it will move back into the window on the next leg of its journey and then continue to follow the equivalent path to before taking into account the new window size. |

