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.

            
              <header>
  <h1>Pls allow the mic... then make some noise</h1>
</header>

<div id="screen">
  <canvas></canvas>
</div>
            
          
!
            
              html {background:#efefef; font-family:"Lato", "Lucida Grande","Lucida Sans Unicode", Tahoma, Sans-Serif; letter-spacing:-0.2px;}
body {margin:0px; padding:0px; overflow:hidden;}
header {
  display:flex; padding:5px;
  justify-content:space-around;
  h1 {
    display:block; padding:5px 15px;
    border: 3px solid #343436; border-radius: 3px;
    background-color:#343436;
    text-align:center; text-decoration:none; color:#fff; font-size:1.1em;
    &:hover {background-color:#4d4d50;}
  }
  
}
#screen, canvas {width: 100vw; height: 100vh; padding:0px; margin:0px;}
#screen {
  position: relative;
  background: black;
}


            
          
!
            
              // this is all the drawing stuff right here
var shapeCount = 16;

function hex() {
  ctx.fillStyle = "#000";
  ctx.fillRect(0,0,canvas.width, canvas.height);

  var frequencies = adjustFreqData(shapeCount);
  var newData = frequencies.newFreqs;
  ctx.globalCompositeOperation = "hard-light";
   ctx.lineWidth = 4;
  
  for(var i=0;i<newData.length;i++) {
    var d = newData[i];
    // ctx.beginPath();
    drawHex(ctx, (d*1.5)+20, i*(screen.width/shapeCount), i*(screen.height/shapeCount));
    ctx.strokeStyle = "hsla("+(i*10+150)+",60%,80%,1)";
    ctx.fillStyle = "hsla("+(i*10+150)+",60%,80%,0.7)";
    ctx.fill();
    ctx.stroke();
  }

}


// this is all the other stuff to make it work
const audioApi = new window.AudioContext;

// variables
var audioBuffer,
    analyserNode,
    frequencyData = new Uint8Array(4096);

// create an audio API analyser node and connect to source
function createAnalyserNode(audioSource) {
  analyserNode = audioApi.createAnalyser();
  analyserNode.fftSize = 8192;
  audioSource.connect(analyserNode);
}

// getUserMedia success callback -> pipe audio stream into audio API
function gotStream(stream) {
    // Create an audio input from the stream.
    console.log('got stream');
    var audioSource = audioApi.createMediaStreamSource(stream);
    createAnalyserNode(audioSource);
    draw();
}

function adjustFreqData(shapeNo) {
  analyserNode.getByteFrequencyData(frequencyData);
  var removed = frequencyData.slice(0,1024);
  
  var newFreqs = [], lowFreqs, midFreqs, highFreqs, prevRangeStart = 0, prevItemCount = 0;

  // set up the maxPow & thus ratio based on shapeCount
  var maxPow = Math.pow(2,shapeNo/2);
  var ratio = 1024/maxPow;
  
  // looping - get values for new array based on shapeCount
  for (let j=1; j<shapeNo+1; j++) {
    var itemCount, rangeStart;

    var pow = j/2;

    // use ratio to get itemCount (round)
    itemCount = Math.ceil( ((Math.pow(2, pow))*ratio)/2 );

    rangeStart = prevRangeStart + Math.ceil(prevItemCount/2);
     // get new values
    var newValue = 0, total = 0;
    for (let k=rangeStart; k<rangeStart+itemCount; k++) {
      // add up items and divide by total
      total += frequencyData[k];
      newValue = parseInt(total/itemCount);
    }
    // add to new array
    newFreqs.push(newValue);

    prevItemCount = itemCount;
    prevRangeStart = rangeStart;
  }

  var oneThird = Math.floor(shapeNo/3);

  function avFreqs(arrPart) {
    var arrPart = arrPart;
    var avValue;
    var totalVal = 0;
    for (let l=0; l<arrPart.length; l++) {
      totalVal += arrPart[l];
    }
    avValue = Math.floor(totalVal/arrPart.length);
    return avValue;
  }

  lowFreqs = avFreqs(newFreqs.slice(0,oneThird));
  midFreqs = avFreqs(newFreqs.slice(oneThird, oneThird*2));
  highFreqs = avFreqs(newFreqs.slice(oneThird*2));
  
  return {
    newFreqs: newFreqs,
    lowFreqs: lowFreqs,
    midFreqs: midFreqs,
    highFreqs: highFreqs
  };
}

// pipe in analysing to getUserMedia
navigator.mediaDevices.getUserMedia({ audio: true, video: false })
  .then(gotStream);


function draw() {
  requestAnimationFrame(draw);
  hex();
}

const screen = {
  width: window.innerWidth,
  height: window.innerHeight,
  centerX: window.innerWidth/2,
  centerY: window.innerHeight/2,
  maxRadius: (window.innerHeight-(window.innerHeight/6))/2,
  minRadius: (window.innerHeight/10)/2
};
// Converts from degrees to radians.
Math.radians = function(degrees) {
  return degrees * Math.PI / 180;
};

function drawHex(ctx, sideLength, startX, startY) {

  // maths mother fucker
  const moveX = Math.sin(Math.radians(30))*sideLength;
  const moveY = Math.cos(Math.radians(30))*sideLength;

  // I actually want the origin to be in the centre
  var startX = startX-(sideLength/2);
  var startY = startY-moveY;

  ctx.beginPath(); // instigate
  ctx.moveTo(startX, startY); // start at pos
  ctx.lineTo(startX+sideLength, startY); // go right along top (we're drawing clockwise from top left)

  ctx.lineTo(startX+sideLength+moveX, startY+moveY);
  ctx.lineTo(startX+sideLength, startY+(moveY*2));
  ctx.lineTo(startX, startY+(moveY*2));
  ctx.lineTo(startX-moveX, startY+moveY);
  ctx.lineTo(startX, startY);
  ctx.closePath();
}

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


            
          
!
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