Advertisement
Wumbolo

Snowfall background animation

Nov 23rd, 2024
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.81 KB | Source Code | 0 0
  1. <style>
  2. * {
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. }
  7.  
  8. body {
  9. background-color: black;
  10. overflow: hidden;
  11. width: 100vw;
  12. height: 100vh;
  13. }
  14.  
  15. #wrapper {
  16. position: relative !important;
  17. width: 100% !important;
  18. height: 100% !important;
  19. overflow: hidden !important;
  20. }
  21.  
  22. .falling {
  23. position: absolute;
  24. animation: fall linear;
  25. }
  26.  
  27. .rotating {
  28. animation: rotate linear infinite;
  29. }
  30.  
  31. @keyframes fall {
  32. 0% {
  33. transform: translateY(-60vh);
  34. }
  35. 100% {
  36. transform: translateY(60vh);
  37. }
  38. }
  39.  
  40. @keyframes rotate {
  41. 0% {
  42. transform: rotate(0deg);
  43. }
  44. 100% {
  45. transform: rotate(360deg);
  46. }
  47. }
  48. </style>
  49. <script type="module">
  50. import { random } from "https://cdn.skypack.dev/@georgedoescode/generative-utils@1.0.0";
  51.  
  52. // Returns a random integer between two values
  53. function randomInt(min, max) {
  54. return Math.round(random(min, max));
  55. }
  56.  
  57. const container = document.getElementById("wrapper");
  58. let snowflakeId = 0;
  59.  
  60. function createSnowflake() {
  61. if (document.hidden) return;
  62.  
  63. const id = `snowflake-${snowflakeId++}`;
  64.  
  65. // Outer div for falling animation
  66. const outerDiv = document.createElement("div");
  67. outerDiv.classList.add("falling");
  68. outerDiv.style.animationDuration = `${random(8, 18)}s`;
  69. outerDiv.style.left = `${random(0, window.innerWidth)}px`;
  70.  
  71. // Inner div for rotation animation
  72. const innerDiv = document.createElement("div");
  73. innerDiv.classList.add("rotating");
  74. innerDiv.style.animationDuration = `${random(4, 8)}s`;
  75.  
  76. // Snowflake SVG
  77. const snowflake = document.createElementNS(
  78. "http://www.w3.org/2000/svg",
  79. "svg"
  80. );
  81. snowflake.setAttribute("viewBox", "0 0 100 100");
  82. snowflake.setAttribute("xmlns", "http://www.w3.org/2000/svg");
  83. snowflake.setAttribute("id", id);
  84. snowflake.style.width = `${random(80, 150)}px`;
  85. snowflake.style.height = snowflake.style.width;
  86.  
  87. // Generate snowflake properties
  88. snowflake.innerHTML = `
  89. <title>Snowflake ${id}</title>
  90. <g>
  91. ${buildSnowflake(id, randomSnowflakeSettings())}
  92. </g>
  93. `;
  94.  
  95. innerDiv.appendChild(snowflake);
  96. outerDiv.appendChild(innerDiv);
  97. container.appendChild(outerDiv);
  98.  
  99. // Remove outer div after the falling animation ends
  100. outerDiv.addEventListener("animationend", () => {
  101. outerDiv.remove();
  102. });
  103. }
  104.  
  105. function buildSnowflake(id, { trunkLength, branches, treeCount }) {
  106. return `
  107. <g id="${id}-snowflake">
  108. ${buildTree(id, { trunkLength, branches })}
  109. ${buildTreeCopies(id, treeCount)}
  110. </g>
  111. `;
  112. }
  113.  
  114. function buildTree(id, { trunkLength, branches }) {
  115. const trunk = `
  116. <line id="${id}-trunk" x1="50" y1="50" x2="50" y2="${
  117. 50 - trunkLength
  118. }" stroke="#fff" stroke-linecap="round"/>
  119. `;
  120.  
  121. const branchStrings = branches.map(({ distance, length }, index) => {
  122. const startY = 50 - distance;
  123. return `
  124. <line
  125. id="${id}-branch-${index}"
  126. x1="50"
  127. y1="${startY}"
  128. x2="${50 - length}"
  129. y2="${startY - length}"
  130. stroke="#fff"
  131. stroke-linecap="round"
  132. />
  133. `;
  134. });
  135.  
  136. return `<g id="${id}-tree">
  137. ${trunk}
  138. <g id="${id}-branches">${branchStrings.join(" ")}</g>
  139. <use href="#${id}-branches" style="scale: -1 1; transform-origin: center;"/>
  140. </g>`;
  141. }
  142.  
  143. function buildTreeCopies(id, branchCount) {
  144. let copies = "";
  145. for (let i = 0; i < branchCount; i++) {
  146. copies += `
  147. <use
  148. href="#${id}-tree"
  149. transform="rotate(${(360 / branchCount) * i},50,50)"
  150. />
  151. `;
  152. }
  153. return copies;
  154. }
  155.  
  156. function randomSnowflakeSettings() {
  157. const trunkLength = randomInt(20, 40); // Random trunk length
  158. const branches = [];
  159.  
  160. for (
  161. let distance = randomInt(6, 10);
  162. distance < trunkLength;
  163. distance += randomInt(2, 10)
  164. ) {
  165. const length = randomInt(5, Math.min(trunkLength - distance, 10)); // Random branch length
  166. branches.push({ distance, length });
  167. }
  168.  
  169. // const treeCount = randomInt(5, 12); // Random number of trees
  170. const treeCount = 6;
  171.  
  172. return { trunkLength, branches, treeCount };
  173. }
  174.  
  175. function startSnowfall() {
  176. setInterval(() => {
  177. createSnowflake();
  178. }, 1000);
  179. }
  180.  
  181. startSnowfall();
  182. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement