Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     function loop() {
  2.         if(lastRender) {
  3.             let timestamp = performance.now();
  4.             var progress = timestamp - lastRender
  5.             update(progress);
  6.             draw();
  7.             lastRender = timestamp;
  8.         } else lastRender = performance.now();
  9.         window.requestAnimationFrame(loop);
  10.     }
  11.     function update(progress) {
  12.         offset += progress / (1000 / MIDI.noteplayer.bpm);
  13.         updateParticles();
  14.     }
  15.  
  16.  
  17.     function addParticle(particles) {
  18.         let velX = .5- Math.random();
  19.         velX = Math.min(Math.max(velX, -.2), .2);
  20.         particles.push({
  21.             offsetX: (.5  - Math.random()) * 10,
  22.             offsetY: 0,
  23.             velX: velX,
  24.             velY: -Math.max(1, (Math.random() * 2)),
  25.             opacity: .6,
  26.             size: 2,
  27.             color: getRandomBrightColor()
  28.         });
  29.     }
  30.  
  31.     function getRandomBrightColor() {
  32.         let result = [];
  33.         let big = 200 + Math.random() * 30;
  34.         let bigIndex = Math.random() * 3 | 0;
  35.         result[bigIndex] = big;
  36.         if(bigIndex == 0) {
  37.             result[1] = Math.random() * big;
  38.             result[2] = Math.random() * big;
  39.         } else if(bigIndex == 1) {
  40.             result[0] = Math.random() * big;
  41.             result[2] = Math.random() * big;
  42.         } else if(bigIndex == 2) {
  43.             result[0] = Math.random() * big;
  44.             result[1] = Math.random() * big;
  45.         }
  46.         return result;
  47.     }
  48.  
  49.     function updateParticles() {
  50.         Object.keys(MIDI.keyboard.activeKeys).forEach(key => {
  51.             if(MIDI.keyboard.activeKeys[key]) {
  52.                 MIDI.noteplayer.keyParticles[key] = MIDI.noteplayer.keyParticles[key] || [];
  53.                 let particles = MIDI.noteplayer.keyParticles[key];
  54.  
  55.                 addParticle(particles);
  56.  
  57.                 if(particles.length > 100)
  58.                     particles.shift();
  59.             }
  60.         })
  61.         Object.keys(MIDI.noteplayer.keyParticles).forEach(key => {
  62.             let particles = MIDI.noteplayer.keyParticles[key];
  63.             particles.forEach(p => {
  64.                 p.offsetX += p.velX;
  65.                 p.offsetY += p.velY;
  66.                 p.velY = Math.min(-.2, p.velY *= .98);
  67.                 p.opacity *= .99;
  68.  
  69.             });
  70.             particles = particles.filter(p => p.opacity > .05);
  71.             if(particles.length == 0) delete MIDI.noteplayer.keyParticles[key];
  72.         });
  73.     }
  74.  
  75.  
  76.     function drawParticles(canvas, ctx) {
  77.         Object.keys(MIDI.noteplayer.keyParticles).forEach(key => {
  78.             let particles = MIDI.noteplayer.keyParticles[key];
  79.  
  80.             let index = findNthWhiteKey(key);
  81.             let keyWidth = canvas.width / 52;
  82.             particles.forEach(p => {
  83.                 let [r,g,b] = p.color;
  84.                 ctx.fillStyle = `rgba(${r + 52},${g + 235}, ${b + 97}, ${p.opacity})`;
  85.                 if(isWhiteKey(key)) ctx.fillRect(index * keyWidth + (keyWidth / 2) - (p.size / 2) + p.offsetX, canvas.height + p.offsetY, p.size, p.size);
  86.                 else ctx.fillRect(index * keyWidth - (p.size / 2) + p.offsetX, canvas.height + p.offsetY, p.size, p.size);
  87.             });
  88.         });
  89.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement