slaingerz

Untitled

Oct 30th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.83 KB | None | 0 0
  1. <script type="text/javascript">
  2. /** @license
  3. * DHTML Snowstorm! JavaScript-based Snow for web pages
  4. * --------------------------------------------------------
  5. * Version 1.41.20101113 (Previous rev: 1.4.20091115)
  6. * Copyright (c) 2007, Scott Schiller. All rights reserved.
  7. * Code provided under the BSD License:
  8. * http://schillmania.com/projects/snowstorm/license.txt
  9. */
  10.  
  11. /*global window, document, navigator, clearInterval, setInterval */
  12. /*jslint white: false, onevar: true, plusplus: false, undef: true, nomen: true, eqeqeq: true, bitwise: true, regexp: true, newcap: true, immed: true */
  13.  
  14. var snowStorm = (function(window, document) {
  15.  
  16. // --- common properties ---
  17.  
  18. this.flakesMax = 128; // Limit total amount of snow made (falling + sticking)
  19. this.flakesMaxActive = 64; // Limit amount of snow falling at once (less = lower CPU use)
  20. this.animationInterval = 33; // Theoretical "miliseconds per frame" measurement. 20 = fast + smooth, but high CPU use. 50 = more conservative, but slower
  21. this.excludeMobile = true; // Snow is likely to be bad news for mobile phones' CPUs (and batteries.) By default, be nice.
  22. this.flakeBottom = null; // Integer for Y axis snow limit, 0 or null for "full-screen" snow effect
  23. this.followMouse = true; // Snow movement can respond to the user's mouse
  24. this.snowColor = '#fff'; // Don't eat (or use?) yellow snow.
  25. this.snowCharacter = '&bull;'; // &bull; = bullet, &middot; is square on some systems etc.
  26. this.snowStick = true; // Whether or not snow should "stick" at the bottom. When off, will never collect.
  27. this.targetElement = null; // element which snow will be appended to (null = document.body) - can be an element ID eg. 'myDiv', or a DOM node reference
  28. this.useMeltEffect = true; // When recycling fallen snow (or rarely, when falling), have it "melt" and fade out if browser supports it
  29. this.useTwinkleEffect = false; // Allow snow to randomly "flicker" in and out of view while falling
  30. this.usePositionFixed = false; // true = snow does not shift vertically when scrolling. May increase CPU load, disabled by default - if enabled, used only where supported
  31.  
  32. // --- less-used bits ---
  33.  
  34. this.freezeOnBlur = true; // Only snow when the window is in focus (foreground.) Saves CPU.
  35. this.flakeLeftOffset = 0; // Left margin/gutter space on edge of container (eg. browser window.) Bump up these values if seeing horizontal scrollbars.
  36. this.flakeRightOffset = 0; // Right margin/gutter space on edge of container
  37. this.flakeWidth = 8; // Max pixel width reserved for snow element
  38. this.flakeHeight = 8; // Max pixel height reserved for snow element
  39. this.vMaxX = 5; // Maximum X velocity range for snow
  40. this.vMaxY = 4; // Maximum Y velocity range for snow
  41. this.zIndex = 0; // CSS stacking order applied to each snowflake
  42.  
  43. // --- End of user section ---
  44.  
  45. var s = this, storm = this, i,
  46. // UA sniffing and backCompat rendering mode checks for fixed position, etc.
  47. isIE = navigator.userAgent.match(/msie/i),
  48. isIE6 = navigator.userAgent.match(/msie 6/i),
  49. isWin98 = navigator.appVersion.match(/windows 98/i),
  50. isMobile = navigator.userAgent.match(/mobile/i),
  51. isBackCompatIE = (isIE && document.compatMode === 'BackCompat'),
  52. noFixed = (isMobile || isBackCompatIE || isIE6),
  53. screenX = null, screenX2 = null, screenY = null, scrollY = null, vRndX = null, vRndY = null,
  54. windOffset = 1,
  55. windMultiplier = 2,
  56. flakeTypes = 6,
  57. fixedForEverything = false,
  58. opacitySupported = (function(){
  59. try {
  60. document.createElement('div').style.opacity = '0.5';
  61. } catch(e) {
  62. return false;
  63. }
  64. return true;
  65. }()),
  66. didInit = false,
  67. docFrag = document.createDocumentFragment();
  68.  
  69. this.timers = [];
  70. this.flakes = [];
  71. this.disabled = false;
  72. this.active = false;
  73. this.meltFrameCount = 20;
  74. this.meltFrames = [];
  75.  
  76. this.events = (function() {
  77.  
  78. var old = (window.attachEvent), slice = Array.prototype.slice,
  79. evt = {
  80. add: (old?'attachEvent':'addEventListener'),
  81. remove: (old?'detachEvent':'removeEventListener')
  82. };
  83.  
  84. function getArgs(oArgs) {
  85. var args = slice.call(oArgs), len = args.length;
  86. if (old) {
  87. args[1] = 'on' + args[1]; // prefix
  88. if (len > 3) {
  89. args.pop(); // no capture
  90. }
  91. } else if (len === 3) {
  92. args.push(false);
  93. }
  94. return args;
  95. }
  96.  
  97. function apply(args, sType) {
  98. var oFunc = args.shift()[evt[sType]];
  99. if (old) {
  100. oFunc(args[0], args[1]);
  101. } else {
  102. oFunc.apply(this, args);
  103. }
  104. }
  105.  
  106. function addEvent() {
  107. apply(getArgs(arguments), 'add');
  108. }
  109.  
  110. function removeEvent() {
  111. apply(getArgs(arguments), 'remove');
  112. }
  113.  
  114. return {
  115. add: addEvent,
  116. remove: removeEvent
  117. };
  118.  
  119. }());
  120.  
  121. function rnd(n,min) {
  122. if (isNaN(min)) {
  123. min = 0;
  124. }
  125. return (Math.random()*n)+min;
  126. }
  127.  
  128. function plusMinus(n) {
  129. return (parseInt(rnd(2),10)===1?n*-1:n);
  130. }
  131.  
  132. this.randomizeWind = function() {
  133. vRndX = plusMinus(rnd(s.vMaxX,0.2));
  134. vRndY = rnd(s.vMaxY,0.2);
  135. if (this.flakes) {
  136. for (var i=0; i<this.flakes.length; i++) {
  137. if (this.flakes[i].active) {
  138. this.flakes[i].setVelocities();
  139. }
  140. }
  141. }
  142. };
  143.  
  144. this.scrollHandler = function() {
  145. // "attach" snowflakes to bottom of window if no absolute bottom value was given
  146. scrollY = (s.flakeBottom?0:parseInt(window.scrollY||document.documentElement.scrollTop||document.body.scrollTop,10));
  147. if (isNaN(scrollY)) {
  148. scrollY = 0; // Netscape 6 scroll fix
  149. }
  150. if (!fixedForEverything && !s.flakeBottom && s.flakes) {
  151. for (var i=s.flakes.length; i--;) {
  152. if (s.flakes[i].active === 0) {
  153. s.flakes[i].stick();
  154. }
  155. }
  156. }
  157. };
  158.  
  159. this.resizeHandler = function() {
  160. if (window.innerWidth || window.innerHeight) {
  161. screenX = window.innerWidth-(!isIE?16:2)-s.flakeRightOffset;
  162. screenY = (s.flakeBottom?s.flakeBottom:window.innerHeight);
  163. } else {
  164. screenX = (document.documentElement.clientWidth||document.body.clientWidth||document.body.scrollWidth)-(!isIE?8:0)-s.flakeRightOffset;
  165. screenY = s.flakeBottom?s.flakeBottom:(document.documentElement.clientHeight||document.body.clientHeight||document.body.scrollHeight);
  166. }
  167. screenX2 = parseInt(screenX/2,10);
  168. };
  169.  
  170. this.resizeHandlerAlt = function() {
  171. screenX = s.targetElement.offsetLeft+s.targetElement.offsetWidth-s.flakeRightOffset;
  172. screenY = s.flakeBottom?s.flakeBottom:s.targetElement.offsetTop+s.targetElement.offsetHeight;
  173. screenX2 = parseInt(screenX/2,10);
  174. };
  175.  
  176. this.freeze = function() {
  177. // pause animation
  178. if (!s.disabled) {
  179. s.disabled = 1;
  180. } else {
  181. return false;
  182. }
  183. for (var i=s.timers.length; i--;) {
  184. clearInterval(s.timers[i]);
  185. }
  186. };
  187.  
  188. this.resume = function() {
  189. if (s.disabled) {
  190. s.disabled = 0;
  191. } else {
  192. return false;
  193. }
  194. s.timerInit();
  195. };
  196.  
  197. this.toggleSnow = function() {
  198. if (!s.flakes.length) {
  199. // first run
  200. s.start();
  201. } else {
  202. s.active = !s.active;
  203. if (s.active) {
  204. s.show();
  205. s.resume();
  206. } else {
  207. s.stop();
  208. s.freeze();
  209. }
  210. }
  211. };
  212.  
  213. this.stop = function() {
  214. this.freeze();
  215. for (var i=this.flakes.length; i--;) {
  216. this.flakes[i].o.style.display = 'none';
  217. }
  218. s.events.remove(window,'scroll',s.scrollHandler);
  219. s.events.remove(window,'resize',s.resizeHandler);
  220. if (s.freezeOnBlur) {
  221. if (isIE) {
  222. s.events.remove(document,'focusout',s.freeze);
  223. s.events.remove(document,'focusin',s.resume);
  224. } else {
  225. s.events.remove(window,'blur',s.freeze);
  226. s.events.remove(window,'focus',s.resume);
  227. }
  228. }
  229. };
  230.  
  231. this.show = function() {
  232. for (var i=this.flakes.length; i--;) {
  233. this.flakes[i].o.style.display = 'block';
  234. }
  235. };
  236.  
  237. this.SnowFlake = function(parent,type,x,y) {
  238. var s = this, storm = parent;
  239. this.type = type;
  240. this.x = x||parseInt(rnd(screenX-20),10);
  241. this.y = (!isNaN(y)?y:-rnd(screenY)-12);
  242. this.vX = null;
  243. this.vY = null;
  244. this.vAmpTypes = [1,1.2,1.4,1.6,1.8]; // "amplification" for vX/vY (based on flake size/type)
  245. this.vAmp = this.vAmpTypes[this.type];
  246. this.melting = false;
  247. this.meltFrameCount = storm.meltFrameCount;
  248. this.meltFrames = storm.meltFrames;
  249. this.meltFrame = 0;
  250. this.twinkleFrame = 0;
  251. this.active = 1;
  252. this.fontSize = (10+(this.type/5)*10);
  253. this.o = document.createElement('div');
  254. this.o.innerHTML = storm.snowCharacter;
  255. this.o.style.color = storm.snowColor;
  256. this.o.style.position = (fixedForEverything?'fixed':'absolute');
  257. this.o.style.width = storm.flakeWidth+'px';
  258. this.o.style.height = storm.flakeHeight+'px';
  259. this.o.style.fontFamily = 'arial,verdana';
  260. this.o.style.overflow = 'hidden';
  261. this.o.style.fontWeight = 'normal';
  262. this.o.style.zIndex = storm.zIndex;
  263. docFrag.appendChild(this.o);
  264.  
  265. this.refresh = function() {
  266. if (isNaN(s.x) || isNaN(s.y)) {
  267. // safety check
  268. return false;
  269. }
  270. s.o.style.left = s.x+'px';
  271. s.o.style.top = s.y+'px';
  272. };
  273.  
  274. this.stick = function() {
  275. if (noFixed || (storm.targetElement !== document.documentElement && storm.targetElement !== document.body)) {
  276. s.o.style.top = (screenY+scrollY-storm.flakeHeight)+'px';
  277. } else if (storm.flakeBottom) {
  278. s.o.style.top = storm.flakeBottom+'px';
  279. } else {
  280. s.o.style.display = 'none';
  281. s.o.style.top = 'auto';
  282. s.o.style.bottom = '0px';
  283. s.o.style.position = 'fixed';
  284. s.o.style.display = 'block';
  285. }
  286. };
  287.  
  288. this.vCheck = function() {
  289. if (s.vX>=0 && s.vX<0.2) {
  290. s.vX = 0.2;
  291. } else if (s.vX<0 && s.vX>-0.2) {
  292. s.vX = -0.2;
  293. }
  294. if (s.vY>=0 && s.vY<0.2) {
  295. s.vY = 0.2;
  296. }
  297. };
  298.  
  299. this.move = function() {
  300. var vX = s.vX*windOffset, yDiff;
  301. s.x += vX;
  302. s.y += (s.vY*s.vAmp);
  303. if (s.x >= screenX || screenX-s.x < storm.flakeWidth) { // X-axis scroll check
  304. s.x = 0;
  305. } else if (vX < 0 && s.x-storm.flakeLeftOffset < 0-storm.flakeWidth) {
  306. s.x = screenX-storm.flakeWidth-1; // flakeWidth;
  307. }
  308. s.refresh();
  309. yDiff = screenY+scrollY-s.y;
  310. if (yDiff<storm.flakeHeight) {
  311. s.active = 0;
  312. if (storm.snowStick) {
  313. s.stick();
  314. } else {
  315. s.recycle();
  316. }
  317. } else {
  318. if (storm.useMeltEffect && s.active && s.type < 3 && !s.melting && Math.random()>0.998) {
  319. // ~1/1000 chance of melting mid-air, with each frame
  320. s.melting = true;
  321. s.melt();
  322. // only incrementally melt one frame
  323. // s.melting = false;
  324. }
  325. if (storm.useTwinkleEffect) {
  326. if (!s.twinkleFrame) {
  327. if (Math.random()>0.9) {
  328. s.twinkleFrame = parseInt(Math.random()*20,10);
  329. }
  330. } else {
  331. s.twinkleFrame--;
  332. s.o.style.visibility = (s.twinkleFrame && s.twinkleFrame%2===0?'hidden':'visible');
  333. }
  334. }
  335. }
  336. };
  337.  
  338. this.animate = function() {
  339. // main animation loop
  340. // move, check status, die etc.
  341. s.move();
  342. };
  343.  
  344. this.setVelocities = function() {
  345. s.vX = vRndX+rnd(storm.vMaxX*0.12,0.1);
  346. s.vY = vRndY+rnd(storm.vMaxY*0.12,0.1);
  347. };
  348.  
  349. this.setOpacity = function(o,opacity) {
  350. if (!opacitySupported) {
  351. return false;
  352. }
  353. o.style.opacity = opacity;
  354. };
  355.  
  356. this.melt = function() {
  357. if (!storm.useMeltEffect || !s.melting) {
  358. s.recycle();
  359. } else {
  360. if (s.meltFrame < s.meltFrameCount) {
  361. s.meltFrame++;
  362. s.setOpacity(s.o,s.meltFrames[s.meltFrame]);
  363. s.o.style.fontSize = s.fontSize-(s.fontSize*(s.meltFrame/s.meltFrameCount))+'px';
  364. s.o.style.lineHeight = storm.flakeHeight+2+(storm.flakeHeight*0.75*(s.meltFrame/s.meltFrameCount))+'px';
  365. } else {
  366. s.recycle();
  367. }
  368. }
  369. };
  370.  
  371. this.recycle = function() {
  372. s.o.style.display = 'none';
  373. s.o.style.position = (fixedForEverything?'fixed':'absolute');
  374. s.o.style.bottom = 'auto';
  375. s.setVelocities();
  376. s.vCheck();
  377. s.meltFrame = 0;
  378. s.melting = false;
  379. s.setOpacity(s.o,1);
  380. s.o.style.padding = '0px';
  381. s.o.style.margin = '0px';
  382. s.o.style.fontSize = s.fontSize+'px';
  383. s.o.style.lineHeight = (storm.flakeHeight+2)+'px';
  384. s.o.style.textAlign = 'center';
  385. s.o.style.verticalAlign = 'baseline';
  386. s.x = parseInt(rnd(screenX-storm.flakeWidth-20),10);
  387. s.y = parseInt(rnd(screenY)*-1,10)-storm.flakeHeight;
  388. s.refresh();
  389. s.o.style.display = 'block';
  390. s.active = 1;
  391. };
  392.  
  393. this.recycle(); // set up x/y coords etc.
  394. this.refresh();
  395.  
  396. };
  397.  
  398. this.snow = function() {
  399. var active = 0, used = 0, waiting = 0, flake = null, i;
  400. for (i=s.flakes.length; i--;) {
  401. if (s.flakes[i].active === 1) {
  402. s.flakes[i].move();
  403. active++;
  404. } else if (s.flakes[i].active === 0) {
  405. used++;
  406. } else {
  407. waiting++;
  408. }
  409. if (s.flakes[i].melting) {
  410. s.flakes[i].melt();
  411. }
  412. }
  413. if (active<s.flakesMaxActive) {
  414. flake = s.flakes[parseInt(rnd(s.flakes.length),10)];
  415. if (flake.active === 0) {
  416. flake.melting = true;
  417. }
  418. }
  419. };
  420.  
  421. this.mouseMove = function(e) {
  422. if (!s.followMouse) {
  423. return true;
  424. }
  425. var x = parseInt(e.clientX,10);
  426. if (x<screenX2) {
  427. windOffset = -windMultiplier+(x/screenX2*windMultiplier);
  428. } else {
  429. x -= screenX2;
  430. windOffset = (x/screenX2)*windMultiplier;
  431. }
  432. };
  433.  
  434. this.createSnow = function(limit,allowInactive) {
  435. for (var i=0; i<limit; i++) {
  436. s.flakes[s.flakes.length] = new s.SnowFlake(s,parseInt(rnd(flakeTypes),10));
  437. if (allowInactive || i>s.flakesMaxActive) {
  438. s.flakes[s.flakes.length-1].active = -1;
  439. }
  440. }
  441. storm.targetElement.appendChild(docFrag);
  442. };
  443.  
  444. this.timerInit = function() {
  445. s.timers = (!isWin98?[setInterval(s.snow,s.animationInterval)]:[setInterval(s.snow,s.animationInterval*3),setInterval(s.snow,s.animationInterval)]);
  446. };
  447.  
  448. this.init = function() {
  449. for (var i=0; i<s.meltFrameCount; i++) {
  450. s.meltFrames.push(1-(i/s.meltFrameCount));
  451. }
  452. s.randomizeWind();
  453. s.createSnow(s.flakesMax); // create initial batch
  454. s.events.add(window,'resize',s.resizeHandler);
  455. s.events.add(window,'scroll',s.scrollHandler);
  456. if (s.freezeOnBlur) {
  457. if (isIE) {
  458. s.events.add(document,'focusout',s.freeze);
  459. s.events.add(document,'focusin',s.resume);
  460. } else {
  461. s.events.add(window,'blur',s.freeze);
  462. s.events.add(window,'focus',s.resume);
  463. }
  464. }
  465. s.resizeHandler();
  466. s.scrollHandler();
  467. if (s.followMouse) {
  468. s.events.add(isIE?document:window,'mousemove',s.mouseMove);
  469. }
  470. s.animationInterval = Math.max(20,s.animationInterval);
  471. s.timerInit();
  472. };
  473.  
  474. this.start = function(bFromOnLoad) {
  475. if (!didInit) {
  476. didInit = true;
  477. } else if (bFromOnLoad) {
  478. // already loaded and running
  479. return true;
  480. }
  481. if (typeof s.targetElement === 'string') {
  482. var targetID = s.targetElement;
  483. s.targetElement = document.getElementById(targetID);
  484. if (!s.targetElement) {
  485. throw new Error('Snowstorm: Unable to get targetElement "'+targetID+'"');
  486. }
  487. }
  488. if (!s.targetElement) {
  489. s.targetElement = (!isIE?(document.documentElement?document.documentElement:document.body):document.body);
  490. }
  491. if (s.targetElement !== document.documentElement && s.targetElement !== document.body) {
  492. s.resizeHandler = s.resizeHandlerAlt; // re-map handler to get element instead of screen dimensions
  493. }
  494. s.resizeHandler(); // get bounding box elements
  495. s.usePositionFixed = (s.usePositionFixed && !noFixed); // whether or not position:fixed is supported
  496. fixedForEverything = s.usePositionFixed;
  497. if (screenX && screenY && !s.disabled) {
  498. s.init();
  499. s.active = true;
  500. }
  501. };
  502.  
  503. function doStart() {
  504. if ((this.excludeMobile && !isMobile) || !this.excludeMobile) {
  505. window.setTimeout(function() {
  506. s.start(true);
  507. }, 20);
  508. }
  509. // event cleanup
  510. s.events.remove(window, 'load', doStart);
  511. }
  512.  
  513. // hooks for starting the snow
  514. s.events.add(window, 'load', doStart, false);
  515.  
  516. return this;
  517.  
  518. }(window, document));
  519. </script>
Advertisement
Add Comment
Please, Sign In to add comment