memchik

snow.js

Nov 30th, 2020
601
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var snowMax = 50;
  2.  
  3. var snowColor = ["#DDD", "#EEE"];
  4.  
  5. var snowEntity = "•";
  6.  
  7. var snowSpeed = 0.75;
  8.  
  9. var snowMinSize = 8;
  10.  
  11. var snowMaxSize = 24;
  12.  
  13. var snowRefresh = 50;
  14.  
  15. var snowStyles = "cursor: default; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; -o-user-select: none; user-select: none;";
  16.  
  17. var snow = [],
  18.     pos = [],
  19.     coords = [],
  20.     lefr = [],
  21.     marginBottom,
  22.     marginRight;
  23.  
  24. function randomise(range) {
  25.     rand = Math.floor(range * Math.random());
  26.     return rand;
  27. }
  28.  
  29. function initSnow() {
  30.     var snowSize = snowMaxSize - snowMinSize;
  31.     marginBottom = document.body.scrollHeight - 5;
  32.     marginRight = document.body.clientWidth - 15;
  33.  
  34.     for (i = 0; i <= snowMax; i++) {
  35.         coords[i] = 0;
  36.         lefr[i] = Math.random() * 15;
  37.         pos[i] = 0.03 + Math.random() / 10;
  38.         snow[i] = document.getElementById("flake" + i);
  39.         snow[i].style.fontFamily = "inherit";
  40.         snow[i].size = randomise(snowSize) + snowMinSize;
  41.         snow[i].style.fontSize = snow[i].size + "px";
  42.         snow[i].style.color = snowColor[randomise(snowColor.length)];
  43.         snow[i].style.zIndex = 1000;
  44.         snow[i].sink = snowSpeed * snow[i].size / 5;
  45.         snow[i].posX = randomise(marginRight - snow[i].size);
  46.         snow[i].posY = randomise(2 * marginBottom - marginBottom - 2 * snow[i].size);
  47.         snow[i].style.left = snow[i].posX + "px";
  48.         snow[i].style.top = snow[i].posY + "px";
  49.     }
  50.  
  51.     moveSnow();
  52. }
  53.  
  54. function resize() {
  55.     marginBottom = document.body.scrollHeight - 5;
  56.     marginRight = document.body.clientWidth - 15;
  57. }
  58.  
  59. function moveSnow() {
  60.     for (i = 0; i <= snowMax; i++) {
  61.         coords[i] += pos[i];
  62.         snow[i].posY += snow[i].sink;
  63.         snow[i].style.left = snow[i].posX + lefr[i] * Math.sin(coords[i]) + "px";
  64.         snow[i].style.top = snow[i].posY + "px";
  65.  
  66.         if (snow[i].posY >= marginBottom - 2 * snow[i].size || parseInt(snow[i].style.left) > (marginRight - 3 * lefr[i])) {
  67.             snow[i].posX = randomise(marginRight - snow[i].size);
  68.             snow[i].posY = 0;
  69.         }
  70.     }
  71.  
  72.     setTimeout("moveSnow()", snowRefresh);
  73. }
  74.  
  75. for (i = 0; i <= snowMax; i++) {
  76.     document.write("<span id='flake" + i + "' style='" + snowStyles + "position:absolute;top:-" + snowMaxSize + "'>" + snowEntity + "</span>");
  77. }
  78.  
  79. window.addEventListener('resize', resize);
  80. window.addEventListener('load', initSnow);
  81.  
  82.  
RAW Paste Data