🏠 Home 

Greasy Fork is available in English.

SnowFall

Snow Fall by Rainbow Arch

Этот скрипт недоступен для установки пользователем. Он является библиотекой, которая подключается к другим скриптам мета-ключом // @require https://update.greasyfork.org/scripts/4769/15928/SnowFall.js

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