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

Auto Save

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>
<button class="record" title="Record" id="toggle">Start Recording</button>
<button class="reveal" aria-pressed="false" title="Show audio">
  <svg viewBox="0 0 640 512" width="100" title="eye-slash">
    <path d="M320 400c-75.85 0-137.25-58.71-142.9-133.11L72.2 185.82c-13.79 17.3-26.48 35.59-36.72 55.59a32.35 32.35 0 0 0 0 29.19C89.71 376.41 197.07 448 320 448c26.91 0 52.87-4 77.89-10.46L346 397.39a144.13 144.13 0 0 1-26 2.61zm313.82 58.1l-110.55-85.44a331.25 331.25 0 0 0 81.25-102.07 32.35 32.35 0 0 0 0-29.19C550.29 135.59 442.93 64 320 64a308.15 308.15 0 0 0-147.32 37.7L45.46 3.37A16 16 0 0 0 23 6.18L3.37 31.45A16 16 0 0 0 6.18 53.9l588.36 454.73a16 16 0 0 0 22.46-2.81l19.64-25.27a16 16 0 0 0-2.82-22.45zm-183.72-142l-39.3-30.38A94.75 94.75 0 0 0 416 256a94.76 94.76 0 0 0-121.31-92.21A47.65 47.65 0 0 1 304 192a46.64 46.64 0 0 1-1.54 10l-73.61-56.89A142.31 142.31 0 0 1 320 112a143.92 143.92 0 0 1 144 144c0 21.63-5.29 41.79-13.9 60.11z" />
  </svg>
  <svg viewBox="0 0 576 512" width="100" title="eye">
    <path d="M572.52 241.4C518.29 135.59 410.93 64 288 64S57.68 135.64 3.48 241.41a32.35 32.35 0 0 0 0 29.19C57.71 376.41 165.07 448 288 448s230.32-71.64 284.52-177.41a32.35 32.35 0 0 0 0-29.19zM288 400a144 144 0 1 1 144-144 143.93 143.93 0 0 1-144 144zm0-240a95.31 95.31 0 0 0-25.31 3.79 47.85 47.85 0 0 1-66.9 66.9A95.78 95.78 0 1 0 288 160z" />
  </svg>
</button>
<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%);
}

audio {
  display: none;
}

canvas {
  height: 300px;
  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);
}

.record {
  border-radius: 50%;
  border: 4px solid white;
  font-size: 0;
  height: 48px;
  width: 48px;
  background: none;
  position: relative;
  overflow: hidden;
}

.record:after {
  content: '';
  position: absolute;
  background: hsl(10, 80%, 50%);
  height: 100%;
  width: 100%;
  top: 50%;
  left: 50%;
  cursor: pointer;
  transform: translate(-50%, -50%) scale(calc(1 - (var(--active, 0) * 0.5)));
  border-radius: 15%;
  transition: transform 0.1s;
}

.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'

const TOGGLE = document.querySelector('#toggle')
const AUDIO = document.querySelector('audio')
const LABEL = document.querySelector('label')
const CANVAS = document.querySelector('canvas')
const DRAWING_CONTEXT = CANVAS.getContext('2d')
const REVEAL = document.querySelector('.reveal')

REVEAL.addEventListener('click', () => {
  const REVEALED = REVEAL.getAttribute('aria-pressed') === 'true' ? false: true
  REVEAL.setAttribute('aria-pressed', REVEALED)
  REVEAL.title = REVEALED ? 'Hide audio' : 'Show audio'
  AUDIO.style.display = REVEALED ? 'block' : 'none'
})

let AUDIO_CONTEXT
let REPORT

CANVAS.width = CANVAS.height = 300

const CONFIG = {
  fps: 60,
  duration: 0.1,
  fft: 32,
}

let visualizing = false
let recorder

// use for visualization
let squares = []

const genSquares = (size) => {
  squares = new Array(size).fill().map(node => ({
    x: gsap.utils.random(0, CANVAS.width),
    y: gsap.utils.random(0, CANVAS.height),
    size: gsap.utils.random(5, 20),
    scale: 1,
    hue: 90,
  }))
}

const drawSquare = ({ x, y, size, scale, hue }) => {
  const SQUARE_SIZE = scale * size
  const SQUARE_POINT_X = x - SQUARE_SIZE / 2
  const SQUARE_POINT_Y = y - SQUARE_SIZE / 2
  DRAWING_CONTEXT.fillStyle = `hsl(${hue}, 80%, 50%)`
  DRAWING_CONTEXT.fillRect(SQUARE_POINT_X, SQUARE_POINT_Y, SQUARE_SIZE, SQUARE_SIZE)  
}

const drawSquares = () => {
  DRAWING_CONTEXT.clearRect(0, 0, CANVAS.width, CANVAS.height)
  for (const square of squares) {
    drawSquare(square)
  }
}

const updateSquares = () => {
  // give the squares a new position on the map   
  gsap.to(squares, {
    x: () => gsap.utils.random(0, CANVAS.width),
    y: () => gsap.utils.random(0, CANVAS.height),
    size: () => gsap.utils.random(5, 20),
    duration: CONFIG.duration * 5,
  })
}

genSquares(CONFIG.fft * 0.5)
drawSquares()

const ANALYSE = stream => {
  AUDIO_CONTEXT = new AudioContext()
  const ANALYSER = AUDIO_CONTEXT.createAnalyser()
  ANALYSER.fftSize = 32
  const SOURCE = AUDIO_CONTEXT.createMediaStreamSource(stream)
  const DATA_ARR = new Uint8Array(ANALYSER.frequencyBinCount)
  SOURCE.connect(ANALYSER)
  
  if (!squares || (squares.length !== DATA_ARR.length)) genSquares(DATA_ARR.length)
  else updateSquares()
 
  
  REPORT = () => {
    if (recorder) {
      ANALYSER.getByteFrequencyData(DATA_ARR)
      const VOLUME = Math.floor((Math.max(...DATA_ARR) / 255) * 100)
      // At this point change the size of nodes
      gsap.to(squares, {
        duration: CONFIG.duration,
        scale: index => {
          const newScale = DATA_ARR[index] / 255
          return 1 + gsap.utils.mapRange(0, 1, 0, 5)(newScale)
        },
        hue: index => {
          return gsap.utils.mapRange(0, 1, 90, 0)(DATA_ARR[index] / 255)
        }
      })
    }
    if (recorder || visualizing) {
      drawSquares()
    }
  }
  gsap.ticker.add(REPORT)
}


const RECORD = () => {
  const toggleRecording = async () => {
    if (!recorder) {
      visualizing = true
      // Reset the audio tag
      AUDIO.removeAttribute('src')
      TOGGLE.style.setProperty('--active', 1)
      TOGGLE.title = TOGGLE.innerText = 'Stop Recording'
      const CHUNKS = []
      const MEDIA_STREAM = await window.navigator.mediaDevices.getUserMedia({
        audio: true
      })
      recorder = new MediaRecorder(MEDIA_STREAM)
      recorder.ondataavailable = event => {
        // Update the UI
        TOGGLE.style.setProperty('--active', 0)
        TOGGLE.innerText = 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()
      ANALYSE(recorder.stream)
    } else {
      // Stop the recorder and return the nodes back to where they were       
      recorder.stop()
      gsap.to(squares, {
        duration: CONFIG.duration,
        scale: 1,
        hue: 90,
        onComplete: () => {
          visualizing = false
          gsap.ticker.remove(REPORT)
          AUDIO_CONTEXT.close()
        }
      })
    }
  }
  toggleRecording()
}

TOGGLE.addEventListener('click', RECORD)
              
            
!
999px

Console