Advertisement
Guest User

Untitled

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