home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 May / comcd0502.iso / homepage / special / javascript / 01_01 / Java / fireworks / fireworks2.js < prev    next >
Encoding:
JavaScript  |  2000-06-20  |  3.6 KB  |  162 lines

  1. /*******************************************************************
  2. *
  3. * File    : fireworks2.js (NOTE: requires animate2.js!!)
  4. *
  5. * Created : 2000/06/05
  6. *
  7. * Author  : Roy Whittle  (Roy@Whittle.com) www.Roy.Whittle.com
  8. *
  9. * Purpose : To create animated fireworks on your desktop
  10. *
  11. * History
  12. * Date         Version        Description
  13. *
  14. * 2000-06-05    1.0        This has to be my best script to date.
  15. *                    This is the culmination of all the knowledge
  16. *                    I have learned over the past 10 Months doing
  17. *                    Javascript.
  18. *            1.1        Fixed a bug in Netscape resize.
  19. ***********************************************************************/
  20. /*** Start - xLayer - a cross browser layer object by www.Roy.Whittle.com ***/
  21. var xLayerNo=0;
  22. function xLayer(xHtml)
  23. {
  24.     var xName="xLayer" + xLayerNo++;
  25.  
  26.     if(document.layers)
  27.     {
  28.         this.layer=new Layer(100);
  29.         this.layer.document.open()
  30.         this.layer.document.write(xHtml)
  31.         this.layer.document.close()
  32.     }
  33.     else
  34.     if(document.all)
  35.     {
  36.         txt =   "<DIV ID='" + xName
  37.             + "' STYLE=\"position:absolute;left:100;top:100;width:100;height:100;visibility:hidden\">"
  38.             + xHtml + "</DIV>";
  39.         document.body.insertAdjacentHTML("BeforeEnd",txt);
  40.         this.element = document.all[xName];
  41.         this.layer   = document.all[xName].style;
  42.     }
  43.  
  44.     this.show       = show;
  45.     this.moveTo     = moveTo;
  46.     this.setContent = setContent;
  47.  
  48.     return(this);
  49. }
  50. function show()
  51. {
  52.     if(document.layers)
  53.         this.layer.visibility="show";
  54.     else
  55.     if(document.all)
  56.         this.layer.visibility="visible";
  57. }
  58. function moveTo(x,y)
  59. {
  60.     if(document.layers)
  61.         this.layer.moveTo(x,y);
  62.     else
  63.     if(document.all)
  64.     {
  65.         this.layer.pixelLeft = x;
  66.         this.layer.pixelTop  = y;
  67.     }    
  68. }
  69. function setContent(xHtml)
  70. {
  71.     if(document.layers)
  72.     {
  73.         this.layer.document.open()
  74.         this.layer.document.write(xHtml)
  75.         this.layer.document.close()
  76.     }
  77.     else 
  78.     if(document.all)
  79.     {
  80.         this.element.innerHTML=xHtml;
  81.     }
  82. }
  83. /*** End  - xLayer - a cross browser layer object by www.Roy.Whittle.com ***/
  84.  
  85. var fwLayer = new Array();
  86. var scrWidth=600;
  87. var scrHeight=400;
  88. var ow;
  89. var oh;
  90. var theTimer;
  91. var layerNo=0;
  92. var imgBase="";
  93. var MAX_FIREWORKS=16;
  94.  
  95. BaseHref=imgBase+"images_";
  96. Sep="_";
  97.  
  98. function moveIt()
  99. {
  100.     var x=Math.floor(Math.random()*scrWidth);
  101.     var y=Math.floor(Math.random()*scrHeight);
  102.     var z=Math.floor(Math.random()*4);
  103.  
  104.     var myLayer=fwLayer[layerNo];
  105.  
  106.     myLayer.moveTo(x,y);
  107.     myLayer.show();
  108.     turn_on("fw"+layerNo, "firework"+z);
  109.     
  110.     layerNo=(layerNo+1)%MAX_FIREWORKS;
  111.  
  112.     theTimer=setTimeout("moveIt()", Math.floor(Math.random()*(1600/MAX_FIREWORKS) ) );
  113. }
  114. function setSize()
  115. {
  116.     if(document.layers)
  117.     {
  118.         ow=window.outerWidth;
  119.         oh=window.outerHeight;
  120.         scrWidth =window.innerWidth;
  121.         scrHeight=window.innerHeight;
  122.     }
  123.     else if(document.all)
  124.     {
  125.         scrWidth  = document.body.clientWidth;
  126.         scrHeight = document.body.clientHeight;
  127.     }
  128.     scrWidth  -= 100;
  129.     scrHeight -= 100;
  130. }
  131. AnimationFrames("firework0", 20, ".gif");
  132. function fireworks()
  133. {
  134.     var i=0;
  135.     setSize();
  136.  
  137.     AniFrame["firework1"]=AniFrame["firework0"];
  138.     AniFrame["firework2"]=AniFrame["firework1"];
  139.     AniFrame["firework3"]=AniFrame["firework2"];
  140.  
  141.     for(i=0 ; i<MAX_FIREWORKS; i++)
  142.     {
  143.         AnimatedImage("fw"+i, "firework0");
  144.         fwLayer[i]=new xLayer("<IMG SRC='"+imgBase+"images_firework0_0.gif' NAME='fw"+i+"'>");
  145.         fwLayer[i].fwNo=0;
  146.     }
  147.  
  148.     theTimer=setTimeout("moveIt()", 1000 );
  149. }
  150. function handle_resize()
  151. {
  152.     if(   document.layers 
  153.        && (   ow != window.outerWidth
  154.            || oh != window.outerHeight) )
  155.     {
  156.         clearTimeout(theTimer);
  157.         location.reload();
  158.     }
  159.     setSize();
  160. }
  161. window.onload=fireworks;
  162. window.onresize=handle_resize;