css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              canvas
p loading audio...

            
          
!
            
              body
  background: #000
  
canvas, p
  position: absolute
  top: 50%
  left: 50%
  transform: translate(-50%, -50%)
  
canvas
  cursor: pointer
  
  &:hover + p.fa
    display: block
  
p
  pointer-events: none
  margin: 0
  font-family: monospace
  color: #fff
  background: #000
  
  &.fa-play
    padding-left: 0.8em
    
  &.fa
    display: none
    padding: 0.6em
    border: 4px solid #fff
    border-radius: 50%
    font-size: 24px
    

            
          
!
            
              !function () {
	// play with these
  var MIN_RADIUS = 100;
  var JITTER_RANGE = 35;
  var HEIGHT = 100;
  var RESOLUTION = 2; // one point every 2
  var NUM_NODES = 512; // only 1/2 of these are actually drawn
  var COLOR = [255, 255, 255];
  
  // derived
  var CENTER = HEIGHT / 2;
  
  // math
  var PI2 = Math.PI * 2;
  var sin = Math.sin;
  var cos = Math.cos;

  // canvas
  var canvas = document.querySelector('canvas');
  var ctx = canvas.getContext('2d');
  canvas.height = HEIGHT;
  canvas.width = NUM_NODES;

  // main loop
  function animate (analyser, SAMPLE_RATE) {
    var binCount = analyser.frequencyBinCount;
    var freqArray = new Uint8Array(binCount);
    
    !function draw () {
      analyser.getByteFrequencyData(freqArray);

      ctx.beginPath();
      ctx.strokeStyle = 'rgb(' + COLOR + ')';
      ctx.lineWidth = 2;

      for (var t = 0, node; t < binCount; t += RESOLUTION) {
        node = 0;
        for (var i = 0, f, A; i < binCount; ++i) {
          f = i * SAMPLE_RATE / NUM_NODES;
          A = freqArray[i];
          node += A * sin(PI2 * f * t);
        }
        ctx.lineTo(t, CENTER + node / NUM_NODES * HEIGHT / 8);
      }

      // close the loop and draw the stroke
      ctx.clearRect(0, 0, NUM_NODES, HEIGHT);
      ctx.stroke();

      requestAnimationFrame(draw);
    }();
  }
  
  function loadSound (url, cb) {
    var request = new XMLHttpRequest();
    request.open('GET', url, true);
    request.responseType = 'arraybuffer';
    request.onload = function () { cb(request.response); };
    request.send();
  }
  
  // load the mp3 and initialize our visualizer
  loadSound('http://rileyjshaw.com/static/fitterhappier.mp3', function (res) {
    var audioContext = new (window.AudioContext || window.webkitAudioContext)();
    var SAMPLE_RATE = audioContext.sampleRate;
    audioContext.decodeAudioData(res, function (buffer) {
      var analyser = audioContext.createAnalyser();
      var sourceNode = audioContext.createBufferSource();
      analyser.smoothingTimeConstant = 0.6;
      analyser.fftSize = NUM_NODES * 2;
      analyser.minDecibels = -90;
      analyser.maxDecibels = -10;
      sourceNode.buffer = buffer;
      analyser.connect(audioContext.destination);
      sourceNode.connect(analyser);
      sourceNode.start(0);
      animate(analyser, SAMPLE_RATE);
      
      // add play/pause control
      var playing = true;
      var control = document.querySelector('p');
      control.className = 'fa fa-pause';
      control.textContent = '';
      
      canvas.addEventListener('click', function () {
				sourceNode[(playing ? 'dis' : '') + 'connect'](analyser);
        control.className = 'fa fa-' + (playing ? 'play' : 'pause');
        playing = !playing;
			}, false);
    });
  });
}();

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console