Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. function qS(q) {
  4.     return document.querySelector(q);
  5. }
  6.  
  7. const player = qS('audio');
  8. const topCanvas = qS('.top');
  9. const topCanvasCtx = topCanvas.getContext('2d');
  10. const botCanvas = qS('.bot');
  11. const botCanvasCtx = botCanvas.getContext('2d');
  12. let audioCtx, audioAnalyser, audioSource, bufferLength, dataArray, width, height;
  13.  
  14. function init() {
  15.     player.volume = 0.15;
  16.     player.play();
  17.  
  18.     audioCtx = new (window.AudioContext || window.webkitAudioContext)();
  19.     audioAnalyser = audioCtx.createAnalyser();
  20.     audioAnalyser.smoothingTimeConstant = 0.75;
  21.  
  22.     audioSource = audioCtx.createMediaElementSource(player);
  23.     audioSource.connect(audioAnalyser);
  24.    
  25.     audioAnalyser.connect(audioCtx.destination);
  26.     audioAnalyser.fftSize = 512;
  27.    
  28.     bufferLength = audioAnalyser.frequencyBinCount / 2;
  29.     dataArray = new Uint8Array(bufferLength);
  30.  
  31.     initCanvas();
  32.     draw();
  33. }
  34.  
  35. function initCanvas() {
  36.     width = 0.65 * window.innerWidth;
  37.     height = qS('img').clientHeight;
  38.     topCanvas.setAttribute('width', width);
  39.     topCanvas.setAttribute('height', height);
  40.     botCanvas.setAttribute('width', width);
  41.     botCanvas.setAttribute('height', height);
  42. }
  43.  
  44. function draw() {
  45.     requestAnimationFrame(draw);
  46.  
  47.     audioAnalyser.getByteFrequencyData(dataArray);
  48.  
  49.     topCanvasCtx.fillStyle = 'rgb(255, 255, 255)';
  50.     topCanvasCtx.fillRect(0, 0, width, height);
  51.  
  52.     botCanvasCtx.fillStyle = 'rgb(255, 255, 255)';
  53.     botCanvasCtx.fillRect(0, 0, width, height);
  54.    
  55.     let barWidth = (width / bufferLength) / 2;
  56.     let barHeight;
  57.  
  58.     for (let i = 0; i < bufferLength; i++) {
  59.         barHeight = dataArray[i];
  60.  
  61.         topCanvasCtx.fillStyle = 'rgb(0, 0, 0)';
  62.         topCanvasCtx.fillRect(i * 2 * barWidth, barHeight / 2, barWidth, height - barHeight / 2);
  63.        
  64.         botCanvasCtx.fillStyle = 'rgb(0, 0, 0)';
  65.         botCanvasCtx.fillRect(width - barWidth - (i * 2 * barWidth), 0, barWidth, height - barHeight / 2);
  66.     }
  67. }
  68.  
  69. function night() {
  70.     qS('body').classList.toggle('night');
  71. }
  72.  
  73. function key(e) {
  74.     if (e.code == 'Space') {
  75.         if (player.paused) {
  76.             player.play();
  77.         } else {
  78.             player.pause();
  79.         }
  80.     }
  81. }
  82.  
  83. qS('body').onload = init;
  84. qS('body').onresize = initCanvas;
  85. qS('body').onwheel = e => {
  86.     try {
  87.         player.volume -= e.deltaY/1000;
  88.     } catch (err) {}
  89. };
  90. qS('body').onclick = night;
  91. qS('body').onkeyup = key;
  92. player.onended = player.play;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement