Advertisement
Guest User

Untitled

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