Snowstorm

Let it snow, let it snow, let in snow

目前为 2024-12-09 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Snowstorm
  3. // @namespace sheesh
  4. // @version 1.1
  5. // @author Ur Momma
  6. // @match https://shikme.chat/
  7. // @icon https://shikme.chat/default_images/icon.png?v=1528136794
  8. // @grant none
  9. // @description Let it snow, let it snow, let in snow
  10. // ==/UserScript==
  11. var snowStorm = (function(window, document) {
  12. // --- common properties ---
  13. this.autoStart = true; // Whether the snow should start automatically or not.
  14. this.excludeMobile = true; // Snow is likely to be bad news for mobile phones' CPUs (and batteries.) Enable at your own risk.
  15. this.flakesMax = 128; // Limit total amount of snow made (falling + sticking)
  16. this.flakesMaxActive = 64; // Limit amount of snow falling at once (less = lower CPU use)
  17. this.animationInterval = 33; // Theoretical "miliseconds per frame" measurement. 20 = fast + smooth, but high CPU use. 50 = more conservative, but slower
  18. this.useGPU = true; // Enable transform-based hardware acceleration, reduce CPU load.
  19. this.className = null; // CSS class name for further customization on snow elements
  20. this.excludeMobile = true; // Snow is likely to be bad news for mobile phones' CPUs (and batteries.) By default, be nice.
  21. this.flakeBottom = null; // Integer for Y axis snow limit, 0 or null for "full-screen" snow effect
  22. this.followMouse = true; // Snow movement can respond to the user's mouse
  23. this.snowColor = '#fff'; // Don't eat (or use?) yellow snow.
  24. this.snowCharacter = '•'; // • = bullet, · is square on some systems etc.
  25. this.snowStick = true; // Whether or not snow should "stick" at the bottom. When off, will never collect.
  26. 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
  27. this.useMeltEffect = true; // When recycling fallen snow (or rarely, when falling), have it "melt" and fade out if browser supports it
  28. this.useTwinkleEffect = false; // Allow snow to randomly "flicker" in and out of view while falling
  29. this.usePositionFixed = false; // true = snow does not shift vertically when scrolling. May increase CPU load, disabled by default - if enabled, used only where supported
  30. this.usePixelPosition = false; // Whether to use pixel values for snow top/left vs. percentages. Auto-enabled if body is position:relative or targetElement is specified.
  31. // --- less-used bits ---
  32. this.freezeOnBlur = true; // Only snow when the window is in focus (foreground.) Saves CPU.
  33. this.flakeLeftOffset = 0; // Left margin/gutter space on edge of container (eg. browser window.) Bump up these values if seeing horizontal scrollbars.
  34. this.flakeRightOffset = 0; // Right margin/gutter space on edge of container
  35. this.flakeWidth = 8; // Max pixel width reserved for snow element
  36. this.flakeHeight = 8; // Max pixel height reserved for snow element
  37. this.vMaxX = 5; // Maximum X velocity range for snow
  38. this.vMaxY = 4; // Maximum Y velocity range for snow
  39. this.zIndex = 0; // CSS stacking order applied to each snowflake
  40. // --- "No user-serviceable parts inside" past this point, yadda yadda ---
  41. var storm = this,
  42. features,
  43. // UA sniffing and backCompat rendering mode checks for fixed position, etc.
  44. isIE = navigator.userAgent.match(/msie/i),
  45. isIE6 = navigator.userAgent.match(/msie 6/i),
  46. isMobile = navigator.userAgent.match(/mobile|opera m(ob|in)/i),
  47. isBackCompatIE = (isIE && document.compatMode === 'BackCompat'),
  48. noFixed = (isBackCompatIE || isIE6),
  49. screenX = null, screenX2 = null, screenY = null, scrollY = null, docHeight = null, vRndX = null, vRndY = null,
  50. windOffset = 1,
  51. windMultiplier = 2,
  52. flakeTypes = 6,
  53. fixedForEverything = false,
  54. targetElementIsRelative = false,
  55. opacitySupported = (function(){
  56. try {
  57. document.createElement('div').style.opacity = '0.5';
  58. } catch(e) {
  59. return false;
  60. }
  61. return true;
  62. }()),
  63. didInit = false,
  64. docFrag = document.createDocumentFragment();
  65. features = (function() {
  66. var getAnimationFrame;
  67. /**
  68. * hat tip: paul irish
  69. * http://paulirish.com/2011/requestanimationframe-for-smart-animating/
  70. * https://gist.github.com/838785
  71. */
  72. function timeoutShim(callback) {
  73. window.setTimeout(callback, 1000/(storm.animationInterval || 20));
  74. }
  75. var _animationFrame = (window.requestAnimationFrame ||
  76. window.webkitRequestAnimationFrame ||
  77. window.mozRequestAnimationFrame ||
  78. window.oRequestAnimationFrame ||
  79. window.msRequestAnimationFrame ||
  80. timeoutShim);
  81. // apply to window, avoid "illegal invocation" errors in Chrome
  82. getAnimationFrame = _animationFrame ? function() {
  83. return _animationFrame.apply(window, arguments);
  84. } : null;
  85. var testDiv;
  86. testDiv = document.createElement('div');
  87. function has(prop) {
  88. // test for feature support
  89. var result = testDiv.style[prop];
  90. return (result !== undefined ? prop : null);
  91. }
  92. // note local scope.
  93. var localFeatures = {
  94. transform: {
  95. ie: has('-ms-transform'),
  96. moz: has('MozTransform'),
  97. opera: has('OTransform'),
  98. webkit: has('webkitTransform'),
  99. w3: has('transform'),
  100. prop: null // the normalized property value
  101. },
  102. getAnimationFrame: getAnimationFrame
  103. };
  104. localFeatures.transform.prop = (
  105. localFeatures.transform.w3 ||
  106. localFeatures.transform.moz ||
  107. localFeatures.transform.webkit ||
  108. localFeatures.transform.ie ||
  109. localFeatures.transform.opera
  110. );
  111. testDiv = null;
  112. return localFeatures;
  113. }());
  114. this.timer = null;
  115. this.flakes = [];
  116. this.disabled = false;
  117. this.active = false;
  118. this.meltFrameCount = 20;
  119. this.meltFrames = [];
  120. this.setXY = function(o, x, y) {
  121. if (!o) {
  122. return false;
  123. }
  124. if (storm.usePixelPosition || targetElementIsRelative) {
  125. o.style.left = (x - storm.flakeWidth) + 'px';
  126. o.style.top = (y - storm.flakeHeight) + 'px';
  127. } else if (noFixed) {
  128. o.style.right = (100-(x/screenX*100)) + '%';
  129. // avoid creating vertical scrollbars
  130. o.style.top = (Math.min(y, docHeight-storm.flakeHeight)) + 'px';
  131. } else {
  132. if (!storm.flakeBottom) {
  133. // if not using a fixed bottom coordinate...
  134. o.style.right = (100-(x/screenX*100)) + '%';
  135. o.style.bottom = (100-(y/screenY*100)) + '%';
  136. } else {
  137. // absolute top.
  138. o.style.right = (100-(x/screenX*100)) + '%';
  139. o.style.top = (Math.min(y, docHeight-storm.flakeHeight)) + 'px';
  140. }
  141. }
  142. };
  143. this.events = (function() {
  144. var old = (!window.addEventListener && window.attachEvent), slice = Array.prototype.slice,
  145. evt = {
  146. add: (old?'attachEvent':'addEventListener'),
  147. remove: (old?'detachEvent':'removeEventListener')
  148. };
  149. function getArgs(oArgs) {
  150. var args = slice.call(oArgs), len = args.length;
  151. if (old) {
  152. args[1] = 'on' + args[1]; // prefix
  153. if (len > 3) {
  154. args.pop(); // no capture
  155. }
  156. } else if (len === 3) {
  157. args.push(false);
  158. }
  159. return args;
  160. }
  161. function apply(args, sType) {
  162. var element = args.shift(),
  163. method = [evt[sType]];
  164. if (old) {
  165. element[method](args[0], args[1]);
  166. } else {
  167. element[method].apply(element, args);
  168. }
  169. }
  170. function addEvent() {
  171. apply(getArgs(arguments), 'add');
  172. }
  173. function removeEvent() {
  174. apply(getArgs(arguments), 'remove');
  175. }
  176. return {
  177. add: addEvent,
  178. remove: removeEvent
  179. };
  180. }());
  181. function rnd(n,min) {
  182. if (isNaN(min)) {
  183. min = 0;
  184. }
  185. return (Math.random()*n)+min;
  186. }
  187. function plusMinus(n) {
  188. return (parseInt(rnd(2),10)===1?n*-1:n);
  189. }
  190. this.randomizeWind = function() {
  191. var i;
  192. vRndX = plusMinus(rnd(storm.vMaxX,0.2));
  193. vRndY = rnd(storm.vMaxY,0.2);
  194. if (this.flakes) {
  195. for (i=0; i<this.flakes.length; i++) {
  196. if (this.flakes[i].active) {
  197. this.flakes[i].setVelocities();
  198. }
  199. }
  200. }
  201. };
  202. this.scrollHandler = function() {
  203. var i;
  204. // "attach" snowflakes to bottom of window if no absolute bottom value was given
  205. scrollY = (storm.flakeBottom ? 0 : parseInt(window.scrollY || document.documentElement.scrollTop || (noFixed ? document.body.scrollTop : 0), 10));
  206. if (isNaN(scrollY)) {
  207. scrollY = 0; // Netscape 6 scroll fix
  208. }
  209. if (!fixedForEverything && !storm.flakeBottom && storm.flakes) {
  210. for (i=0; i<storm.flakes.length; i++) {
  211. if (storm.flakes[i].active === 0) {
  212. storm.flakes[i].stick();
  213. }
  214. }
  215. }
  216. };
  217. this.resizeHandler = function() {
  218. if (window.innerWidth || window.innerHeight) {
  219. screenX = window.innerWidth - 16 - storm.flakeRightOffset;
  220. screenY = (storm.flakeBottom || window.innerHeight);
  221. } else {
  222. screenX = (document.documentElement.clientWidth || document.body.clientWidth || document.body.scrollWidth) - (!isIE ? 8 : 0) - storm.flakeRightOffset;
  223. screenY = storm.flakeBottom || document.documentElement.clientHeight || document.body.clientHeight || document.body.scrollHeight;
  224. }
  225. docHeight = document.body.offsetHeight;
  226. screenX2 = parseInt(screenX/2,10);
  227. };
  228. this.resizeHandlerAlt = function() {
  229. screenX = storm.targetElement.offsetWidth - storm.flakeRightOffset;
  230. screenY = storm.flakeBottom || storm.targetElement.offsetHeight;
  231. screenX2 = parseInt(screenX/2,10);
  232. docHeight = document.body.offsetHeight;
  233. };
  234. this.freeze = function() {
  235. // pause animation
  236. if (!storm.disabled) {
  237. storm.disabled = 1;
  238. } else {
  239. return false;
  240. }
  241. storm.timer = null;
  242. };
  243. this.resume = function() {
  244. if (storm.disabled) {
  245. storm.disabled = 0;
  246. } else {
  247. return false;
  248. }
  249. storm.timerInit();
  250. };
  251. this.toggleSnow = function() {
  252. if (!storm.flakes.length) {
  253. // first run
  254. storm.start();
  255. } else {
  256. storm.active = !storm.active;
  257. if (storm.active) {
  258. storm.show();
  259. storm.resume();
  260. } else {
  261. storm.stop();
  262. storm.freeze();
  263. }
  264. }
  265. };
  266. this.stop = function() {
  267. var i;
  268. this.freeze();
  269. for (i=0; i<this.flakes.length; i++) {
  270. this.flakes[i].o.style.display = 'none';
  271. }
  272. storm.events.remove(window,'scroll',storm.scrollHandler);
  273. storm.events.remove(window,'resize',storm.resizeHandler);
  274. if (storm.freezeOnBlur) {
  275. if (isIE) {
  276. storm.events.remove(document,'focusout',storm.freeze);
  277. storm.events.remove(document,'focusin',storm.resume);
  278. } else {
  279. storm.events.remove(window,'blur',storm.freeze);
  280. storm.events.remove(window,'focus',storm.resume);
  281. }
  282. }
  283. };
  284. this.show = function() {
  285. var i;
  286. for (i=0; i<this.flakes.length; i++) {
  287. this.flakes[i].o.style.display = 'block';
  288. }
  289. };
  290. this.SnowFlake = function(type,x,y) {
  291. var s = this;
  292. this.type = type;
  293. this.x = x||parseInt(rnd(screenX-20),10);
  294. this.y = (!isNaN(y)?y:-rnd(screenY)-12);
  295. this.vX = null;
  296. this.vY = null;
  297. this.vAmpTypes = [1,1.2,1.4,1.6,1.8]; // "amplification" for vX/vY (based on flake size/type)
  298. this.vAmp = this.vAmpTypes[this.type] || 1;
  299. this.melting = false;
  300. this.meltFrameCount = storm.meltFrameCount;
  301. this.meltFrames = storm.meltFrames;
  302. this.meltFrame = 0;
  303. this.twinkleFrame = 0;
  304. this.active = 1;
  305. this.fontSize = (10+(this.type/5)*10);
  306. this.o = document.createElement('div');
  307. this.o.innerHTML = storm.snowCharacter;
  308. if (storm.className) {
  309. this.o.setAttribute('class', storm.className);
  310. }
  311. this.o.style.color = storm.snowColor;
  312. this.o.style.position = (fixedForEverything?'fixed':'absolute');
  313. if (storm.useGPU && features.transform.prop) {
  314. // GPU-accelerated snow.
  315. this.o.style[features.transform.prop] = 'translate3d(0px, 0px, 0px)';
  316. }
  317. this.o.style.width = storm.flakeWidth+'px';
  318. this.o.style.height = storm.flakeHeight+'px';
  319. this.o.style.fontFamily = 'arial,verdana';
  320. this.o.style.cursor = 'default';
  321. this.o.style.overflow = 'hidden';
  322. this.o.style.fontWeight = 'normal';
  323. this.o.style.zIndex = storm.zIndex;
  324. docFrag.appendChild(this.o);
  325. this.refresh = function() {
  326. if (isNaN(s.x) || isNaN(s.y)) {
  327. // safety check
  328. return false;
  329. }
  330. storm.setXY(s.o, s.x, s.y);
  331. };
  332. this.stick = function() {
  333. if (noFixed || (storm.targetElement !== document.documentElement && storm.targetElement !== document.body)) {
  334. s.o.style.top = (screenY+scrollY-storm.flakeHeight)+'px';
  335. } else if (storm.flakeBottom) {
  336. s.o.style.top = storm.flakeBottom+'px';
  337. } else {
  338. s.o.style.display = 'none';
  339. s.o.style.top = 'auto';
  340. s.o.style.bottom = '0%';
  341. s.o.style.position = 'fixed';
  342. s.o.style.display = 'block';
  343. }
  344. };
  345. this.vCheck = function() {
  346. if (s.vX>=0 && s.vX<0.2) {
  347. s.vX = 0.2;
  348. } else if (s.vX<0 && s.vX>-0.2) {
  349. s.vX = -0.2;
  350. }
  351. if (s.vY>=0 && s.vY<0.2) {
  352. s.vY = 0.2;
  353. }
  354. };
  355. this.move = function() {
  356. var vX = s.vX*windOffset, yDiff;
  357. s.x += vX;
  358. s.y += (s.vY*s.vAmp);
  359. if (s.x >= screenX || screenX-s.x < storm.flakeWidth) { // X-axis scroll check
  360. s.x = 0;
  361. } else if (vX < 0 && s.x-storm.flakeLeftOffset < -storm.flakeWidth) {
  362. s.x = screenX-storm.flakeWidth-1; // flakeWidth;
  363. }
  364. s.refresh();
  365. yDiff = screenY+scrollY-s.y+storm.flakeHeight;
  366. if (yDiff<storm.flakeHeight) {
  367. s.active = 0;
  368. if (storm.snowStick) {
  369. s.stick();
  370. } else {
  371. s.recycle();
  372. }
  373. } else {
  374. if (storm.useMeltEffect && s.active && s.type < 3 && !s.melting && Math.random()>0.998) {
  375. // ~1/1000 chance of melting mid-air, with each frame
  376. s.melting = true;
  377. s.melt();
  378. // only incrementally melt one frame
  379. // s.melting = false;
  380. }
  381. if (storm.useTwinkleEffect) {
  382. if (s.twinkleFrame < 0) {
  383. if (Math.random() > 0.97) {
  384. s.twinkleFrame = parseInt(Math.random() * 8, 10);
  385. }
  386. } else {
  387. s.twinkleFrame--;
  388. if (!opacitySupported) {
  389. s.o.style.visibility = (s.twinkleFrame && s.twinkleFrame % 2 === 0 ? 'hidden' : 'visible');
  390. } else {
  391. s.o.style.opacity = (s.twinkleFrame && s.twinkleFrame % 2 === 0 ? 0 : 1);
  392. }
  393. }
  394. }
  395. }
  396. };
  397. this.animate = function() {
  398. // main animation loop
  399. // move, check status, die etc.
  400. s.move();
  401. };
  402. this.setVelocities = function() {
  403. s.vX = vRndX+rnd(storm.vMaxX*0.12,0.1);
  404. s.vY = vRndY+rnd(storm.vMaxY*0.12,0.1);
  405. };
  406. this.setOpacity = function(o,opacity) {
  407. if (!opacitySupported) {
  408. return false;
  409. }
  410. o.style.opacity = opacity;
  411. };
  412. this.melt = function() {
  413. if (!storm.useMeltEffect || !s.melting) {
  414. s.recycle();
  415. } else {
  416. if (s.meltFrame < s.meltFrameCount) {
  417. s.setOpacity(s.o,s.meltFrames[s.meltFrame]);
  418. s.o.style.fontSize = s.fontSize-(s.fontSize*(s.meltFrame/s.meltFrameCount))+'px';
  419. s.o.style.lineHeight = storm.flakeHeight+2+(storm.flakeHeight*0.75*(s.meltFrame/s.meltFrameCount))+'px';
  420. s.meltFrame++;
  421. } else {
  422. s.recycle();
  423. }
  424. }
  425. };
  426. this.recycle = function() {
  427. s.o.style.display = 'none';
  428. s.o.style.position = (fixedForEverything?'fixed':'absolute');
  429. s.o.style.bottom = 'auto';
  430. s.setVelocities();
  431. s.vCheck();
  432. s.meltFrame = 0;
  433. s.melting = false;
  434. s.setOpacity(s.o,1);
  435. s.o.style.padding = '0px';
  436. s.o.style.margin = '0px';
  437. s.o.style.fontSize = s.fontSize+'px';
  438. s.o.style.lineHeight = (storm.flakeHeight+2)+'px';
  439. s.o.style.textAlign = 'center';
  440. s.o.style.verticalAlign = 'baseline';
  441. s.x = parseInt(rnd(screenX-storm.flakeWidth-20),10);
  442. s.y = parseInt(rnd(screenY)*-1,10)-storm.flakeHeight;
  443. s.refresh();
  444. s.o.style.display = 'block';
  445. s.active = 1;
  446. };
  447. this.recycle(); // set up x/y coords etc.
  448. this.refresh();
  449. };
  450. this.snow = function() {
  451. var active = 0, flake = null, i, j;
  452. for (i=0, j=storm.flakes.length; i<j; i++) {
  453. if (storm.flakes[i].active === 1) {
  454. storm.flakes[i].move();
  455. active++;
  456. }
  457. if (storm.flakes[i].melting) {
  458. storm.flakes[i].melt();
  459. }
  460. }
  461. if (active<storm.flakesMaxActive) {
  462. flake = storm.flakes[parseInt(rnd(storm.flakes.length),10)];
  463. if (flake.active === 0) {
  464. flake.melting = true;
  465. }
  466. }
  467. if (storm.timer) {
  468. features.getAnimationFrame(storm.snow);
  469. }
  470. };
  471. this.mouseMove = function(e) {
  472. if (!storm.followMouse) {
  473. return true;
  474. }
  475. var x = parseInt(e.clientX,10);
  476. if (x<screenX2) {
  477. windOffset = -windMultiplier+(x/screenX2*windMultiplier);
  478. } else {
  479. x -= screenX2;
  480. windOffset = (x/screenX2)*windMultiplier;
  481. }
  482. };
  483. this.createSnow = function(limit,allowInactive) {
  484. var i;
  485. for (i=0; i<limit; i++) {
  486. storm.flakes[storm.flakes.length] = new storm.SnowFlake(parseInt(rnd(flakeTypes),10));
  487. if (allowInactive || i>storm.flakesMaxActive) {
  488. storm.flakes[storm.flakes.length-1].active = -1;
  489. }
  490. }
  491. storm.targetElement.appendChild(docFrag);
  492. };
  493. this.timerInit = function() {
  494. storm.timer = true;
  495. storm.snow();
  496. };
  497. this.init = function() {
  498. var i;
  499. for (i=0; i<storm.meltFrameCount; i++) {
  500. storm.meltFrames.push(1-(i/storm.meltFrameCount));
  501. }
  502. storm.randomizeWind();
  503. storm.createSnow(storm.flakesMax); // create initial batch
  504. storm.events.add(window,'resize',storm.resizeHandler);
  505. storm.events.add(window,'scroll',storm.scrollHandler);
  506. if (storm.freezeOnBlur) {
  507. if (isIE) {
  508. storm.events.add(document,'focusout',storm.freeze);
  509. storm.events.add(document,'focusin',storm.resume);
  510. } else {
  511. storm.events.add(window,'blur',storm.freeze);
  512. storm.events.add(window,'focus',storm.resume);
  513. }
  514. }
  515. storm.resizeHandler();
  516. storm.scrollHandler();
  517. if (storm.followMouse) {
  518. storm.events.add(isIE?document:window,'mousemove',storm.mouseMove);
  519. }
  520. storm.animationInterval = Math.max(20,storm.animationInterval);
  521. storm.timerInit();
  522. };
  523. this.start = function(bFromOnLoad) {
  524. if (!didInit) {
  525. didInit = true;
  526. } else if (bFromOnLoad) {
  527. // already loaded and running
  528. return true;
  529. }
  530. if (typeof storm.targetElement === 'string') {
  531. var targetID = storm.targetElement;
  532. storm.targetElement = document.getElementById(targetID);
  533. if (!storm.targetElement) {
  534. throw new Error('Snowstorm: Unable to get targetElement "'+targetID+'"');
  535. }
  536. }
  537. if (!storm.targetElement) {
  538. storm.targetElement = (document.body || document.documentElement);
  539. }
  540. if (storm.targetElement !== document.documentElement && storm.targetElement !== document.body) {
  541. // re-map handler to get element instead of screen dimensions
  542. storm.resizeHandler = storm.resizeHandlerAlt;
  543. //and force-enable pixel positioning
  544. storm.usePixelPosition = true;
  545. }
  546. storm.resizeHandler(); // get bounding box elements
  547. storm.usePositionFixed = (storm.usePositionFixed && !noFixed && !storm.flakeBottom); // whether or not position:fixed is to be used
  548. if (window.getComputedStyle) {
  549. // attempt to determine if body or user-specified snow parent element is relatlively-positioned.
  550. try {
  551. targetElementIsRelative = (window.getComputedStyle(storm.targetElement, null).getPropertyValue('position') === 'relative');
  552. } catch(e) {
  553. // oh well
  554. targetElementIsRelative = false;
  555. }
  556. }
  557. fixedForEverything = storm.usePositionFixed;
  558. if (screenX && screenY && !storm.disabled) {
  559. storm.init();
  560. storm.active = true;
  561. }
  562. };
  563. function doDelayedStart() {
  564. window.setTimeout(function() {
  565. storm.start(true);
  566. }, 20);
  567. // event cleanup
  568. storm.events.remove(isIE?document:window,'mousemove',doDelayedStart);
  569. }
  570. function doStart() {
  571. if (!storm.excludeMobile || !isMobile) {
  572. doDelayedStart();
  573. }
  574. // event cleanup
  575. storm.events.remove(window, 'load', doStart);
  576. }
  577. // hooks for starting the snow
  578. if (storm.autoStart) {
  579. storm.events.add(window, 'load', doStart, false);
  580. }
  581. return this;
  582. }(window, document));