Advertisement
Katsiree

snow

Dec 2nd, 2019
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.09 KB | None | 0 0
  1. <style>
  2. canvas#particle_canvas {
  3. position: absolute;
  4. width: 100%;
  5. }
  6. </style>
  7.  
  8. <script>
  9. function microtime() {
  10. return new Date().getTime() * 0.001;
  11. }
  12.  
  13. // returns a random integer from min to max
  14. function irand(min, max) {
  15. return Math.floor((min || 0) + Math.random() * ((max + 1 || 100) - (min || 0)));
  16. }
  17.  
  18. // returns a random float from min to max
  19. function frand(min, max) {
  20. return (min || 0) + Math.random() * ((max || 1) - (min || 0));
  21. }
  22.  
  23. function clamp(value, min, max) {
  24. return Math.min(Math.max(value, min), max);
  25. }
  26.  
  27. // Two component vector class
  28. function Vector2(x, y) {
  29. this.x = x || 0;
  30. this.y = y || 0;
  31.  
  32. this.add = function (other) {
  33. this.x += other.x;
  34. this.y += other.y;
  35. }
  36.  
  37. this.magnitude = function () {
  38. return Math.sqrt(this.x * this.x + this.y * this.y);
  39. }
  40. }
  41.  
  42. function Color(r, g, b) {
  43. this.r = r || 0;
  44. this.g = g || 0;
  45. this.b = b || 0;
  46. }
  47.  
  48. window.addEventListener('resize', function () {
  49. jsCanvasSnow.resize(window.innerWidth, window.innerHeight);
  50. jsCanvasSnow.init("particle_canvas", options);
  51. }, false);
  52.  
  53. window.addEventListener('load', function () {
  54. //var options = {};
  55. var options = {
  56. 'amount': 40,
  57. 'size': [8, 20],
  58. 'rotation': [1, 5],
  59. 'speed': [0, 100],
  60. 'swing': [0.1, 1],
  61. 'amplitude': [30, 50],
  62. 'alpha': [0.1, 0.95],
  63. 'images': ["https://i.imgur.com/ufp4kCZ.png", "https://i.imgur.com/Cf6MtC0.png",
  64. "https://i.imgur.com/WZNhvnO.png", "https://i.imgur.com/VAutkUx.png"
  65. ]
  66. };
  67.  
  68. jsCanvasSnow.init("particle_canvas", options);
  69. jsCanvasSnow.start();
  70. }, false);
  71.  
  72. function jsParticle(origin, velocity, size, amplitude, rspeed, alpha, image) {
  73. this.origin = origin;
  74. this.position = new Vector2(origin.x, origin.y);
  75. this.velocity = velocity || new Vector2(0, 0);
  76. this.size = size;
  77. this.rspeed = rspeed;
  78. this.amplitude = amplitude;
  79. this.alpha = alpha;
  80. this.image = image;
  81.  
  82. this.dx = Math.random() * 100;
  83. this.rotation = Math.random() * 360;
  84.  
  85. this.update = function (delta_time) {
  86. this.dx += this.velocity.x * delta_time;
  87. this.position.y += this.velocity.y * delta_time;
  88. this.position.x = this.origin.x + (this.amplitude * Math.sin(this.dx));
  89. this.rotation += this.rspeed * delta_time;
  90. }
  91. }
  92.  
  93. var jsCanvasSnow = {
  94. canvas: null,
  95. ctx: null,
  96. particles: [],
  97. running: false,
  98.  
  99. pImageObjects: [],
  100.  
  101. start_time: 0,
  102. frame_time: 0,
  103.  
  104. init: function (canvas_id, options) {
  105. // use the container width and height
  106. this.canvas = document.getElementById(canvas_id);
  107. this.ctx = this.canvas.getContext('2d');
  108. this.resize(window.innerWidth, window.innerHeight);
  109.  
  110. // default values
  111. this.pAmount = options.amount || 500;
  112. this.pSize = options.size || [8, 26];
  113. this.pRotation = options.rotation || [-5, 5];
  114. this.pSwing = options.swing || [0.1, 1];
  115. this.pSpeed = options.speed || [40, 100],
  116. this.pAmplitude = options.amplitude || [20, 50];
  117. this.pAlpha = options.alpha || [0.25, 1];
  118. this.pImageNames = options.images || [];
  119.  
  120. // initialize all the images
  121. for (var i = 0; i < this.pImageNames.length; i++) {
  122. var image = new Image();
  123. image.src = this.pImageNames[i];
  124. this.pImageObjects.push(image);
  125. }
  126.  
  127. this._init_particles();
  128. },
  129.  
  130. start: function () {
  131. this.running = true;
  132. this.start_time = this.frame_time = microtime();
  133. this._loop();
  134. },
  135.  
  136. stop: function () {
  137. this.running = false;
  138. },
  139.  
  140. resize: function (w, h) {
  141. this.canvas.width = w;
  142. this.canvas.height = h;
  143. },
  144.  
  145. _loop: function () {
  146. if (jsCanvasSnow.running) {
  147. jsCanvasSnow._clear();
  148. jsCanvasSnow._update();
  149. jsCanvasSnow._draw();
  150. jsCanvasSnow._queue();
  151. }
  152. },
  153.  
  154. _init_particles: function () {
  155. // clear the particles array
  156. this.particles.length = 0;
  157.  
  158. for (var i = 0; i < this.pAmount; i++) {
  159. var origin = new Vector2(frand(0, this.canvas.width), frand(-this.canvas.height, 0));
  160. var velocity = new Vector2(frand(this.pSwing[0], this.pSwing[1]), frand(this.pSpeed[0],
  161. this
  162. .pSpeed[1]));
  163. var size = frand(this.pSize[0], this.pSize[1]);
  164. var amplitude = frand(this.pAmplitude[0], this.pAmplitude[1]);
  165. var rspeed = frand(this.pRotation[0], this.pRotation[1]) * ((Math.random() < 0.5) ? -1 :
  166. 1);
  167. var alpha = frand(this.pAlpha[0], this.pAlpha[1]);
  168. var image = (this.pImageObjects.length > 0) ? irand(0, this.pImageObjects.length - 1) :
  169. -1;
  170.  
  171. this.particles.push(new jsParticle(origin, velocity, size, amplitude, rspeed, alpha,
  172. image));
  173. }
  174. },
  175.  
  176. _update: function () {
  177. var now_time = microtime();
  178. var delta_time = now_time - this.frame_time;
  179.  
  180. for (var i = 0; i < this.particles.length; i++) {
  181. var particle = this.particles[i];
  182. particle.update(delta_time);
  183.  
  184. if (particle.position.y - particle.size > this.canvas.height) {
  185. particle.position.y = -particle.size * 2;
  186. particle.position.x = particle.origin.x = Math.random() * this.canvas.width;
  187. }
  188. }
  189.  
  190. this.frame_time = now_time;
  191. },
  192.  
  193. _draw: function () {
  194. this.ctx.fillStyle = 'rgb(255,255,255)';
  195.  
  196. for (var i = 0; i < this.particles.length; i++) {
  197. var particle = this.particles[i];
  198. var center = -(particle.size / 2);
  199.  
  200. this.ctx.save();
  201. this.ctx.translate(particle.position.x, particle.position.y);
  202. this.ctx.rotate(particle.rotation);
  203. this.ctx.globalAlpha = this.particles[i].alpha;
  204.  
  205. if (particle.image == -1)
  206. this.ctx.fillRect(center, center, particle.size, particle.size);
  207. else
  208. this.ctx.drawImage(this.pImageObjects[particle.image], center, center, particle
  209. .size,
  210. particle.size);
  211.  
  212. this.ctx.restore();
  213. }
  214. },
  215.  
  216. _clear: function () {
  217. this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
  218. },
  219.  
  220. _queue: function () {
  221. window.requestAnimationFrame(jsCanvasSnow._loop);
  222. },
  223. }
  224. </script>
  225.  
  226.  
  227.  
  228. <canvas id="particle_canvas"></canvas>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement