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 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.
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.
If the stylesheet 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 CSS 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.
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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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 width="460" height="320"></canvas>
<div class="call-to-action">Click to play!</div>
html, body {
height: 100%;
}
body {
background-image: radial-gradient(circle, #F0C505, #F76E2A 10%, #A71A5B 40%, #651366);
position: relative;
overflow: hidden;
}
canvas {
display: block;
}
.call-to-action {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #FFF;
cursor: pointer;
}
let canvas = document.querySelector("canvas");
let ctx = canvas.getContext("2d");
canvas.width = document.body.clientWidth;
canvas.height = document.body.clientHeight;
let centerX = canvas.width / 2;
let centerY = canvas.height / 2;
let radius = document.body.clientWidth <= 425 ? 120 : 160;
let steps = document.body.clientWidth <= 425 ? 60 : 120;
let interval = 360 / steps;
let pointsUp = [];
let pointsDown = [];
let running = false;
let pCircle = 2 * Math.PI * radius;
let angleExtra = 90;
// Create points
for(let angle = 0; angle < 360; angle += interval) {
let distUp = 1.1;
let distDown = 0.9;
pointsUp.push({
angle: angle + angleExtra,
x: centerX + radius * Math.cos((-angle + angleExtra) * Math.PI / 180) * distUp,
y: centerY + radius * Math.sin((-angle + angleExtra) * Math.PI / 180) * distUp,
dist: distUp
});
pointsDown.push({
angle: angle + angleExtra + 5,
x: centerX + radius * Math.cos((-angle + angleExtra + 5) * Math.PI / 180) * distDown,
y: centerY + radius * Math.sin((-angle + angleExtra + 5) * Math.PI / 180) * distDown,
dist: distDown
});
}
// -------------
// Audio stuff
// -------------
// make a Web Audio Context
const context = new AudioContext();
const splitter = context.createChannelSplitter();
const analyserL = context.createAnalyser();
analyserL.fftSize = 8192;
const analyserR = context.createAnalyser();
analyserR.fftSize = 8192;
splitter.connect(analyserL, 0, 0);
splitter.connect(analyserR, 1, 0);
// Make a buffer to receive the audio data
const bufferLengthL = analyserL.frequencyBinCount;
const audioDataArrayL = new Uint8Array(bufferLengthL);
const bufferLengthR = analyserR.frequencyBinCount;
const audioDataArrayR = new Uint8Array(bufferLengthR);
// Make a audio node
const audio = new Audio();
function loadAudio() {
audio.loop = false;
audio.autoplay = false;
audio.crossOrigin = "anonymous";
// call `handleCanplay` when it music can be played
audio.addEventListener('canplay', handleCanplay);
audio.src = "https://s3.eu-west-2.amazonaws.com/nelsoncodepen/Audiobinger_-_The_Garden_State.mp3";
audio.load();
running = true;
}
function handleCanplay() {
// connect the audio element to the analyser node and the analyser node
// to the main Web Audio context
const source = context.createMediaElementSource(audio);
source.connect(splitter);
splitter.connect(context.destination);
}
function toggleAudio() {
if (running === false) {
loadAudio();
document.querySelector('.call-to-action').remove();
}
if (audio.paused) {
audio.play();
} else {
audio.pause();
}
}
canvas.addEventListener('click', toggleAudio);
document.body.addEventListener('touchend', function(ev) {
context.resume();
});
// -------------
// Canvas stuff
// -------------
function drawLine(points) {
let origin = points[0];
ctx.beginPath();
ctx.strokeStyle = 'rgba(255,255,255,0.5)';
ctx.lineJoin = 'round';
ctx.moveTo(origin.x, origin.y);
for (let i = 0; i < points.length; i++) {
ctx.lineTo(points[i].x, points[i].y);
}
ctx.lineTo(origin.x, origin.y);
ctx.stroke();
}
function connectPoints(pointsA, pointsB) {
for (let i = 0; i < pointsA.length; i++) {
ctx.beginPath();
ctx.strokeStyle = 'rgba(255,255,255,0.5)';
ctx.moveTo(pointsA[i].x, pointsA[i].y);
ctx.lineTo(pointsB[i].x, pointsB[i].y);
ctx.stroke();
}
}
function update(dt) {
let audioIndex, audioValue;
// get the current audio data
analyserL.getByteFrequencyData(audioDataArrayL);
analyserR.getByteFrequencyData(audioDataArrayR);
for (let i = 0; i < pointsUp.length; i++) {
audioIndex = Math.ceil(pointsUp[i].angle * (bufferLengthL / (pCircle * 2))) | 0;
// get the audio data and make it go from 0 to 1
audioValue = audioDataArrayL[audioIndex] / 255;
pointsUp[i].dist = 1.1 + audioValue * 0.8;
pointsUp[i].x = centerX + radius * Math.cos(-pointsUp[i].angle * Math.PI / 180) * pointsUp[i].dist;
pointsUp[i].y = centerY + radius * Math.sin(-pointsUp[i].angle * Math.PI / 180) * pointsUp[i].dist;
audioIndex = Math.ceil(pointsDown[i].angle * (bufferLengthR / (pCircle * 2))) | 0;
// get the audio data and make it go from 0 to 1
audioValue = audioDataArrayR[audioIndex] / 255;
pointsDown[i].dist = 0.9 + audioValue * 0.2;
pointsDown[i].x = centerX + radius * Math.cos(-pointsDown[i].angle * Math.PI / 180) * pointsDown[i].dist;
pointsDown[i].y = centerY + radius * Math.sin(-pointsDown[i].angle * Math.PI / 180) * pointsDown[i].dist;
}
}
function draw(dt) {
requestAnimationFrame(draw);
if (running) {
update(dt);
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawLine(pointsUp);
drawLine(pointsDown);
connectPoints(pointsUp, pointsDown);
}
draw();
Also see: Tab Triggers