Advertisement
tremywoods

Fireflies in a Jar

Jul 13th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. <script type="text/javascript">
  2. // <![CDATA[
  3. var colour="#CEF6D8"; // what colour are the blobs
  4. var speed=70; // speed of animation, lower is faster
  5. var blobs=18; // how many blobs are in the jar
  6. var charc="*"; // a blob - can be changed to charc='hello' or charc='*' for a different effect
  7.  
  8. /***************************\
  9. * Blobs in a Jar Effect *
  10. *(c)2012-13 mf2fm web-design*
  11. * http://www.mf2fm.com/rv *
  12. * DON'T EDIT BELOW THIS BOX *
  13. \***************************/
  14.  
  15. var div;
  16. var xpos=new Array();
  17. var ypos=new Array();
  18. var zpos=new Array();
  19. var dx=new Array();
  20. var dy=new Array();
  21. var dz=new Array();
  22. var blob=new Array();
  23. var swide=800;
  24. var shigh=600;
  25. var ie_version=(navigator.appVersion.indexOf("MSIE")!=-1)?parseFloat(navigator.appVersion.split("MSIE")[1]):false;
  26.  
  27. function addLoadEvent(funky) {
  28. var oldonload=window.onload;
  29. if (typeof(oldonload)!='function') window.onload=funky;
  30. else window.onload=function() {
  31. if (oldonload) oldonload();
  32. funky();
  33. }
  34. }
  35.  
  36. addLoadEvent(fill_the_jar);
  37.  
  38. function fill_the_jar() {
  39. var i, dvs;
  40. div=document.createElement('div');
  41. dvs=div.style;
  42. dvs.position='fixed';
  43. dvs.left='0px';
  44. dvs.top='0px';
  45. dvs.width='1px';
  46. dvs.height='1px';
  47. document.body.appendChild(div);
  48. set_width();
  49. for (i=0; i<blobs; i++) {
  50. add_blob(i);
  51. jamjar(i);
  52. }
  53. }
  54.  
  55. function add_blob(ref) {
  56. var dv, sy;
  57. dv=document.createElement('div');
  58. sy=dv.style;
  59. sy.position='absolute';
  60. sy.textAlign='center';
  61. if (ie_version && ie_version<10) {
  62. sy.fontSize="10px";
  63. sy.width="100px";
  64. sy.height="100px";
  65. sy.paddingTop="40px";
  66. sy.color=colour;
  67. dv.appendChild(document.createTextNode(charc));
  68. }
  69. else if (ie_version) {
  70. sy.fontSize="1px";
  71. sy.width="0px";
  72. sy.height="0px";
  73. }
  74. else {
  75. dv.appendChild(document.createTextNode(charc));
  76. sy.color='rgba(0,0,0,0)';
  77. }
  78. ypos[ref]=Math.floor(shigh*Math.random());
  79. dy[ref]=(0.5+Math.random())*(Math.random()>.5?2:-2);
  80. xpos[ref]=Math.floor(swide*Math.random());
  81. dx[ref]=(0.5+Math.random())*(Math.random()>.5?2:-2);
  82. zpos[ref]=Math.random()*20;
  83. dz[ref]=(0.5+Math.random())*(Math.random()>.5?.5:-.5);
  84. blob[ref]=dv;
  85. div.appendChild(blob[ref]);
  86. set_blob(ref);
  87. }
  88.  
  89. function rejig(ref, xy) {
  90. if (xy=='y') {
  91. dx[ref]=(0.5+Math.random())*sign(dx[ref]);
  92. dy[ref]=(0.5+Math.random())*-sign(dy[ref]);
  93. }
  94. else {
  95. dx[ref]=(0.5+Math.random())*-sign(dx[ref]);
  96. dy[ref]=(0.5+Math.random())*sign(dy[ref]);
  97. }
  98. }
  99.  
  100. function sign(a) {
  101. if (a<0) return (-2);
  102. else if (a>0) return (2);
  103. else return (0);
  104. }
  105.  
  106. function set_blob(ref) {
  107. var sy;
  108. sy=blob[ref].style;
  109. sy.top=ypos[ref]+'px';
  110. sy.left=xpos[ref]+'px';
  111. if (ie_version && ie_version<10) {
  112. sy.filter="glow(color="+colour+",strength="+zpos[ref]+")";
  113. sy.fontSize=30-zpos[ref]+"px";
  114. }
  115. else if (ie_version) {
  116. sy.boxShadow="0px 0px 40px "+zpos[ref]+"px "+colour;
  117. }
  118. else {
  119. sy.textShadow=colour+' 0px 0px '+zpos[ref]+'px';
  120. sy.fontSize=40+zpos[ref]+'px';
  121. }
  122. }
  123.  
  124. function jamjar(ref) {
  125. if (ypos[ref]+dy[ref]<-50 || ypos[ref]+dy[ref]>shigh) rejig(ref, 'y');
  126. ypos[ref]+=dy[ref];
  127. if (xpos[ref]+dx[ref]<-50 || xpos[ref]+dx[ref]>swide) rejig(ref, 'x');
  128. xpos[ref]+=dx[ref];
  129. if (zpos[ref]+dz[ref]<0 || zpos[ref]+dz[ref]>20) dz[ref]=-dz[ref];
  130. zpos[ref]+=dz[ref];
  131. set_blob(ref);
  132. setTimeout("jamjar("+ref+")", speed);
  133. }
  134.  
  135. window.onresize=set_width;
  136. function set_width() {
  137. var sw_min=999999;
  138. var sh_min=999999;
  139. if (document.documentElement && document.documentElement.clientWidth) {
  140. if (document.documentElement.clientWidth>0) sw_min=document.documentElement.clientWidth;
  141. if (document.documentElement.clientHeight>0) sh_min=document.documentElement.clientHeight;
  142. }
  143. if (typeof(self.innerWidth)!="undefined" && self.innerWidth) {
  144. if (self.innerWidth>0 && self.innerWidth<sw_min) sw_min=self.innerWidth;
  145. if (self.innerHeight>0 && self.innerHeight<sh_min) sh_min=self.innerHeight;
  146. }
  147. if (document.body.clientWidth) {
  148. if (document.body.clientWidth>0 && document.body.clientWidth<sw_min) sw_min=document.body.clientWidth;
  149. if (document.body.clientHeight>0 && document.body.clientHeight<sh_min) sh_min=document.body.clientHeight;
  150. }
  151. if (sw_min==999999 || sh_min==999999) {
  152. sw_min=800;
  153. sh_min=600;
  154. }
  155. swide=sw_min;
  156. shigh=sh_min;
  157. }
  158. // ]]>
  159. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement