<DIV id="trailSprite1" style="position: absolute; height:28px; width:36px;"> <img src="trailgif1.gif" height=28 width=36 border=0> </DIV>
References to these sprites ID are stored in an array as a string. The ID's are named trailsprite1, trailsprite2, trailsprite3, etc. This array is treated like a kind of 'queue'. When the mouse moves, the last sprite ID in the array is positioned closest to the mouse's current position (there's a slight offset so the trail doesn't cover up anything clickable). The array is then shifted by 1 element so this last reference becomes the first. This process continues so the queue is 'looped' around on its self and therefore there's alway's a sprite available for the trail.
Next we will show you step by step how we created this effect.
Page 1:Cursor Trails
Page 2:Trails Demonstration
Page 3:Creating Cursor Trails
Page 4:Improving the Trail
© 1997-2000 InsideDHTML.com, LLC. All rights reserved.
|
||
|
| Inside Technique : Cursor Trails : Creating Cursor Trails Forgetting about the fade for the moment the code to achieve a simple trail runs like this:
This this is a basic trail, but it kind of gets in the way of whats on the page. Next we improve the trail to constantly fade. The fade consists of a five frame animation of a trail part getting gradually smaller before dissapearing completely. The animation for a sprite is started when it's moved closest to the mouse. This gives the effect of the trail graduating away as the mouse is moved and disappearing completely when mouse movement is stopped. Page 1:Cursor Trails © 1997-2000 InsideDHTML.com, LLC. All rights reserved. |
|
||
|
| Inside Technique : Cursor Trails : Improving the Trail First, five GIF's needed to be created for the animation. All are the same dimensions but with an increasing amount of transparent area and the dot (in this case) gets smaller. These sprites will need to be preloaded. The filenames of the sprites are trailgif1.gif, trailgif2.gif, trailgif3.gif, etc. A name attribute also needs to be added to each sprite img tag so the scripting can reference it. Compared to the basic trail, the scripting becomes a more complicated when it comes to the adding the fade. The queue now has to track not just a string reference to each sprites ID, but also its current frame and, if its in an active state, look after the animation. This is ideal scenario for using a basic Javascript Object. Rather than filling the queue array with
just strings that reference layers, we'll fill the array with objects that reference not only the sprites
ID, but also a reference to the sprites image and the sprites current frame of fade animation.
This simple object will also have one method which performs and looks after the animation process.
Here is the trail object:
The queue array is now filled with instances of this object:
The Object is constructed with just 1 parameter, a number related to the sprites ID in the HTML. Another function is also introduced which fires off each objects 'animateTrailSprite' function. The processAnim fuction
shown below simply calls each objects cycle function. 'processAnim' is added to a 'setinterval' function
so it is called regularly.
Now if this code is run, the mouse trail will just simply disappear and not reappear. To fix this, a couple of
extra lines need to be added to the queue shufferling function. Now the sprite its
getting from the back of the queue and moving closest to the mouse has its animation reset, by making it visible
and setting its curent frame to 1 . The lines are:
And thats it - now the trail now quite happily fades away. Now the final thing to do is to make it work with older, less dynamic, browsers. Because in the script tag we've told the browser its version 1.2 Javascript older browsers will ignore it. The simplest way to avoid the images between the div tag appearing in older browsers is to put the tags at the very bottom of your page and replace the dot images with a blank transparent gif, with the same dimensions as a trail part. Using a <span> tag rather than a <div> will also stop the page from trailing off to long (A div causes a line break, whilst a span doesn't). About the AuthorMatthew Allum is a 23 year old programmer from the UK. He works on the websites for a large consumer magazine publisher located at www.erack.com. Apart from coding in various Internet technologies, he also enjoys cruising in his Karmann Ghia, collecting obscure records and just hanging out. His home page is located at http://hopskotch.eimages.co.uk. Page 1:Cursor Trails © 1997-2000 InsideDHTML.com, LLC. All rights reserved. |