Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                .hole.hidden
p loading audio...

              
            
!

CSS

              
                $BG: rgb(65, 106, 141)
body
  background: $BG
  
canvas, p, .hole
  position: absolute
  top: 50%
  left: 50%
  transform: translate(-50%, -50%)
  
p
  margin: 0
  font-family: monospace
  color: #fff
  
.hole
  height: 24px
  width: 24px
  border-radius: 50%
  background: rgb(57, 98, 133)
  //display: none
  
  &.hidden
    display: none
  
canvas
  z-index: -1

              
            
!

JS

              
                !function () {
  // play with these
  var MIN_RADIUS = 200;
  var JITTER_RANGE = 200;
  var NUM_NODES = 256; // only 1/2 of these are actually drawn
  var FG = [240, 231, 185];
  var BG = [65, 106, 141].map(function (c) { return c - 8; });
  var GLOW = 8;
  var SMOOTHING_TIME_CONSTANT = 0.75; // 0-1
  var SHRINK_AMOUNT = 0.08;
  
  // derived
  var center = MIN_RADIUS + JITTER_RANGE;
  var size = center * 2;
  var shrink = size * SHRINK_AMOUNT;
  // the top half of this song is pretty boring...
  var i0 = 0;
  var i1 = NUM_NODES / 2;
  var range = i1 - i0;
  
  // math
  var PI = Math.PI;
  var sin = Math.sin;
  var cos = Math.cos;

  // canvas
  var canvas1 = document.createElement('canvas');
  var canvas2 = document.createElement('canvas');
  const ctx1 = canvas1.getContext('2d');
  const ctx2 = canvas2.getContext('2d');
  [canvas1, canvas2].forEach(function (canvas) {
    canvas.height = canvas.width = size;
    canvas.style.height = canvas.style.width = size / 2 + 'px';
  });
  [ctx1, ctx2].forEach(function (ctx) {
    ctx.lineWidth = 1;
    ctx.strokeStyle = 'rgb(' + FG + ')';
    ctx.fillStyle = 'rgb(' + BG + ')';
    ctx.shadowBlur = GLOW;
    ctx.shadowColor = 'rgb(' + FG + ')';
  });
  
  // helper function
  function dbToXY (db, i) {
    var r = MIN_RADIUS + db / 255 * JITTER_RANGE;
    var theta = 2*PI * (i / range + 1/4);
    
		return {
      x: r * cos(theta) + center,
      y: r * sin(theta) + center,
    };
  }
  
  function smoothCurveTo (ctx, db1, db2, i) {
    var coords1 = dbToXY(db1, i);
    // midpoint between db1 and db2
    var coords2 = dbToXY((db1 + db2) / 2, i + 0.5);

    ctx.quadraticCurveTo(coords1.x, coords1.y, coords2.x, coords2.y);
  }

  // main loop
  function update (analyser, freqArray, toggle) {
    var i;
    var ctx = toggle ? ctx1 : ctx2;
    var canvas = toggle ? canvas2 : canvas1;
    ctx.clearRect(0, 0, size, size);
    analyser.getByteFrequencyData(freqArray);
    
    var average = 0;
    for (i = i0; i < i1; ++i) {
      average += freqArray[i]; 
    }
    average /= range;
    
    // draw a straight line from i0 to i1 and add it to the
    // resultant shape to ensure the lo and hi nodes meet smoothly
    var lo = freqArray[i0];
    var hi = freqArray[i1];
    var slope = lo - hi;
    
    // also ensure that our average radius never changes
    var offset = (average + slope) / 2;

    ctx.beginPath();
    var firstDb = freqArray[i0] - offset;
    var lastDb = freqArray[i1 - 1] + (range - 1) * slope / range - offset;
    var initialCoords = dbToXY((lastDb + firstDb) / 2, range - 0.5);
    ctx.moveTo.apply(ctx, [initialCoords.x, initialCoords.y]);
    
    var prevDb, db = firstDb;
    for (i = i0 + 1; i < i1; ++i) {
      prevDb = db;
      db = freqArray[i] + (i - i0) * slope / range - offset;
      smoothCurveTo(ctx, prevDb, db, i - i0 - 1);
    }
    smoothCurveTo(ctx, lastDb, firstDb, range - 1);    
    ctx.stroke();
    ctx.fill();

	  ctx.drawImage(canvas, shrink / 2, shrink / 2, size - shrink, size - shrink);
    requestAnimationFrame(update.bind(this, analyser, freqArray, !toggle));
  }
  
  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('https://raw.githubusercontent.com/muhammederdem/mini-player/master/mp3/4.mp3', function (res) {
    var audioContext = new (window.AudioContext || window.webkitAudioContext)();
    audioContext.decodeAudioData(res, function (buffer) {
      var analyser = audioContext.createAnalyser();
      var freqArray = new Uint8Array(analyser.frequencyBinCount);
      var sourceNode = audioContext.createBufferSource();
      analyser.smoothingTimeConstant = SMOOTHING_TIME_CONSTANT;
      analyser.fftSize = NUM_NODES * 2;
      analyser.minDecibels = -90;
      analyser.maxDecibels = -10;
      sourceNode.buffer = buffer;
      analyser.connect(audioContext.destination);
      sourceNode.connect(analyser, freqArray);
      sourceNode.start(0);
      update(analyser, freqArray);

      document.querySelector('p').textContent = '';
      document.querySelector('.hole').className = 'hole';
    });
  });
  
  document.body.appendChild(canvas1);
  document.body.appendChild(canvas2);
}();

              
            
!
999px

Console