Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

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

<div id="screen">
  <canvas></canvas>
</div>
              
            
!

CSS

              
                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;
}


              
            
!

JS

              
                // 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

Console