|
||||||
As Christmas approaches it's time to thing about putting up those decorations - this simple tutorial shows how to add a flurry of snow to any web site.
Every year the preparations for Christmas start earlier and earlier:
Therefore, of course, it is essential that all web sites should be able to do the same - and a nice decoration for any web site is some seasonal snowflakes. Any web site designers have two options, they can:
In either case the first step is to create an image of a snowflake. Creating Snowflakes for a Web PageOne of the most beautiful sights is a snowflake and, although they look complex, their geometry is actually very simple - a recognisable snowflake can be created by using an icon editor such as KIconEdit to:
The icon then needs to saved onto the web server as (for example) snowflake.png. Creating a Static DisplayThere are a few things to bear in mind when adding static snowflakes to a web page:
Fortunately this can all be achieved by using standard HTML:
The HTML code for a web page with static snowflakes will be something like the following. <body bgcolor=blue>
<div style=position:absolute;z-index:1>
<h1>Static Snowflakes</h1>
</div>
<img id=snowflake1 src=snowflake.png
style=position:absolute>
<img id=snowflake2 src=snowflake.png
width=16 height=16
style=position:absolute;left:50;top:50>
</body>
Creating an Animated DisplayThe animated snow scene is basically the same as the static one - the web page consists of its normal contents: <div style=position:absolute;z-index:1>
<h1>Using Javascript to Animate Snowflakes</h1>
</div>
However, the position of the snowflakes will be controlled by Javascript code. <script>
In the static example a number of snowflakes were added manually and their size set individually - Javascript will do all of that with code and so the programmer must start by defining how many snowflakes are to be shown and how big (in pixels) they will be by default: var slumber = 25;
var max_size = 36;
A simple Javascript loop will add all of the images required by the page (giving each a unique ID as it does so): for (var s = 1; s < slumber; s++ ) {
document.write (
"<img id=snowflake" +
s +
" src=snowflake.png style=position:absolute>"
);
}
Since the code to move the snowflakes will be used a lot the most sensible thing to do is to encapsulate it in a function: function move_snowflake
(snowflake_id, top, left, speed, size, max_size) {
var snowflake = document.getElementById(snowflake_id);
The function updates the style.top property with the value that's been fed to it: snowflake.style.top = top;
and then increases the input value by 1 pixel (which actually means one pixel further down the page - positon 0,0 is in the top left corner of the screen): var top = top + 1;
When snowflake reaches the bottom of the page then the function will move it back to the top, but will now choose a new random left value (so that it doesn't just start from the same position each time): if (top > document.body.clientHeight - 36) {
top = 0;
var rand_no = Math.random();
rand_no = rand_no * 100;
var left = document.body.clientWidth * rand_no / 100;
The function will also choose a new random size for the snowflake: rand_no = Math.random();
var size = max_size * parseInt( rand_no * 100 ) / 100;
A new speed of descent will now be calculated - this will be related to the size so that smaller snowflakes will fall more slowly (to give a 3-D effect): var speed = 500 * (100 - parseInt( rand_no * 100 )) / 100;
}
The snowflake's properties can now be updated: snowflake.style.left = left;
snowflake.width = size;
snowflake.height = size;
And finally the setTimeout method is used to add a timer - this sets when the position of the snowflake is to be updated again: setTimeout (
'move_snowflake("'
+ snowflake_id + '",'
+ top + ',' + left + ','
+ speed + ',' + size + ',' + max_size + ')'
, speed );
}
The final step of the whole process is to set the snowflakes into motion: for (var s = 1; s < slumber; s++ ) {
move_snowflake(
"snowflake" + s,
document.body.clientHeight,
0, s*100, max_size, max_size
);
}
</script>
ConclusionThe end result is a simple yet effective Christmas decoration for any web site - achieved with just a single image and a little bit of Javascript code.
The copyright of the article How to Use Javascript to Animate Snowflakes in Javascript/Java Programming is owned by Mark Alexander Bain. Permission to republish How to Use Javascript to Animate Snowflakes in print or online must be granted by the author in writing.
Comments
Nov 27, 2008 1:30 PM
Guest :
Nov 27, 2008 1:57 PM
Mark Alexander Bain :
2 Comments
|
||||||
|
|
||||||
|
|
||||||