Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.onload = function()
  2. {
  3.     // Canvas setup
  4.     let canvas = document.querySelector('#canvas');
  5.     let context = canvas.getContext('2d');
  6.     let height = window.innerHeight;
  7.     let width = window.innerWidth;
  8.     canvas.width = width;
  9.     canvas.height = height;
  10.  
  11.     // Reset the drawing canvas
  12.     let clearCanvas = () => {
  13.         context.clearRect(0, 0, canvas.width, canvas.height);
  14.     };
  15.  
  16.     // Get a random number between a given interval.s
  17.     let randomRange = (min, max) => {
  18.         return Math.random() * (max - min) + min;
  19.     };
  20.  
  21.     // Return the euclidean distance between two positions.
  22.     let distance = function(a, b) {
  23.         let dx = a.x - b.x;
  24.         let dy = a.y - b.y;
  25.         return Math.sqrt(dx * dx + dy * dy);
  26.     };
  27.  
  28.     // Detect when the window is resize, update the corresponding sizes.
  29.     window.onresize = (e) => {
  30.         canvas.width = window.innerWidth;
  31.         canvas.height =  window.innerHeight;
  32.         clearCanvas();
  33.     };
  34.  
  35.     // Circle class to spawn multiple instances.
  36.     class Circle {
  37.         // Setup the ball properties.
  38.         constructor(x = 0, y = 0, radius = 10, fillColor = 'red') {
  39.             this.x = x;
  40.             this.y = y;
  41.             this.vx = randomRange(-3 ,3);
  42.             this.vy = randomRange(-3 ,3);
  43.             this.bounce_factor = 0.9995;
  44.             this.radius = randomRange(5, 20);
  45.             this.fillColor = 'rgb('+randomRange(0, 255)+', '+randomRange(0, 255)+', '+randomRange(0, 255)+')';
  46.         }
  47.  
  48.         // Draw the ball with its properties.
  49.         draw(ctx) {
  50.             ctx.fillStyle = this.fillColor;
  51.             ctx.beginPath();
  52.             ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI, false);
  53.             ctx.fill();
  54.         }
  55.     }
  56.  
  57.     // Generate some balls and store them into an array.
  58.     let circles = [];
  59.     for (let i = 0; i < 100; ++i) {
  60.         let circle = new Circle(randomRange(0, width), randomRange(0, height));
  61.         circles.push(circle);
  62.     }
  63.  
  64.     setInterval(() => {
  65.  
  66.         // First clear the canvas.
  67.         clearCanvas();
  68.  
  69.         // For each ball, compute their new positions
  70.         circles.forEach(circle =>
  71.         {
  72.             // Circles collisions
  73.             circles.forEach(other => {
  74.                 // Get the distance between the two balls
  75.                 let dist = distance(circle, other);
  76.  
  77.                 // If the distance is less than the two ball radius
  78.                 // then inverse their velocity according to their
  79.                 // bounce factor.
  80.                 if (dist < (circle.radius + other.radius))
  81.                 {
  82.                     // Calculate the new velocity of the first ball.
  83.                     circle.vx = -circle.vx * circle.bounce_factor;
  84.                     circle.vy = -circle.vy * circle.bounce_factor;
  85.  
  86.                     // Calculate the new velocity of the other ball.
  87.                     other.vx = -other.vx * other.bounce_factor;
  88.                     other.vy = -other.vy * other.bounce_factor;
  89.                 }
  90.             });
  91.  
  92.             // Right border screen collision
  93.             if (circle.x + circle.radius >= canvas.width)
  94.                 circle.vx = -circle.vx * circle.bounce_factor;
  95.             // Left border screen collision
  96.             if (circle.x - circle.radius <= 0)
  97.                 circle.vx = -circle.vx * circle.bounce_factor;
  98.  
  99.             // Bottom screen border collision
  100.             if (circle.y + circle.radius >= canvas.height)
  101.                 circle.vy = -circle.vy * circle.bounce_factor;
  102.             // Top border screen collision
  103.             if (circle.y - circle.radius <= 0)
  104.                 circle.vy = -circle.vy * circle.bounce_factor;
  105.  
  106.             // Apply gravity
  107.             circle.vy += 2;
  108.  
  109.             // Add velocity (speed) to the ball position
  110.             circle.x += circle.vx;
  111.             circle.y += circle.vy;
  112.  
  113.             // Fix jiggling balls when position is more than the canvas height.
  114.             if (circle.y + circle.radius > canvas.height)
  115.                 circle.y = canvas.height - circle.radius;
  116.  
  117.             // Draw the actual ball with the computed positions.
  118.             circle.draw(context);
  119.         });
  120.  
  121.     }, 1000 / 60);
  122. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement