Guest User

Untitled

a guest
Dec 3rd, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. // The star of every good animation
  2. var requestAnimationFrame = window.requestAnimationFrame ||
  3. window.mozRequestAnimationFrame ||
  4. window.webkitRequestAnimationFrame ||
  5. window.msRequestAnimationFrame;
  6.  
  7. var transforms = ["transform",
  8. "msTransform",
  9. "webkitTransform",
  10. "mozTransform",
  11. "oTransform"];
  12.  
  13. var transformProperty = getSupportedPropertyName(transforms);
  14.  
  15. // Array to store our Snowflake objects
  16. var snowflakes = [];
  17.  
  18. // Global variables to store our browser's window size
  19. var browserWidth;
  20. var browserHeight;
  21.  
  22. // Specify the number of snowflakes you want visible
  23. var numberOfSnowflakes = 50;
  24.  
  25. // Flag to reset the position of the snowflakes
  26. var resetPosition = false;
  27.  
  28. //
  29. // It all starts here...
  30. //
  31. function setup() {
  32. window.addEventListener("DOMContentLoaded", generateSnowflakes, false);
  33. window.addEventListener("resize", setResetFlag, false);
  34. }
  35. setup();
  36.  
  37. //
  38. // Vendor prefix management
  39. //
  40. function getSupportedPropertyName(properties) {
  41. for (var i = 0; i < properties.length; i++) {
  42. if (typeof document.body.style[properties[i]] != "undefined") {
  43. return properties[i];
  44. }
  45. }
  46. return null;
  47. }
  48.  
  49. //
  50. // Constructor for our Snowflake object
  51. //
  52. function Snowflake(element, radius, speed, xPos, yPos) {
  53.  
  54. // set initial snowflake properties
  55. this.element = element;
  56. this.radius = radius;
  57. this.speed = speed;
  58. this.xPos = xPos;
  59. this.yPos = yPos;
  60.  
  61. // declare variables used for snowflake's motion
  62. this.counter = 0;
  63. this.sign = Math.random() < 0.5 ? 1 : -1;
  64.  
  65. // setting an initial opacity and size for our snowflake
  66. this.element.style.opacity = .1 + Math.random();
  67. this.element.style.fontSize = 12 + Math.random() * 50 + "px";
  68. }
  69.  
  70. //
  71. // The function responsible for actually moving our snowflake
  72. //
  73. Snowflake.prototype.update = function () {
  74.  
  75. // using some trigonometry to determine our x and y position
  76. this.counter += this.speed / 5000;
  77. this.xPos += this.sign * this.speed * Math.cos(this.counter) / 40;
  78. this.yPos += Math.sin(this.counter) / 40 + this.speed / 30;
  79.  
  80. // setting our snowflake's position
  81. setTranslate3DTransform(this.element, Math.round(this.xPos), Math.round(this.yPos));
  82.  
  83. // if snowflake goes below the browser window, move it back to the top
  84. if (this.yPos > browserHeight) {
  85. this.yPos = -50;
  86. }
  87. }
  88.  
  89. //
  90. // A performant way to set your snowflake's position
  91. //
  92. function setTranslate3DTransform(element, xPosition, yPosition) {
  93. var val = "translate3d(" + xPosition + "px, " + yPosition + "px" + ", 0)";
  94. element.style[transformProperty] = val;
  95. }
  96.  
  97. //
  98. // The function responsible for creating the snowflake
  99. //
  100. function generateSnowflakes() {
  101.  
  102. // get our snowflake element from the DOM and store it
  103. var originalSnowflake = document.querySelector(".snowflake");
  104.  
  105. // access our snowflake element's parent container
  106. var snowflakeContainer = originalSnowflake.parentNode;
  107.  
  108. // get our browser's size
  109. browserWidth = document.documentElement.clientWidth;
  110. browserHeight = document.documentElement.clientHeight;
  111.  
  112. // create each individual snowflake
  113. for (var i = 0; i < numberOfSnowflakes; i++) {
  114.  
  115. // clone our original snowflake and add it to snowflakeContainer
  116. var snowflakeCopy = originalSnowflake.cloneNode(true);
  117. snowflakeContainer.appendChild(snowflakeCopy);
  118.  
  119. // set our snowflake's initial position and related properties
  120. var initialXPos = getPosition(50, browserWidth);
  121. var initialYPos = getPosition(50, browserHeight);
  122. var speed = 5+Math.random()*40;
  123. var radius = 4+Math.random()*10;
  124.  
  125. // create our Snowflake object
  126. var snowflakeObject = new Snowflake(snowflakeCopy,
  127. radius,
  128. speed,
  129. initialXPos,
  130. initialYPos);
  131. snowflakes.push(snowflakeObject);
  132. }
  133.  
  134. // remove the original snowflake because we no longer need it visible
  135. snowflakeContainer.removeChild(originalSnowflake);
  136.  
  137. // call the moveSnowflakes function every 30 milliseconds
  138. moveSnowflakes();
  139. }
  140.  
  141. //
  142. // Responsible for moving each snowflake by calling its update function
  143. //
  144. function moveSnowflakes() {
  145. for (var i = 0; i < snowflakes.length; i++) {
  146. var snowflake = snowflakes[i];
  147. snowflake.update();
  148. }
  149.  
  150. // Reset the position of all the snowflakes to a new value
  151. if (resetPosition) {
  152. browserWidth = document.documentElement.clientWidth;
  153. browserHeight = document.documentElement.clientHeight;
  154.  
  155. for (var i = 0; i < snowflakes.length; i++) {
  156. var snowflake = snowflakes[i];
  157.  
  158. snowflake.xPos = getPosition(50, browserWidth);
  159. snowflake.yPos = getPosition(50, browserHeight);
  160. }
  161.  
  162. resetPosition = false;
  163. }
  164.  
  165. requestAnimationFrame(moveSnowflakes);
  166. }
  167.  
  168. //
  169. // This function returns a number between (maximum - offset) and (maximum + offset)
  170. //
  171. function getPosition(offset, size) {
  172. return Math.round(-1*offset + Math.random() * (size+2*offset));
  173. }
  174.  
  175. //
  176. // Trigger a reset of all the snowflakes' positions
  177. //
  178. function setResetFlag(e) {
  179. resetPosition = true;
  180. }
Add Comment
Please, Sign In to add comment