EduardET

particle effect

Dec 11th, 2017
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2.     // Little Canvas things
  3. var canvas = document.querySelector("#canvas"),
  4.     ctx = canvas.getContext("2d");
  5.  
  6. // Set Canvas to be window size
  7. canvas.width = window.innerWidth;
  8. canvas.height = window.innerHeight;
  9.  
  10. // Configuration, Play with these
  11. var config = {
  12.     particleNumber: 100,
  13.     maxParticleSize: 10,
  14.     maxSpeed: 25,
  15.     colorVariation: 5
  16. };
  17.  
  18. // Colors
  19. var colorPalette = {
  20.     bg: { r: 50, g: 150, b: 150 },
  21.     matter: [
  22.         { r: 36, g: 18, b: 42  }, // darkPRPL
  23.         { r: 78, g: 36, b: 42 }, // rockDust
  24.         { r: 252, g: 178, b: 96 }, // solorFlare
  25.         { r: 253, g: 238, b: 152 } // totesASun
  26.     ]
  27. };
  28.  
  29. // Some Variables hanging out
  30. var particles = [],
  31.     centerX = canvas.width / 2,
  32.     centerY = canvas.height / 2,
  33.     drawBg,
  34.     // Draws the background for the canvas, because space
  35.     drawBg = function(ctx, color) {
  36.         ctx.fillStyle = "rgb(" + color.r + "," + color.g + "," + color.b + ")";
  37.         ctx.fillRect(0, 0, canvas.width, canvas.height);
  38.     };
  39.  
  40. // Particle Constructor
  41. var Particle = function(x, y) {
  42.     // X Coordinate
  43.     this.x = x || Math.round(Math.random() * canvas.width);
  44.     // Y Coordinate
  45.     this.y = y || Math.round(Math.random() * canvas.height);
  46.     // Radius of the space dust
  47.     this.r = Math.ceil(Math.random() * config.maxParticleSize);
  48.     // Color of the rock, given some randomness
  49.     this.c = colorVariation(
  50.         colorPalette.matter[Math.floor(Math.random() * colorPalette.matter.length)],
  51.         true
  52.     );
  53.     // Speed of which the rock travels
  54.     this.s = Math.pow(Math.ceil(Math.random() * config.maxSpeed), 0.7);
  55.     // Direction the Rock flies
  56.     this.d = Math.round(Math.random() * 360);
  57. };
  58.  
  59. // Provides some nice color variation
  60. // Accepts an rgba object
  61. // returns a modified rgba object or a rgba string if true is passed in for argument 2
  62. var colorVariation = function(color, returnString) {
  63.     var r, g, b, a, variation;
  64.     r = Math.round(
  65.         Math.random() * config.colorVariation - config.colorVariation / 2 + color.r
  66.     );
  67.     g = Math.round(
  68.         Math.random() * config.colorVariation - config.colorVariation / 2 + color.g
  69.     );
  70.     b = Math.round(
  71.         Math.random() * config.colorVariation - config.colorVariation / 2 + color.b
  72.     );
  73.     a = Math.random() + 0.5;
  74.     if (returnString) {
  75.         return "rgba(" + r + "," + g + "," + b + "," + a + ")";
  76.     } else {
  77.         return {
  78.             r, g, b, a
  79.         };
  80.     }
  81. };
  82.  
  83. // Used to find the rocks next point in space, accounting for speed and direction
  84. var updateParticleModel = function(p) {
  85.     var a = 180 - (p.d + 90); // find the 3rd angle
  86.     p.d > 0 && p.d < 180 ? (p.x += p.s * Math.sin(p.d) / Math.sin(p.s)) : (p.x -= p.s * Math.sin(p.d) / Math.sin(p.s)); // jshint ignore:line
  87.     p.d > 90 && p.d < 270 ? (p.y += p.s * Math.sin(a) / Math.sin(p.s)) : (p.y -= p.s * Math.sin(a) / Math.sin(p.s)); // jshint ignore:line
  88.     return p;
  89. };
  90.  
  91. // Just the function that physically draws the particles
  92. // Physically? sure why not, physically.
  93. var drawParticle = function(x, y, r, c) {
  94.     ctx.beginPath();
  95.     ctx.fillStyle = c;
  96.     ctx.arc(x, y, r, 0, 2 * Math.PI, false);
  97.     ctx.fill();
  98.     ctx.closePath();
  99. };
  100.  
  101. // Remove particles that aren't on the canvas
  102. var cleanUpArray = function() {
  103.     particles = particles.filter(p => {
  104.         return p.x > -100 && p.y > -100;
  105.     });
  106. };
  107.  
  108. var initParticles = function(numParticles, x, y) {
  109.     for (let i = 0; i < numParticles; i++) {
  110.         particles.push(new Particle(x, y));
  111.     }
  112.     particles.forEach(p => {
  113.         drawParticle(p.x, p.y, p.r, p.c);
  114.     });
  115. };
  116.  
  117. // That thing
  118. window.requestAnimFrame = (function() {
  119.     return (
  120.         window.requestAnimationFrame ||
  121.         window.webkitRequestAnimationFrame ||
  122.         window.mozRequestAnimationFrame ||
  123.         function(callback) {
  124.             window.setTimeout(callback, 1000 / 60);
  125.         }
  126.     );
  127. })();
  128.  
  129. // Our Frame function
  130. var frame = function() {
  131.     // Draw background first
  132.     drawBg(ctx, colorPalette.bg);
  133.     // Update Particle models to new position
  134.     particles.map(p => {
  135.         return updateParticleModel(p);
  136.     });
  137.     // Draw em'
  138.     particles.forEach(p => {
  139.         drawParticle(p.x, p.y, p.r, p.c);
  140.     });
  141.     // Play the same song? Ok!
  142.     window.requestAnimFrame(frame);
  143. };
  144.  
  145. // Click listener
  146. document.body.addEventListener("click", function(event) {
  147.     var x = event.clientX,
  148.         y = event.clientY;
  149.     cleanUpArray();
  150.     initParticles(config.particleNumber, x, y);
  151. });
  152.  
  153. // First Frame
  154. frame();
  155.  
  156. // First particle explosion
  157. initParticles(config.particleNumber);
  158. </script>
Advertisement
Add Comment
Please, Sign In to add comment