Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. window.onload = function() {
  2.  
  3. var canvas// = document.getElementById( "canvas" );
  4.  
  5. if ( !canvas ) {
  6. canvas = document.createElement( "canvas" );
  7. canvas.style.zIndex = 9999;
  8. canvas.style.display = "block";
  9. //canvas.style.width = "100%";
  10. //canvas.style.height = "100%";
  11. document.body.appendChild( canvas );
  12. }
  13.  
  14. var ctx = canvas.getContext("2d");
  15.  
  16. //canvas dimensions
  17. var W = window.innerWidth;
  18. var H = window.innerHeight;
  19. canvas.width = W;
  20. canvas.height = H;
  21.  
  22. //snowflake particles
  23. var mp = 64; //max particles
  24. var particles = [];
  25. for ( var i = 0; i < mp; i++ ) {
  26. particles.push( {
  27. x: Math.random() * W, //x-coordinate
  28. y: Math.random() * H, //y-coordinate
  29. r: Math.random() * 4 + 1, //radius
  30. d: Math.random() * mp //density
  31. } )
  32. }
  33.  
  34. //Lets draw the flakes
  35. function draw() {
  36. var W = window.innerWidth;
  37. var H = window.innerHeight;
  38. canvas.width = W;
  39. canvas.height = H;
  40.  
  41. ctx.clearRect( 0, 0, W, H );
  42.  
  43. ctx.fillStyle = "rgba(255, 255, 255, 0.8)";
  44. ctx.beginPath();
  45.  
  46. for ( var i = 0; i < mp; i++ ) {
  47. var p = particles[ i ];
  48. ctx.moveTo( p.x, p.y );
  49. ctx.arc( p.x, p.y, p.r, 0, Math.PI * 2, true );
  50. }
  51.  
  52. ctx.fill();
  53. update();
  54. }
  55.  
  56. //Function to move the snowflakes
  57. //angle will be an ongoing incremental flag. Sin and Cos functions will be applied to it to create vertical and horizontal movements of the flakes
  58. var angle = 0;
  59. function update() {
  60. var W = window.innerWidth;
  61. var H = window.innerHeight;
  62.  
  63.  
  64. angle += 0.01;
  65. for ( var i = 0; i < mp; i++ ) {
  66. var p = particles[i];
  67. //Updating X and Y coordinates
  68. //We will add 1 to the cos function to prevent negative values which will lead flakes to move upwards
  69. //Every particle has its own density which can be used to make the downward movement different for each flake
  70. //Lets make it more random by adding in the radius
  71. p.y += Math.cos(angle+p.d) + 1 + p.r/2;
  72. p.x += Math.sin(angle) * 2;
  73.  
  74. //Sending flakes back from the top when it exits
  75. //Lets make it a bit more organic and let flakes enter from the left and right also.
  76. if ( p.x > W+5 || p.x < -5 || p.y > H ) {
  77. if(i%3 > 0) { //66.67% of the flakes
  78. particles[i] = {x: Math.random()*W, y: -10, r: p.r, d: p.d};
  79. } else {
  80. //If the flake is exitting from the right
  81. if(Math.sin(angle) > 0) {
  82. //Enter from the left
  83. particles[i] = {x: -5, y: Math.random()*H, r: p.r, d: p.d};
  84. } else {
  85. //Enter from the right
  86. particles[i] = {x: W+5, y: Math.random()*H, r: p.r, d: p.d};
  87. }
  88. }
  89. }
  90. }
  91. }
  92.  
  93. //animation loop
  94. setInterval(draw, 33);
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement