473,385 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

snow script


I use a JavaScript to generate snowflakes on a HTML page.
I want to calculate the size of the snowflakes by using th
Math.random() function to get a great variety of snow flakes. I jus
can't find the right method to do so.

The line where it's all about (to my opinion) is:

document.all["dot"+i].scaleX = document.all["dot"+i].scaleY
20+Math.random()*30;

<script language="JavaScript1.2">

var number = 30; // number of snowflakes
var speed = 20; //lower number=faster snowflakes
var snowflake = "snow4.gif";

var ns4up = (document.layers) ? 1 : 0;
var ie4up = (document.all) ? 1 : 0;
var dx, xp, yp; // coordinates and position of variables
var am, stx, sty; // amplitude and steps of variables
var i, doc_width = 800, doc_height = 600;

if (ie4up) {
doc_width = document.body.clientWidth;
doc_height = document.body.clientHeight;
}

dx = new Array();
xp = new Array();
yp = new Array();
am = new Array();
stx = new Array();
sty = new Array();

for (i = 0; i < number; ++ i) {
dx[i] = 0; // set coordinate variables
xp[i] = Math.random()*(doc_width-50); // set position on X and Y
yp[i] = Math.random()*doc_height;
am[i] = Math.random()*20; // set amplitude
stx[i] = 0.02 + Math.random()/10; // set number steps X
sty[i] = 0.7 + Math.random(); // set number steps Y
if (ie4up) { // set div voor IE
if (i == 0) {
document.write("<div id=\"dot"+ i +"\" style=\"POSITION:absolute
Z-INDEX: "+ i +"; VISIBILITY:visible; TOP: 15px; LEFT: 15px;\"><im
src='"+snowflake+"' border=\"0\"></div>");
} else {
document.write("<div id=\"dot"+ i +"\" style=\"POSITION:absolute
Z-INDEX: "+ i +"; VISIBILITY:visible; TOP: 15px; LEFT: 15px;\"><im
src='"+snowflake+"' border=\"0\"></div>");
}
}
}
function snowIE() { // IE main animation function
for (i = 0; i <number; ++ i) { // iterate for every dot
yp[i] += sty[i];

if (yp[i] > doc_height-50) {
xp[i] = Math.random()*(doc_width-am[i]-30);
yp[i] = 0;
stx[i] = 0.02 + Math.random()/10;
sty[i] = 0.7 + Math.random();
doc_width = document.body.clientWidth;
doc_height = document.body.clientHeight;
document.all["dot"+i].scaleX = document.all["dot"+i].scaleY
20+Math.random()*30;
}
dx[i] += stx[i];
document.all["dot"+i].style.pixelTop = yp[i];
document.all["dot"+i].style.pixelLeft = xp[i] + am[i]*Math.sin(dx[i]);

}
setTimeout("snowIE()", speed);
}

if (ns4up) {
snowNS();
} else if (ie4up) {
snowIE();
}

</script>

Thnx

Arwe

arwe
-----------------------------------------------------------------------
Posted via http://www.forum4designers.co
-----------------------------------------------------------------------
View this thread: http://www.forum4designers.com/message28165.htm

Jul 20 '05 #1
2 2069
arwes <ar*********@mail.forum4designers.com> writes:
I use a JavaScript to generate snowflakes on a HTML page.
I want to calculate the size of the snowflakes by using the
Math.random() function to get a great variety of snow flakes. I just
can't find the right method to do so.

The line where it's all about (to my opinion) is:

document.all["dot"+i].scaleX = document.all["dot"+i].scaleY =
20+Math.random()*30;
I would probably round it to an integer, i.e.,
Math.floor(20+Math.random()*30)
That gives a integer in the range 20-49 inclusive.

Also, the properties "scaleX" and "scaleY" are not used anywhere, and
they don't do anything in themselves.
Try:
var dotStyle = document.all["dot"+i].style;
dotStyle.width = dotStyle.height = Math.floor(20+Math.random()*30)+"px";
<script language="JavaScript1.2">
<script type="text/javascript">

In HTML 4, the type attrribute is *required*. The language attribute
is never needed, and if you use it, at least use another version
number than 1.2 (using JavaScript1.2 can make Netscape go into 1.2
compatability mode, which has some subtle but annoying differences
from normal Javascript ... there is a reason 1.2 was only the default
for Netscape 4.0 to 4.05, then they changed to 1.3).
var number = 30; // number of snowflakes
var speed = 20; //lower number=faster snowflakes
var snowflake = "snow4.gif";

var ns4up = (document.layers) ? 1 : 0;
var ie4up = (document.all) ? 1 : 0;


Warning! A sure sign of a script written by someone who is not very
good at Javscript, is that they use numbers to represent boolean
values. Javascript has "true" and "false" built in. It is not C.

Also, this code is obviously from back when Netscape 4 and IE were the
only browsers. Not so any more, and this script will fail in, e.g.,
Mozilla/Netscape 6+

I'm pretty sure I have seen this script before here, and I recommended
finding a more up-to-date script. This one should be dead and buried
long ago.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
Lasse Reichstein Nielsen wrote:
Also, the properties "scaleX" and "scaleY" are not used anywhere, and
they don't do anything in themselves.
Try:
var dotStyle = document.all["dot"+i].style;
dotStyle.width = dotStyle.height =
Math.floor(20+Math.random()*30)+"px";


This will not resize the image, only the containing <div />.

You can fix this by adding a name to the image , e.g. `dotimage + i`, and
then access it through the document.images property:

document.images['dotimage'+i].style.width =
document.images['dotimage'+i].style.height =
(20+Math.random()*30) + 'px';

Or simply by referencing a DOM object (which requires IE version 5.5+):

document.all["dot"+i].firstChild.style.width =
document.all["dot"+i].firstChild.style.height =
(20+Math.random()*30) + 'px';

Apart from this, I agree with you that the OP should search for an updated
script.
JW

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Mike Daniel | last post by:
I am attempting to use document.write(pageVar) that displays a new html page within a pop-up window and the popup is failing. Also note that pageVar is a complete HTML page containing other java...
1
by: JKD | last post by:
I'm looking for a script in javascript that simulates the snow effect on my web site. I've searched in google but scripts I found use applets or move images on the screen. I had a javascript with a...
5
by: toronto | last post by:
I'm stumped. I'm playing with a javascript that produces falling snowflakes on screen. I got the script from this author: http://thelocust.org/projects/snow2/ If I hotlink to the exploding flake...
3
by: Water Cooler v2 | last post by:
Questions: 1. Can there be more than a single script block in a given HEAD tag? 2. Can there be more than a single script block in a given BODY tag? To test, I tried the following code. None...
2
by: bilaribilari | last post by:
Hi all, I am using Tidy (C) for parsing html pages. I encountered a page that has some script as follows: <script> .... var abc = "<script>some stuff here</" + "script>"; .... </script>
19
by: thisis | last post by:
Hi All, i have this.asp page: <script type="text/vbscript"> Function myFunc(val1ok, val2ok) ' do something ok myFunc = " return something ok" End Function </script>
3
by: rsteph | last post by:
I have a script that shows the time and date. It's been working on my site for quite a while now. Suddenly it stops showing up, after getting my drop down menu to work. If I put text between the...
1
by: stevesnow | last post by:
---------------------------------------------- Please forward this work experience & skills summary to your Database & software development, MIS/IT/Software Department for review. ...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.