Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. document.addEventListener('DOMContentLoaded', init);
  2.  
  3. function init(){
  4.     class Ball {
  5.         constructor(x, y, dx, dy, radius, color){
  6.             this.x = x;
  7.             this.y = y;
  8.             this.dx = dx;
  9.             this.dy = dy;
  10.             this.radius = radius;
  11.             this.color = color;
  12.         }
  13.  
  14.         update() {
  15.             if (this.y + this.radius + this.dy> canvas.height) {
  16.                 this.dy = -this.dy;
  17.                 this.dy = this.dy * friction;
  18.                 this.dx = this.dx * friction;
  19.             } else {
  20.                 this.dy += gravity;
  21.             }
  22.  
  23.             if (this.x + this.radius >= canvas.width || this.x - this.radius <= 0) {
  24.                 this.dx = -this.dx * friction;
  25.             }
  26.  
  27.             this.x += this.dx;
  28.             this.y += this.dy;
  29.             this.draw();
  30.         };
  31.  
  32.         draw() {
  33.             c.beginPath();
  34.             c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
  35.             c.fillStyle = this.color;
  36.             c.fill();
  37.             c.stroke();
  38.             c.closePath();
  39.         };
  40.     }
  41.  
  42.     // Music
  43.     const audio = document.querySelector("audio");
  44.     const audioCtx = new AudioContext();
  45.     const analyser = audioCtx.createAnalyser();
  46.     const source = audioCtx.createMediaElementSource(audio);
  47.     source.connect(analyser);
  48.     analyser.connect(audioCtx.destination);
  49.     analyser.fftSize = 256;
  50.     audio.play();
  51.     let frequencyData = new Uint8Array(analyser.frequencyBinCount);
  52.  
  53.     const canvas = document.querySelector('canvas');
  54.     canvas.width = innerWidth;
  55.     canvas.height = innerHeight;
  56.     const w = canvas.width;
  57.     const h = canvas.height;
  58.     const c = canvas.getContext('2d');
  59.  
  60.     let gravity = 0.9;
  61.     let friction = 0.98;
  62.  
  63.     function randomInt(min,max) {
  64.         return Math.floor(Math.random() * (max - min + 1) + min);
  65.     }
  66.  
  67.     let ballArray = [];
  68.  
  69.     for (let i = 0; i < 100; i++) {
  70.         const radius = randomInt(8, 20);
  71.         const x = randomInt(radius, canvas.width - radius);
  72.         const y = randomInt(0, canvas.height - radius);
  73.         const dx = randomInt(-3, 3);
  74.         const dy = randomInt(-2, 2);
  75.         ballArray.push(new Ball(x, y, dx, dy, radius, "#FFF"));
  76.     }
  77.  
  78.     function drawLoop(){
  79.         c.clearRect(0, 0, w, h);
  80.  
  81.         analyser.getByteFrequencyData(frequencyData);
  82.         for (let i = 0; i < ballArray.length; i++) {
  83.             ballArray[i].update();
  84.         }
  85.  
  86.         let sum = 0;
  87.         for(let d in frequencyData) {
  88.             let f = frequencyData[d];
  89.             sum += d;
  90.             c.beginPath();
  91.             c.fillStyle = "#FFF";
  92.             c.rect(d*5, 0, 4, f);
  93.             c.fill();
  94.         }
  95.  
  96.         // SON
  97.         c.beginPath();
  98.         c.font = "12px sans-serif";
  99.         c.fillStyle = '#FFF';
  100.         c.fillText('Frequence:', 10, 30);
  101.  
  102.         // SUN
  103.         c.save();
  104.  
  105.         c.translate(sun.x, sun.y);
  106.         c.rotate(sun.angle);
  107.  
  108.         c.beginPath();
  109.         c.fillStyle = '#fff522';
  110.         c.arc(0, 0, sun.r, 0, 2*Math.PI, true);
  111.         c.fill();
  112.         c.beginPath();
  113.         c.fillStyle = '#ffd42a';
  114.         c.arc(-sun.r/5, 0, sun.r/5, 0, 2*Math.PI, true);
  115.         c.fill();
  116.         c.beginPath();
  117.         c.fillStyle = '#ffd42a';
  118.         c.arc(sun.r/2, sun.r/2, sun.r/6, 0, 2*Math.PI, true);
  119.         c.fill();
  120.         sun.angle += Math.PI/90;
  121.  
  122.         c.restore();
  123.  
  124.  
  125.         c.beginPath();
  126.         c.fillStyle = '#81ff66';
  127.         c.arc(p.x, p.y, 10, 0, 2*Math.PI, true);
  128.         c.fill();
  129.  
  130.         p.ax = (sun.x - p.x) / 10000;
  131.         p.ay = (sun.y - p.y) / 10000;
  132.  
  133.         p.sx += p.ax;
  134.         p.sy += p.ay;
  135.  
  136.         p.x += p.sx;
  137.         p.y += p.sy;
  138.  
  139.         window.requestAnimationFrame(drawLoop);
  140.     }
  141.  
  142.     window.requestAnimationFrame(drawLoop);
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement