home *** CD-ROM | disk | FTP | other *** search
/ ftp.anonawest.com / ftp.anonawest.com.tar / ftp.anonawest.com / JSFX_Halloween.js < prev    next >
Text File  |  2013-03-02  |  7KB  |  255 lines

  1. /*******************************************************************
  2. *
  3. * File    : JSFX_Halloween.js ⌐ JavaScript-FX.com
  4. *
  5. * Created : 2001/10/16
  6. *
  7. * Author  : Roy Whittle www.Roy.Whittle.com
  8. *           
  9. * Purpose : To create animated Ghosts on the page using any user 
  10. *        supplied image.
  11. *
  12. * History
  13. * Date         Version        Description
  14. *
  15. * 2001-10-16    1.0        Created for javascript-fx
  16. ***********************************************************************/
  17. /*** START OF CODE FROM JSFX_Layer.js ***/
  18. if(!window.JSFX)
  19.     JSFX=new Object();
  20. JSFX.layerNo=0; 
  21. /**********************************************************************************/
  22. JSFX.createElem = function(htmlStr)
  23. {
  24.     var elem = null;
  25.  
  26.      if(document.layers) 
  27.     {
  28.         elem=new Layer(2000); 
  29.         elem.document.open(); 
  30.         elem.document.write(htmlStr); 
  31.         elem.document.close(); 
  32.         elem.innerHTML = htmlStr;
  33.     }
  34.     else 
  35.     if(document.all) 
  36.     {
  37.         var xName = "xLayer" + JSFX.layerNo++; 
  38.         var txt = "<DIV ID='" + xName 
  39.             + "' STYLE=\"position:absolute;" 
  40.             + "visibility:hidden\">" 
  41.             + htmlStr 
  42.             + "</DIV>"; 
  43.  
  44.             document.body.insertAdjacentHTML("BeforeEnd",txt); 
  45.  
  46.         elem = document.all[xName]; 
  47.     } 
  48.     else 
  49.     if (document.getElementById)
  50.     {
  51.         var xName="xLayer" + JSFX.layerNo++;
  52.         var txt = ""
  53.             + "position:absolute;"
  54.             + "visibility:hidden";
  55.  
  56.         var newRange = document.createRange();
  57.  
  58.         elem = document.createElement("DIV");
  59.         elem.setAttribute("style",txt);
  60.         elem.setAttribute("id", xName);
  61.  
  62.         document.body.appendChild(elem);
  63.  
  64.         newRange.setStartBefore(elem);
  65.         strFrag = newRange.createContextualFragment(htmlStr);    
  66.         elem.appendChild(strFrag);
  67.     }
  68.  
  69.     return elem;
  70. }
  71. /**********************************************************************************/
  72. JSFX.Layer = function(newLayer) 
  73. {
  74.     if(!newLayer)
  75.         return;
  76.  
  77.     if(typeof newLayer == "string")
  78.         this.elem = JSFX.createElem(newLayer);
  79.     else
  80.         this.elem=newLayer;
  81.  
  82.     if(document.layers)
  83.     {
  84.         this.images=this.elem.document.images; 
  85.         this.style = this.elem;
  86.      } 
  87.     else 
  88.     {
  89.         this.images  = document.images; 
  90.         this.style   = this.elem.style; 
  91.     } 
  92. /**********************************************************************************/
  93. var ns4 = (navigator.appName.indexOf("Netscape") != -1 && !document.getElementById);
  94.  
  95. /**********************************************************************************/
  96. /*** moveTo (x,y) ***/
  97. JSFX.Layer.prototype.moveTo = function(x,y)
  98. {
  99.     this.style.left = x+"px";
  100.     this.style.top = y+"px";
  101. }
  102. if(ns4)
  103.     JSFX.Layer.prototype.moveTo = function(x,y) { this.elem.moveTo(x,y); }
  104. /**********************************************************************************/
  105. /*** show()/hide() Visibility ***/
  106. JSFX.Layer.prototype.show        = function()     { this.style.visibility = "visible"; } 
  107. JSFX.Layer.prototype.hide        = function()     { this.style.visibility = "hidden"; } 
  108. if(ns4)
  109. {
  110.     JSFX.Layer.prototype.show        = function()     { this.style.visibility = "show"; }
  111.     JSFX.Layer.prototype.hide         = function()     { this.style.visibility = "hide"; }
  112. }
  113. /**********************************************************************************/
  114. /*** Opacity ***/
  115. if(document.all)
  116. {
  117.     JSFX.Layer.prototype.setOpacity = function(pc)
  118.     {
  119.         if(this.style.filter=="")
  120.             this.style.filter="alpha(opacity=100);";
  121.         this.elem.filters.alpha.opacity=pc;
  122.     }
  123. }
  124. else
  125.     JSFX.Layer.prototype.setOpacity = function(pc) {return 0;}
  126. /******* END OF CODE FROM JSFX.Layer.js ***/
  127. /*** START OF CODE FROM JSFX.Browser.js ***/
  128. JSFX.Browser = new Object();
  129.  
  130. if(navigator.appName.indexOf("Netscape") != -1)
  131. {
  132.     JSFX.Browser.getCanvasWidth    = function() {return innerWidth;}
  133.     JSFX.Browser.getCanvasHeight    = function() {return innerHeight;}
  134.     JSFX.Browser.getMinX        = function() {return(pageXOffset);}
  135.     JSFX.Browser.getMinY        = function() {return(pageYOffset);}
  136.     JSFX.Browser.getMaxX        = function() {return(pageXOffset+innerWidth);}
  137.     JSFX.Browser.getMaxY        = function() {return(pageYOffset+innerHeight);}
  138. }
  139. else     if(document.all)
  140. {
  141.     JSFX.Browser.getCanvasWidth    = function() {return document.body.clientWidth;}
  142.     JSFX.Browser.getCanvasHeight    = function() {return document.body.clientHeight;}
  143.     JSFX.Browser.getMinX        = function() {return(document.body.scrollLeft);}
  144.     JSFX.Browser.getMinY        = function() {return(document.body.scrollTop);}
  145.     JSFX.Browser.getMaxX        = function() {
  146.         return(document.body.scrollLeft
  147.             +document.body.clientWidth);
  148.     }
  149.     JSFX.Browser.getMaxY        = function() {
  150.             return(document.body.scrollTop
  151.                 +document.body.clientHeight);
  152.     }
  153. /*** END OF CODE FROM JSFX.Browser.js ***/
  154. /*** START OF CODE FROM JSFX.Ghosts.js ***/
  155.  
  156. JSFX.Halloween = new Object();
  157. JSFX.Halloween.Ghosts = new Array();
  158. JSFX.Halloween.start = function()
  159. {
  160.     if(JSFX.Halloween.theTimer == null)
  161.     {
  162.         JSFX.Halloween.theTimer = setTimeout("JSFX.Halloween.animateAll()", 40);
  163.     }
  164. }
  165. JSFX.Halloween.animateAll = function()
  166. {
  167.     JSFX.Halloween.theTimer = setTimeout("JSFX.Halloween.animateAll()", 40);
  168.     var sp = JSFX.Halloween.Ghosts;
  169.     var i;
  170.     for(i=0 ; i<sp.length ; i++)
  171.     {
  172.         sp[i].animate();
  173.     }
  174.  
  175. }
  176. JSFX.AddGhost = function(img)
  177. {
  178.     var myGhost = null;
  179.     var htmlStr = "<IMG SRC='"+img+"'>";
  180.  
  181.     myGhost = new JSFX.Layer(htmlStr);
  182.     myGhost.op = 0;
  183.     myGhost.x = Math.random()*JSFX.Browser.getMaxX();
  184.     myGhost.y = Math.random()*JSFX.Browser.getMaxY();
  185.     myGhost.dx = 0;
  186.     myGhost.dy = 0;
  187.     myGhost.w = 30;
  188.     myGhost.h = 30;
  189.     myGhost.targetX = Math.random()*JSFX.Browser.getMaxX();
  190.     myGhost.targetY = Math.random()*JSFX.Browser.getMaxY();
  191.     myGhost.state = "off"
  192.     myGhost.animate = JSFX.animateGhosts;
  193.     myGhost.hide();
  194.     myGhost.setOpacity(this.op);
  195.     myGhost.moveTo(myGhost.x,myGhost.y);
  196.     JSFX.Halloween.Ghosts[JSFX.Halloween.Ghosts.length] = myGhost;
  197.     JSFX.Halloween.start();
  198. }
  199. JSFX.animateGhosts = function()
  200. {
  201.     if(this.state == "off")
  202.     {
  203.         if(Math.random() > .99)
  204.         {
  205.             this.state="up";
  206.             this.show();
  207.         }
  208.     }
  209.     else if(this.state == "on")
  210.     {
  211.         if(Math.random() > .98)
  212.             this.state="down";
  213.     }
  214.     else if(this.state == "up")
  215.     {
  216.         this.op += 2;
  217.         this.setOpacity(this.op);
  218.         if(this.op==100)
  219.             this.state = "on";
  220.     }
  221.     else if(this.state == "down")
  222.     {
  223.         this.op -= 2;
  224.         if(this.op==0)
  225.         {
  226.             this.hide();
  227.             this.state = "off";
  228.         }
  229.         else
  230.             this.setOpacity(this.op);
  231.     }
  232.  
  233.     m = this;
  234.     var X = (this.targetX - m.x);
  235.     var Y = (this.targetY - m.y);
  236.     var len = Math.sqrt(X*X+Y*Y);
  237.     if(len < 1) len = 1;
  238.     var dx = 20 * (X/len);
  239.     var dy = 20 * (Y/len);
  240.     var ddx = (dx - this.dx)/10;
  241.     var ddy = (dy - this.dy)/10;
  242.     this.dx += ddx;
  243.     this.dy += ddy;
  244.     m.x += this.dx;
  245.     m.y += this.dy;
  246.     m.moveTo(m.x, m.y);
  247.     if(Math.random() >.95 )
  248.     {
  249.         this.targetX = Math.random()*(JSFX.Browser.getCanvasWidth()-150);
  250.         this.targetY = Math.random()*(JSFX.Browser.getCanvasHeight()+JSFX.Browser.getMinY()-150);
  251.     }
  252. }
  253.