HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<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');
Also see: Tab Triggers