Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. function random(min, max) {
  2. return Math.round(min - 0.5 + Math.random() * (max - min + 1));
  3. }
  4.  
  5. class Ball {
  6. static get colors() {
  7. return [
  8. 'yellow', 'red', 'green', 'blue', 'orange', 'gold', 'purple', 'darkRed'
  9. ];
  10. }
  11.  
  12. constructor() {
  13. this.radius = random(12.5, 20);
  14. this.x = random(this.radius - 10, width);
  15. this.y = -this.radius;
  16. this.speed = random(2, 4);
  17. this.color = Ball.colors[random(0, Ball.colors.length - 1)];
  18. }
  19. }
  20.  
  21.  
  22. let canvas = document.getElementById('canvas'),
  23. ctx = canvas.getContext('2d'),
  24. width = canvas.width,
  25. height = canvas.height;
  26.  
  27. function resizeCanvas() {
  28. canvas.width = window.innerWidth;
  29. canvas.height = window.innerHeight;
  30.  
  31. width = window.innerWidth;
  32. height = window.innerHeight;
  33. }
  34.  
  35. window.addEventListener('resize', resizeCanvas);
  36.  
  37.  
  38. resizeCanvas();
  39.  
  40. let balls = [];
  41.  
  42.  
  43.  
  44. ctx.fillStyle = 'black';
  45. ctx.fillRect(0, 0, width, height);
  46.  
  47. setInterval(() => {
  48. balls.push(new Ball());
  49. }, random(50, 200));
  50.  
  51. requestAnimationFrame(function game() {
  52. ctx.fillStyle = 'black';
  53. ctx.fillRect(0, 0, canvas.width, canvas.height);
  54.  
  55. for (let i = 0; i < balls.length; i++) {
  56. let ball = balls[i];
  57.  
  58. ball.y += ball.speed;
  59.  
  60. ctx.fillStyle = ball.color;
  61. ctx.beginPath();
  62. ctx.arc(ball.x, ball.y, ball.radius, Math.PI * 2, false);
  63. ctx.closePath();
  64. ctx.fill();
  65. }
  66.  
  67. requestAnimationFrame(game);
  68. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement