DHTML : Text with wave effects
Mickaël le Moal
Animation
Let's deal with the animation. All possible parameters and positions for our various layers have already been defined by the
lance() function. We need to assign a coordinate to a layer and to update the coordinate in order to produce the animation. This
is child's play!
49: function deplace()
50: {
51: for (i=0;i<nb_obj;i++) {
52: j = pos[i];
53: l[i].top = cy[j];
54: pos[i] += dy[i];
55: if (pos[i]>19) {
56: pos[i] = 19;
57: dy[i] = -1;
58: }
59: else if (pos[i]<1) {
60: pos[i] = 0;
61: dy[i] = 1;
62: }
63: }
64: setTimeout ("deplace()",50);
65: }
The new position of each layer is determined on line 54 by adding its current position (array
pos) to its direction of movement(array
dy). Since the layer position must be equal to or greater than 0 but lower than 20, we need to check it (lines 55-62) in order
to invert the direction of movement and to maintain the layer's position between our limits.
Given this position and the vertical coordinate of the layer (array
cy), we redisplay the layer at this new coordinate (lines 52 and 53).
All that is left to do is to call up this function indefinitely to produce our animation. The JavaScript
setTimeout function undertakes this task (line 64) and calls up the
deplace() function every 50 milliseconds.
In this way, each character will pass through the twenty positions in one direction, then in the other, with an offset of
two positions relative to its neighbouring letters, thus producing the expected effect: a wave!
Customise the effect! To animate a text of any length, the number of elements of the
pos and
dy arrays must be the same as the number of characters composing the text.
For instance, the values of the two arrays for displaying a text of 15 characters are as follows:
37: pos = new Array(0,2,4,6,8,10,12,14,16,18,18,16,14,12,10);
38: dy = new Array(1,1,1,1,1,1,1,1,1,1,-1,-1,-1,-1,-1);
A wavy text wave