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></canvas>
<div class="controls">
<button class="record" title="Record" id="toggle">
Start Recording
<svg viewBox="0 0 448 512" width="100" title="pause">
<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" />
</svg>
</button>
<button class="stop" title="Stop Recording" id="stop">
Stop Recording
<svg viewBox="0 0 448 512" width="100" title="stop">
<path d="M400 32H48C21.5 32 0 53.5 0 80v352c0 26.5 21.5 48 48 48h352c26.5 0 48-21.5 48-48V80c0-26.5-21.5-48-48-48z" />
</svg>
</button>
</div>
<audio controls />
* {
box-sizing: border-box;
}
body {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
flex-gap: 1rem;
gap: 1rem;
background: hsl(0, 0%, 20%);
}
audio {
display: none;
}
canvas {
height: 200px;
width 300px;
background: hsl(0, 0%, 10%);
box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
}
.controls {
position: relative;
}
.record,
.stop {
border-radius: 50%;
border: 4px solid white;
display: flex;
align-items: center;
justify-content: center;
font-size: 0;
height: 48px;
width: 48px;
background: none;
position: relative;
overflow: hidden;
transform: translate(calc(var(--x, 0) * 1%), calc(var(--y, 0) * 1%)) scale(var(--scale, 1));
transition: transform 0.1s;
}
.record {
color: hsl(10, 80%, 50%);
}
.record svg {
transform: scale(calc(5 - (var(--active, 0) * 4)));
transform-origin: 20% 50%;
transition: transform 0.1s;
}
.record,
.stop {
&:hover {
--scale: 1.1;
}
&:active {
--scale: 0.9;
}
}
.stop {
display: none;
position: absolute;
left: 125%;
border: none;
background: none;
top: 50%;
--y: -50;
color: hsl(0, 0%, 100%);
}
.stop svg
.record svg {
height: 65%;
fill: currentColor;
}
.reveal {
cursor: pointer;
position: fixed;
top: 1rem;
right: 1rem;
height: 48px;
width: 48px;
display: grid;
place-items: center;
padding: 0;
background: none;
border: none;
}
.reveal svg {
width: 100%;
fill: hsl(0, 0%, 6%);
}
.reveal[aria-pressed="true"] svg:nth-of-type(1),
.reveal svg:nth-of-type(2) {
display: none;
}
.reveal[aria-pressed="true"] svg:nth-of-type(2) {
display: block;
}
import gsap from 'https://cdn.skypack.dev/gsap'
import { GUI } from 'https://cdn.skypack.dev/dat.gui'
const TOGGLE = document.querySelector('#toggle')
const STOP = document.querySelector('.stop')
const AUDIO = document.querySelector('audio')
const LABEL = document.querySelector('label')
const CANVAS = document.querySelector('canvas')
const DRAWING_CONTEXT = CANVAS.getContext('2d')
let AUDIO_CONTEXT
let REPORT
let ANALYSER
CANVAS.width = CANVAS.offsetWidth
CANVAS.height = CANVAS.offsetHeight
const CONFIG = {
fft: 2048,
show: false,
duration: 0.1,
fps: 24,
barWidth: 4,
barMinHeight: 0.04,
barMaxHeight: 0.8,
barGap: 2,
}
const CTRL = new GUI()
CTRL.add(CONFIG, 'show')
.name('Show Audio')
.onChange(show => (AUDIO.style.display = show ? 'block' : 'none'))
gsap.ticker.fps(CONFIG.fps)
let visualizing = false
let recorder
let timeline = gsap.timeline()
// use for visualization
const BARS = []
const fillStyle = DRAWING_CONTEXT.createLinearGradient(
CANVAS.width / 2,
0,
CANVAS.width / 2,
CANVAS.height
)
// Color stop is three colors
fillStyle.addColorStop(0.2, 'hsl(10, 80%, 50%)')
fillStyle.addColorStop(0.8, 'hsl(10, 80%, 50%)')
fillStyle.addColorStop(0.5, 'hsl(120, 80%, 50%)')
DRAWING_CONTEXT.fillStyle = fillStyle
const drawBar = ({ x, size }) => {
const POINT_X = x - CONFIG.barWidth / 2
const POINT_Y = CANVAS.height / 2 - size / 2
DRAWING_CONTEXT.fillRect(POINT_X, POINT_Y, CONFIG.barWidth, size)
}
const drawBars = () => {
DRAWING_CONTEXT.clearRect(0, 0, CANVAS.width, CANVAS.height)
for (const BAR of BARS) {
drawBar(BAR)
}
}
const BAR_DURATION =
CANVAS.width / ((CONFIG.barWidth + CONFIG.barGap) * CONFIG.fps)
const addBar = (volume = 0) => {
const BAR = {
x: CANVAS.width + CONFIG.barWidth / 2,
// Note the volume is 0
size: gsap.utils.mapRange(
0,
100,
CANVAS.height * CONFIG.barMinHeight,
CANVAS.height * CONFIG.barMaxHeight
)(volume),
}
// Add to bars Array
BARS.push(BAR)
// Add the bar animation to the timeline
// The actual pixels per second is (1 / fps * shift) * fps
// if we have 50fps, the bar needs to have moved bar width before the next comes in
// 1/50 = 4 === 50 * 4 = 200
timeline.to(
BAR,
{
x: `-=${CANVAS.width + CONFIG.barWidth}`,
ease: 'none',
duration: BAR_DURATION,
},
BARS.length * (1 / CONFIG.fps)
)
}
// Given the canvas and the nodes Array, etc. Pad out the timeline and draw it.
const padTimeline = () => {
// Doesn't matter if we have more bars than width. We will shift them over to the correct spot
const padCount = Math.floor(CANVAS.width / CONFIG.barWidth)
// Duplicate of what happens in REPORT needs moving and refactoring.
for (let p = 0; p < padCount; p++) {
addBar()
}
// Sets the timeline to the correct spot for being added to
timeline.totalTime(timeline.totalDuration() - BAR_DURATION)
}
const ANALYSE = stream => {
AUDIO_CONTEXT = new AudioContext()
ANALYSER = AUDIO_CONTEXT.createAnalyser()
ANALYSER.fftSize = CONFIG.fft
const SOURCE = AUDIO_CONTEXT.createMediaStreamSource(stream)
const DATA_ARR = new Uint8Array(ANALYSER.frequencyBinCount)
SOURCE.connect(ANALYSER)
// Reset the bars and pad them out...
if (BARS && BARS.length > 0) {
BARS.length = 0
padTimeline()
}
REPORT = () => {
if (recorder && recorder.state === 'recording') {
ANALYSER.getByteFrequencyData(DATA_ARR)
const VOLUME = Math.floor((Math.max(...DATA_ARR) / 255) * 100)
addBar(VOLUME)
}
if (recorder || visualizing) {
drawBars()
}
}
gsap.ticker.add(REPORT)
}
const RECORD = () => {
const toggleRecording = async () => {
// If we aren't recording, we need to start a recording.
if (!recorder) {
visualizing = true
STOP.style.display = 'flex'
TOGGLE.style.setProperty('--active', 1)
// Reset the audio tag
AUDIO.removeAttribute('src')
TOGGLE.title = 'Pause Recording'
const CHUNKS = []
const MEDIA_STREAM = await window.navigator.mediaDevices.getUserMedia({
audio: true,
})
recorder = new MediaRecorder(MEDIA_STREAM)
// This signals stopping the recording. Only accessible via the "Stop" button.
recorder.ondataavailable = event => {
// Update the UI
TOGGLE.style.setProperty('--active', 0)
STOP.style.display = 'none'
TOGGLE.title = 'Start Recording'
// Create the blob and show an audio element
CHUNKS.push(event.data)
const AUDIO_BLOB = new Blob(CHUNKS, { type: 'audio/mp3' })
AUDIO.setAttribute('src', window.URL.createObjectURL(AUDIO_BLOB))
// Tear down after recording.
recorder.stream.getTracks().forEach(t => t.stop())
recorder = null
}
recorder.start()
timeline.play()
ANALYSE(recorder.stream)
} else {
const RECORDING = recorder.state === 'recording'
// Pause or resume recorder based on state.
TOGGLE.style.setProperty('--active', RECORDING ? 0 : 1)
timeline[RECORDING ? 'pause' : 'play']()
recorder[RECORDING ? 'pause' : 'resume']()
}
}
// Don't pause or restart it now. Start initially, pause, then make Stop available.
toggleRecording()
}
TOGGLE.addEventListener('click', RECORD)
STOP.addEventListener('click', () => {
// Stop the recorder and return the nodes back to where they were
if (recorder) recorder.stop()
timeline.clear()
gsap.ticker.remove(REPORT)
AUDIO_CONTEXT.close()
})
// INITIAL RENDERING so we don't have a black box.
padTimeline()
drawBars()
Also see: Tab Triggers