Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's 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 it's URL and the proper URL extention.

+ 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>
<button class="record" title="Record" id="toggle">Start Recording</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'
import { GUI } from 'https://cdn.skypack.dev/dat.gui'

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

let AUDIO_CONTEXT
let REPORT
let ANALYSER

CANVAS.width = CANVAS.height = 300

const CONFIG = {
  fps: 60,
  duration: 0.1,
  fft: 32,
  fftPlaceholder: 4,
  show: false
}

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 updateFft = value => {
  const NEW_FFT = Math.pow(2, value)
  CONFIG.fft = NEW_FFT
  if (ANALYSER) ANALYSER.fftSize = NEW_FFT
  genSquares(NEW_FFT * 0.5)
  drawSquares()
}
const CTRL = new GUI()
CTRL.add(CONFIG, 'fftPlaceholder', 4, 10, 1).name('fftSize (power)').onChange(updateFft)
CTRL.add(CONFIG, 'show').name('Show Audio').onChange(show => AUDIO.style.display = show ? 'block' : 'none')

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()
  ANALYSER = AUDIO_CONTEXT.createAnalyser()
  ANALYSER.fftSize = CONFIG.fft
  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