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.
<canvas id="canvas"></canvas>
<button onClick="start()">Start</button>
body {
background: radial-gradient(farthest-side, #182158 0%, #030414 100%) no-repeat fixed 0 0;
margin: 0;
}
h1 {
color: #fff;
font: 10vh/1.2 sans-serif;
}
#canvas {
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 100%;
height: 400px;
}
button {
position: absolute;
top: 50%;
left: 50%;
transform: translate3d(-50%,-50%,0);
font-size: 2vw;
border-radius: 9em;
padding: 0.5em 1.5em;
border: none;
background: rgba(255,255,255,0.8);
}
// the canvas size
const WIDTH = 1000;
const HEIGHT = 400;
const ctx = canvas.getContext("2d");
// options to tweak the look
const opts = {
smoothing: 0.6,
fft: 8,
minDecibels: -70,
scale: 0.2,
glow: 10,
color1: [203, 36, 128],
color2: [41, 200, 192],
color3: [24, 137, 218],
fillOpacity: 0.6,
lineWidth: 1,
blend: "screen",
shift: 50,
width: 60,
amp: 1
};
// Interactive dat.GUI controls
const gui = new dat.GUI();
// hide them by default
gui.close();
// connect gui to opts
gui.addColor(opts, "color1");
gui.addColor(opts, "color2");
gui.addColor(opts, "color3");
gui.add(opts, "fillOpacity", 0, 1);
gui.add(opts, "lineWidth", 0, 10).step(1);
gui.add(opts, "glow", 0, 100);
gui.add(opts, "blend", [
"normal",
"multiply",
"screen",
"overlay",
"lighten",
"difference"
]);
gui.add(opts, "smoothing", 0, 1);
gui.add(opts, "minDecibels", -100, 0);
gui.add(opts, "amp", 0, 5);
gui.add(opts, "width", 0, 60);
gui.add(opts, "shift", 0, 200);
let context;
let analyser;
// Array to hold the analyzed frequencies
let freqs;
navigator.getUserMedia =
navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia;
/**
* Create an input source from the user media stream, connect it to
* the analyser and start the visualization.
*/
function onStream(stream) {
console.log("onStream");
const input = context.createMediaStreamSource(stream);
input.connect(analyser);
requestAnimationFrame(visualize);
}
/**
* Display an error message.
*/
function onStreamError(e) {
document.body.innerHTML = "<h1>This pen only works with https://</h1>";
console.error(e);
}
/**
* Utility function to create a number range
*/
function range(i) {
return Array.from(Array(i).keys());
}
// shuffle frequencies so that neighbors are not too similar
const shuffle = [1, 3, 0, 4, 2];
/**
* Pick a frequency for the given channel and value index.
*
* The channel goes from 0 to 2 (R/G/B)
* The index goes from 0 to 4 (five peaks in the curve)
*
* We have 2^opts.fft frequencies to choose from and
* we want to visualize most of the spectrum. This function
* returns the bands from 0 to 28 in a nice distribution.
*/
function freq(channel, i) {
const band = 2 * channel + shuffle[i] * 6;
return freqs[band];
}
/**
* Returns the scale factor fot the given value index.
* The index goes from 0 to 4 (curve with 5 peaks)
*/
function scale(i) {
const x = Math.abs(2 - i); // 2,1,0,1,2
const s = 3 - x; // 1,2,3,2,1
return s / 3 * opts.amp;
}
/**
* This function draws a path that roughly looks like this:
* .
* __/\_/ \_/\__
* \/ \ / \/
* '
* 1 2 3 4 5
*
* The function is called three times (with channel 0/1/2) so that the same
* basic shape is drawn in three different colors, slightly shifted and
* each visualizing a different set of frequencies.
*/
function path(channel) {
// Read color1, color2, color2 from the opts
const color = opts[`color${channel + 1}`].map(Math.floor);
// turn the [r,g,b] array into a rgba() css color
ctx.fillStyle = `rgba(${color}, ${opts.fillOpacity})`;
// set stroke and shadow the same solid rgb() color
ctx.strokeStyle = ctx.shadowColor = `rgb(${color})`;
ctx.lineWidth = opts.lineWidth;
ctx.shadowBlur = opts.glow;
ctx.globalCompositeOperation = opts.blend;
const m = HEIGHT / 2; // the vertical middle of the canvas
// for the curve with 5 peaks we need 15 control points
// calculate how much space is left around it
const offset = (WIDTH - 15 * opts.width) / 2;
// calculate the 15 x-offsets
const x = range(15).map(
i => offset + channel * opts.shift + i * opts.width
);
// pick some frequencies to calculate the y values
// scale based on position so that the center is always bigger
const y = range(5).map(i =>
Math.max(0, m - scale(i) * freq(channel, i))
);
const h = 2 * m;
ctx.beginPath();
ctx.moveTo(0, m); // start in the middle of the left side
ctx.lineTo(x[0], m + 1); // straight line to the start of the first peak
ctx.bezierCurveTo(x[1], m + 1, x[2], y[0], x[3], y[0]); // curve to 1st value
ctx.bezierCurveTo(x[4], y[0], x[4], y[1], x[5], y[1]); // 2nd value
ctx.bezierCurveTo(x[6], y[1], x[6], y[2], x[7], y[2]); // 3rd value
ctx.bezierCurveTo(x[8], y[2], x[8], y[3], x[9], y[3]); // 4th value
ctx.bezierCurveTo(x[10], y[3], x[10], y[4], x[11], y[4]); // 5th value
ctx.bezierCurveTo(x[12], y[4], x[12], m, x[13], m); // curve back down to the middle
ctx.lineTo(1000, m + 1); // straight line to the right edge
ctx.lineTo(x[13], m - 1); // and back to the end of the last peak
// now the same in reverse for the lower half of out shape
ctx.bezierCurveTo(x[12], m, x[12], h - y[4], x[11], h - y[4]);
ctx.bezierCurveTo(x[10], h - y[4], x[10], h - y[3], x[9], h - y[3]);
ctx.bezierCurveTo(x[8], h - y[3], x[8], h - y[2], x[7], h - y[2]);
ctx.bezierCurveTo(x[6], h - y[2], x[6], h - y[1], x[5], h - y[1]);
ctx.bezierCurveTo(x[4], h - y[1], x[4], h - y[0], x[3], h - y[0]);
ctx.bezierCurveTo(x[2], h - y[0], x[1], m, x[0], m);
ctx.lineTo(0, m); // close the path by going back to the start
ctx.fill();
ctx.stroke();
}
/**
* requestAnimationFrame handler that drives the visualization
*/
function visualize() {
// set analysert props in the loop react on dat.gui changes
analyser.smoothingTimeConstant = opts.smoothing;
analyser.fftSize = Math.pow(2, opts.fft);
analyser.minDecibels = opts.minDecibels;
analyser.maxDecibels = 0;
analyser.getByteFrequencyData(freqs);
// set size to clear the canvas on each frame
canvas.width = WIDTH;
canvas.height = HEIGHT;
// draw three curves (R/G/B)
path(0);
path(1);
path(2);
// schedule next paint
requestAnimationFrame(visualize);
}
function start() {
context = new AudioContext();
analyser = context.createAnalyser();
freqs = new Uint8Array(analyser.frequencyBinCount);
document.querySelector("button").remove();
navigator.getUserMedia({ audio: true }, onStream, onStreamError);
}
Also see: Tab Triggers