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.
<!-- so yeh me and michells had a pint and then this happened: michelle did the animated grid and i added some sounds -->
<div class="grid">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<section id="controls">
<button data-playing="null" class="controls__button">
<span>Play/Pause</span>
</button>
<section class="controls__credit">
<p>Music by</p>
<p class="credit--artist">
<a href="https://2xaa.fm/" labelledby="artist">
<span id="artist">2xaa</span>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="-537.4 426.7 840.8 229.4"><path class="st0" d="M-92,492.1l-0.2-0.2h-99.3l-25.1,33.1l99,130.8l0.2,0.2h50.7l-49.3-65.2h48.1l49.2,65l0.1,0.2h50.7L-92,492.1z
M-141.5,557.1l-23.9-31.6h48.1l23.9,31.6H-141.5z M154.9,426.9l-0.2-0.2h-543.6L-512.5,590l-24.9,32.9l25.1,33.1h346.9l-75.1-99.2
l-49.6,65.6h-48.1l73.7-97.3l-25.3-33.5l-49.6,65.6h-49.4l-25.5,33.6h49.3l-23.9,31.6h-97.5l73.3-96.8h49.4l25.5-33.6h-49.4
l23.9-31.6h98.3l24.9,33l24.9-33h345.1l122.7,162.1h-48.1l-98.6-130.3l-0.1-0.2H6.1L-19,525.1l99,130.8l0.2,0.2h50.7l-49.4-65.2
h48.1l49.2,65l0.2,0.2h99.3l25.1-33.1L154.9,426.9z M56,557.1l-23.9-31.6h48.1l23.9,31.6H56z" /></svg>
</a>
</p>
</section>
<section class="controls__credit">
<p>on</p>
<p class="credit--platform">
<a href="https://soundcloud.com/2xaa" labelledby="platform">
<span id="platform">Soundcloud</span>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><path d="M111.4 256.3l5.8 65-5.8 68.3c-.3 2.5-2.2 4.4-4.4 4.4s-4.2-1.9-4.2-4.4l-5.6-68.3 5.6-65c0-2.2 1.9-4.2 4.2-4.2 2.2 0 4.1 2 4.4 4.2zm21.4-45.6c-2.8 0-4.7 2.2-5 5l-5 105.6 5 68.3c.3 2.8 2.2 5 5 5 2.5 0 4.7-2.2 4.7-5l5.8-68.3-5.8-105.6c0-2.8-2.2-5-4.7-5zm25.5-24.1c-3.1 0-5.3 2.2-5.6 5.3l-4.4 130 4.4 67.8c.3 3.1 2.5 5.3 5.6 5.3 2.8 0 5.3-2.2 5.3-5.3l5.3-67.8-5.3-130c0-3.1-2.5-5.3-5.3-5.3zM7.2 283.2c-1.4 0-2.2 1.1-2.5 2.5L0 321.3l4.7 35c.3 1.4 1.1 2.5 2.5 2.5s2.2-1.1 2.5-2.5l5.6-35-5.6-35.6c-.3-1.4-1.1-2.5-2.5-2.5zm23.6-21.9c-1.4 0-2.5 1.1-2.5 2.5l-6.4 57.5 6.4 56.1c0 1.7 1.1 2.8 2.5 2.8s2.5-1.1 2.8-2.5l7.2-56.4-7.2-57.5c-.3-1.4-1.4-2.5-2.8-2.5zm25.3-11.4c-1.7 0-3.1 1.4-3.3 3.3L47 321.3l5.8 65.8c.3 1.7 1.7 3.1 3.3 3.1 1.7 0 3.1-1.4 3.1-3.1l6.9-65.8-6.9-68.1c0-1.9-1.4-3.3-3.1-3.3zm25.3-2.2c-1.9 0-3.6 1.4-3.6 3.6l-5.8 70 5.8 67.8c0 2.2 1.7 3.6 3.6 3.6s3.6-1.4 3.9-3.6l6.4-67.8-6.4-70c-.3-2.2-2-3.6-3.9-3.6zm241.4-110.9c-1.1-.8-2.8-1.4-4.2-1.4-2.2 0-4.2.8-5.6 1.9-1.9 1.7-3.1 4.2-3.3 6.7v.8l-3.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6.9-6.9z"/></svg>
</a>
</p>
</section>
:root {
--beat: 50;
--bright: 64;
--padding: 8px;
}
* {
box-sizing: border-box;
}
body {
background-color: #23262d;
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
max-width: 400px;
height: 400px;
gap: 20px;
margin: 80px auto;
animation: resize calc(var(--beat)*60ms) ease infinite both;
}
.item {
--delay: 0ms;
animation: colorChange 3000ms ease var(--delay) infinite both;
&:nth-child(4n - 2) {
--delay: 1000ms;
}
&:nth-child(4n) {
--delay: 2000ms;
}
}
@keyframes colorChange {
0% {
background-color: hsl(36, 100%, calc(64%));
}
25% {
background-color: hsl(76, 100%, calc(var(--bright)*1%));
}
50% {
background-color: hsl(206, 100%, calc(var(--bright)*1%));
}
75% {
background-color: hsl(305, 100%, calc(var(--bright)*1%));
}
100% {
background-color: hsl(36, 100%, calc(var(--bright)*1%));
}
}
@keyframes resize {
0% {
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
}
25% {
grid-template-columns: 1fr 2fr 3fr;
grid-template-rows: 1fr 2fr 3fr;
}
50% {
grid-template-columns: 1fr 5fr 1fr;
grid-template-rows: 1fr 5fr 1fr;
}
75% {
grid-template-columns: 3fr 2fr 1fr;
grid-template-rows: 3fr 2fr 1fr;
}
100% {
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
}
}
#controls {
box-sizing: border-box;
width: 12vw; min-width: 200px;
position: absolute; bottom: 5vh; right: 16vw;
// transform: translate(6vw, 0%);
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto;
}
.controls__button {
grid-area: 1 / 1 / 2 / 2;
justify-self: stretch; align-self: stretch;
padding: var(--padding);
background: black url('data:image/svg+xml;charset=utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M424.4 214.7L72.4 6.6C43.8-10.3 0 6.1 0 47.9V464c0 37.5 40.7 60.1 72.4 41.3l352-208c31.4-18.5 31.5-64.1 0-82.6z" fill="white" /></svg>') no-repeat center center;
background-size: 40% 40%;
border: 2px solid white; border-right-width: 0px;
cursor: pointer;
}
.controls__button:hover {
background: white url('data:image/svg+xml;charset=utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M424.4 214.7L72.4 6.6C43.8-10.3 0 6.1 0 47.9V464c0 37.5 40.7 60.1 72.4 41.3l352-208c31.4-18.5 31.5-64.1 0-82.6z" fill="black" /></svg>') no-repeat center center;
background-size: 40% 40%;
border-color: black;
}
.controls__button.button--on {
background: black url('data:image/svg+xml;charset=utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M144 479H48c-26.5 0-48-21.5-48-48V79c0-26.5 21.5-48 48-48h96c26.5 0 48 21.5 48 48v352c0 26.5-21.5 48-48 48zm304-48V79c0-26.5-21.5-48-48-48h-96c-26.5 0-48 21.5-48 48v352c0 26.5 21.5 48 48 48h96c26.5 0 48-21.5 48-48z" fill="white" /></svg>') no-repeat center center;
background-size: 40% 40%;
}
.controls__button.button--on:hover {
background: white url('data:image/svg+xml;charset=utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M144 479H48c-26.5 0-48-21.5-48-48V79c0-26.5 21.5-48 48-48h96c26.5 0 48 21.5 48 48v352c0 26.5-21.5 48-48 48zm304-48V79c0-26.5-21.5-48-48-48h-96c-26.5 0-48 21.5-48 48v352c0 26.5 21.5 48 48 48h96c26.5 0 48-21.5 48-48z" fill="black" /></svg>') no-repeat center center;
background-size: 40% 40%;
}
span {visibility: hidden;}
.controls__credit {
box-sixing: border-box;
padding: var(--padding) var(--padding) 0px;
background-color: white; border: 2px solid black;
color: black;
}
.controls__credit:nth-of-type(1) {
grid-area: 1 / 2 / 2 / 3;
border-width: 2px 0px;
p {padding-bottom: var(--padding);}
svg {width: 80px; padding-top: var(--padding);}
}
.controls__credit:nth-of-type(2) {
grid-area: 1 / 3 / 2 / 4;
border-left-width:0px;
}
p {
text-align: center;}
p span {display: none;}
.credit--artist {}
.credit--platform {}
svg {width: 60px;}
// if value is above a certain amount - update beat
// pipe value into brightness
console.clear();
// some utility functions
const arrAvg = arr => arr.reduce((a,b) => a + b, 0) / arr.length;
// at some point refactor this to use viz colours
// const colourPalette =
//~~~~~~~~~~~~~~~~~~~~~~~~~SOUNDCLOUD STUFF
// Massive thanks to @jake_albaugh for letting me use his key. Also a lot of the soundcloud stuff is influenced by https://codepen.io/jakealbaugh/pen/MyJXoK?editors=0010 which you should check out!
const track_id = "433074246";
const client_id = "787c22af89922d1be9202d2f0cc90586";
const src_url = "https://api.soundcloud.com/tracks/" + track_id + "/stream?client_id=" + client_id;
const controls = document.querySelector('button');
let track = new Audio();
track.crossOrigin = "anonymous";
function initTrack(url, trackInstance) {
let trackInst = trackInstance;
trackInst.src = url;
trackInst.crossOrigin = "anonymous";
trackInst.play();
}
function playPause(playing, trackInstance) {
let trackInst = trackInstance;
if (playing) {
trackInst.pause();
} else {
trackInst.play();
}
}
controls.addEventListener("click", function() {
// check if context is in suspended state (autoplay policy)
if (audCtx.state === 'suspended') {
audCtx.resume();
}
this.classList.toggle('button--on');
// first click - initiate soundcloud call
if (this.dataset.playing === 'null') {
initTrack(src_url, track);
this.dataset.playing = "true";
// and set up audio graph
connectGraph(audCtx, track, analyserNode);
} else { // toggle play
let play = this.dataset.playing === "true" ? true : false;
this.dataset.playing = this.dataset.playing === "true" ? "false" : "true";
playPause(play, track);
}
}, false);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ AUDIO STUFF
// set up audio canCtx & analyser
const binSize = 2048;
let bin = new Uint8Array(binSize);
const audCtx = new (window.AudioContext || window.webkitAudioContext);
const analyserNode = new AnalyserNode(audCtx, {
fftSize: binSize*2,
maxDecibels: -25,
minDecibels: -60,
smoothingTimeConstant: 0.5,
});
function connectGraph(ctx, trackInstance, node) {
let trackInst = trackInstance,
audioCtx = ctx,
analyserNode = node;
const source = audioCtx.createMediaElementSource(trackInst);
source.connect(analyserNode).connect(audioCtx.destination);
}
function analyse(node) {
let analyserNode = node;
analyserNode.getByteFrequencyData(bin);
// console.log(bin);
}
// visual setup
const beatThreshold = 30; // average gain for change
const debounceRate = 20; // lower = higher frame count
let debounce = 0;
let directions = [-1, 0, 1];
function draw() {
requestAnimationFrame(draw);
analyse(analyserNode);
let newBin = bin.slice(0, binSize/16);
// if the first half of the array averages over 200, change lines
let binAvg = arrAvg(newBin);
document.documentElement.style.setProperty('--bright', binAvg+20);
if ( (binAvg > beatThreshold) && (debounce === 0) ) {
document.documentElement.style.setProperty('--beat', binAvg);
// startSteps === 4 ? startSteps = 2 : startSteps++;
debounce = debounceRate;
}
debounce > 0 ? debounce-- : debounce = 0;
}
draw();
Also see: Tab Triggers