reptile7's JavaScript blog
Tuesday, July 24, 2012
Little Fishes Express
Blog Entry #259

In today's post we will tackle the Changing the Stacking Order of Fish and Poles animation of the Swimming Fish Example of Netscape's Dynamic HTML in Netscape Communicator resource. You may try out the animation in the div below.

Fish Example 2

Upon clicking the button, the swimming fish moves rightward: for this direction, the fish passes in front of the blue pole, behind the red pole, and in front of the green pole à la the Positioning and Moving the Fish and Poles animation we detailed in the previous post. When the fish reaches the right edge of the display, it turns around and swims leftward: for this direction, the fish passes behind the green pole, in front of the red pole, and behind the blue pole. At the left edge of the display, the fish turns around and swims rightward again. Can the fish in your aquarium do this? ;-)

The Swimming Fish Example's Changing the Stacking Order of Fish and Poles section offers two versions of the above animation: a fish2.htm version and a fish2css.htm version that wrap the animation images in layer elements and positioned span elements respectively but whose JavaScript parts are essentially identical (their layer object reference expressions vary but otherwise there's no difference between them). We will employ the fish2css.htm code in the discussion below.

The right-to-left fish

The Changing the Stacking Order animation actually features two swimming fish images:
(1) a right-facing fish1.gif image that is moved left to right (the same image we used for the Positioning and Moving animation); and
(2) a left-facing fish2.gif image that is moved right to left.

#fishB { position: absolute; top: 0px; left: 0px; visibility: hidden; }
<span id="fishB"><img src="images/fish2.gif" name="fishB"></span>

The fish2.gif image is hard-coded in the fish2css.htm document body in order to preload it, thereby ensuring a smooth fish1.gif-to-fish2.gif transition at the right edge of the display. The image's img placeholder is given a name="fishB" attribute for the purpose of referencing it; the fish1.gif image's img placeholder is given a name="fish" attribute* for the same reason. (These attributes negligently do not appear in the fish img code in the Changing the Stacking Order section's text.) The fish2.gif img is placed in an id="fishB" span, which is 'layerized' and removed from the normal flow of the document via its absolute positioning and is hidden.

The fishB span and img elements are merely containers for the fish2.gif image and will not themselves be moved across the viewport; we'll see later that the fish2.gif span/img scaffolding is excess baggage and can be thrown out.

*Excepting this small addition, the fish1.gif and pole images are coded/styled as they are for the Positioning and Moving animation.

Other new HTML

The button's parent form is wrapped in a layer element that positions the form and its button closer to the animation images.

<layer left="10" top="100" name="fishlink">
<form><input type="button" value="Move the fish" onclick="initializeFish( ); movefish2( ); return false;"></form>

Modern browsers will position form elements

#formID { position: absolute; left: 10px; top: 100px; }

but Netscape 4.x won't, ergo the layer. (However, Netscape 4.x will apply the CSS margin properties to a form, and the fishlink layer can be approximately duplicated by a form { margin-top: 55px; } style rule.)

Controlling properties

For both its left-to-right and right-to-left paths, the Changing the Stacking Order animation moves the fish span/layer

#fish { position: absolute; top: 170px; left: 40px; }
<span id="fish"><img src="images/fish1.gif" name="fish"></span>

across the viewport. The fish layer is given three custom properties that control its direction of movement or the source of the image it contains:
(1) direction
(2) forwardimg
(3) backwardimg
These properties are initially set by an initializeFish( ) function that is called when the button is clicked.

function initializeFish( ) {
    var fish = document.layers["fish"];
    var fishB = document.layers["fishB"];
    fish.direction = "forward";
    fish.forwardimg = fish.document.images["fish"].src;
    fish.backwardimg = fishB.document.images["fishB"].src; return; }

• The direction property is initialized to forward, which signifies left-to-right movement; we'll set direction to backward when it's time to move right to left.

• The forwardimg property points to the fish1.gif image held by the fish layer. In the context of classical JavaScript, the layer object is somewhat like a window object in that it has a child document object that provides access to the same types of layer descendants that a window.document object can access for a body element - images, forms and their controls, links, etc. - this is very briefly discussed in The Document Property of Layers and the Layers Property of Documents subsection of the DHiNC resource. The fish.document.images["fish"].src expression returns the src of the img named fish in the fish layer's document, i.e., scheme://domains/path/fish1.gif (the full URL, not just the file name).

• Similarly, the backwardimg property points to the fish2.gif image held by the fishB layer.

The layer element/object has a src attribute/property for specifying an external file that contains HTML-formatted text to be displayed in the layer. I find that I can load an image into a layer by setting a layer src to an image URL (e.g., fish.src = "fish2.gif"), which would allow us to get rid of the forwardimg/backwardimg properties and layerObject.document.images["imageName"].src expressions, but let's just stick with what we have for the time being.

Layer dynamics

Left to right

Clicking the button also calls a movefish2( ) function, which sends the fish1.gif fish on its way.

function movefish2( ) {
    var fish = document.layers["fish"];
    if (fish.direction == "forward") {
        if (fish.left < 450) { fish.moveBy(5, 0); }
    window.setTimeout("movefish2( );", 10); return; }

After first getting the fish layer and assigning it to a fish variable, the movefish2( ) function tests if the fish layer's direction is equal to forward: check. If the fish layer's left edge is less than 450 pixels to the right of the viewport's left edge, then the fish layer is moved horizontally 5 pixels to the right by a fish.moveBy(5, 0); command. Finally, a setTimeout( ) command re-calls the movefish2( ) function after a 10-millisecond delay to keep the animation going.

Right edge transition

When the fish.left value hits 450, a changePoles( ) function and a changeDirection( ) function are called.

else { changePoles( ); changeDirection( ); }

The changePoles( ) function gets the redpole, bluepole, greenpole, and fish layers and then reverses their z-stacking order via the moveAbove( ) method of the layer object.

function changePoles( ) {
    var redpole = document.layers["redpole"];
    var bluepole = document.layers["bluepole"];
    var greenpole = document.layers["greenpole"];
    var fish = document.layers["fish"];
    greenpole.moveAbove(bluepole); return; }

The moveAbove( ) method moves the calling layer above the argument layer, e.g., fish.moveAbove(redpole); moves the fish layer above the redpole layer. When the changePoles( ) function has finished executing, the redpole layer is on the bottom, the fish layer is above the redpole layer, the bluepole layer is above the fish layer, and the greenpole layer is on top. The layers' left and top offsets are not affected by the moveAbove( ) operations.

At the right edge of the display, the changeDirection( ) function switches the fish layer's direction to backward and loads the fish2.gif image into the fish layer's fish img placeholder.

function changeDirection( ) {
    var fish = document.layers["fish"];
    if (fish.direction == "forward") {
        fish.direction = "backward";
        fish.document.images["fish"].src = fish.backwardimg; } ... }

Right to left

With the fish.direction now backward and the fish.backwardimg in place, the movefish2( ) function tests if the fish.left value is greater than 10; if so, then the fish layer is moved horizontally 5 pixels to the left by a fish.moveBy(-5, 0); command.

else { if (fish.left > 10) { fish.moveBy(-5, 0); } ... }

Right-to-left movement continues via the window.setTimeout("movefish2( );", 10); command.

Left edge transition

When the left edge of the fish.backwardimg is 10 pixels to the right of the viewport's left edge, a resetPoles( ) function is called and the changeDirection( ) function is re-called.

else { resetPoles( ); changeDirection( ); }

Complementing the changePoles( ) function, the resetPoles( ) function regenerates the original fish/pole layer z-stacking order.

function resetPoles( ) {
    var redpole = document.layers["redpole"];
    var bluepole = document.layers["bluepole"];
    var greenpole = document.layers["greenpole"];
    var fish = document.layers["fish"];
    redpole.moveAbove(fish); return; }

At the left edge of the display, the changeDirection( ) function switches the fish.direction back to forward and reloads the fish1.gif image into the fish img.

else {
    fish.direction = "forward";
    fish.document.images["fish"].src = fish.forwardimg; }

Control passes back to the movefish2( ) function, which moves the fish1.gif fish rightward per the Left to right subsection above; and so on, until you leave the page.

If you look at the source of my fishdemo2 demo, you'll see that it's quite a bit different from the fish2css.htm code presented in this entry - I'll go through the fishdemo2 code in the following entry.

It registers zero on the Glycemic index, so many smart consumers worldwide are now using liquid stevia sweetener to enhance the flavor of natural health drinks that do not contain sugar or artificial sweeteners. Essentially, there were at least 18 is really so opened like Samantha. Who doesn't like to save money?
The male enhancement pills are truly perfect for all males who are experiencing reduced sexual drive and lower level of libido. Lets go back to the Law of Attraction and the creative power of your thoughts. One of my challenges (yes, there are many) with cooking is that I've never been able to make a good baked potato. No one needs to read a novel about your birth desires. Allow your family to grieve in peace, and not gain the additional burden of carrying your financial obligation. Understand Jesus is not talking about a church built on power, or money, or social concern. Even if you live in an area that sees a plethora of those little white flakes, you know it takes ideal conditions to make a snow man or a snow ball. This is one of the lost arts and few people are able to offer this technique today. This latest tour will take him across the United States and Canada, and follows Cohen's latest album Old Ideas that was released earlier this year.

If you are purchasing an unframed work directly from the artist it never hurts to ask if they have a connection to get you a good priced frame. Naturally, there may be pain in and around the incision site and a risk of infection of the incision, but if you follow your the instructions your doctor gives you, those risks should be limited. if you plan to visit Egypt & make a trip to Coptic cairo and khan elkhaliliy bazars ,here is the contact and address information Simply collapse it and store it in the flat drawer.
Post a Comment

<< Home

Powered by Blogger

Actually, reptile7's JavaScript blog is powered by Café La Llave. ;-)