Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <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 />  
              
            
!

CSS

              
                * {
  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%);
}

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;
}
              
            
!

JS

              
                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
let START_POINT

CANVAS.width = CANVAS.offsetWidth
CANVAS.height = CANVAS.offsetHeight

const CONFIG = {
  fft: 2048,
  show: true,
  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()
  }
  START_POINT = timeline.totalDuration() - BAR_DURATION
  // Sets the timeline to the correct spot for being added to
  timeline.totalTime(START_POINT)
}

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()
  }
  if (REPORT) gsap.ticker.remove(REPORT)
  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 timeline
      timeline.clear()
      // Reset the audio tag
      AUDIO.removeAttribute('src')
      AUDIO.removeAttribute('controls')
      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)

const SCRUB = (time = 0, trackTime = 0) => {
  gsap.to(timeline, {
    totalTime: time,
    onComplete: () => {
      AUDIO.currentTime = trackTime
    },
  })
}

STOP.addEventListener('click', () => {
  if (recorder) recorder.stop()
  AUDIO.setAttribute('controls', true)
  AUDIO_CONTEXT.close()
  timeline.pause()
  SCRUB(START_POINT)
})

// INITIAL RENDERING so we don't have a black box.
padTimeline()
drawBars()

const UPDATE = e => {
  switch (e.type) {
    case 'play':
      timeline.totalTime(AUDIO.currentTime + START_POINT)
      timeline.play()
      break
    case 'seeking':
    case 'seeked':
      timeline.totalTime(AUDIO.currentTime + START_POINT)
      break
    case 'pause':
      timeline.pause()
      break
    case 'ended':
      timeline.pause()
      SCRUB(START_POINT)
      break
  }
}

// Set up AUDIO scrubbing
['play', 'seeking', 'seeked', 'pause', 'ended'].forEach(event => AUDIO.addEventListener(event, UPDATE))

              
            
!
999px

Console