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

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

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 CANVAS = document.querySelector('canvas')
const CONTEXT = CANVAS.getContext('2d')

CANVAS.width = CANVAS.height = CANVAS.offsetHeight


const CONFIG = {
  duration: 0.1,
}

const SQUARE = {
  size: 40,
  scale: 1,
  hue: 100,
}

const drawSquare = () => {
  const SQUARE_SIZE = SQUARE.scale * SQUARE.size
  const SQUARE_POINT = CANVAS.width / 2 - SQUARE_SIZE / 2
  CONTEXT.clearRect(0, 0, CANVAS.width, CANVAS.height)
  CONTEXT.fillStyle = `hsl(${SQUARE.hue}, 80%, 50%)`
  CONTEXT.fillRect(SQUARE_POINT, SQUARE_POINT, SQUARE_SIZE, SQUARE_SIZE)  
}

// Draw the square initially
drawSquare()

let recorder
let report
let audioContext

const ANALYSE = stream => {
  audioContext = new AudioContext()
  const ANALYSER = audioContext.createAnalyser()
  const SOURCE = audioContext.createMediaStreamSource(stream)
  const DATA_ARR = new Uint8Array(ANALYSER.frequencyBinCount)
  SOURCE.connect(ANALYSER)
  report = () => {
    if (recorder) {
      ANALYSER.getByteFrequencyData(DATA_ARR)
      const VOLUME = Math.max(...DATA_ARR) / 255
      gsap.to(SQUARE, {
        duration: CONFIG.duration,
        hue: gsap.utils.mapRange(0, 1, 100, 0)(VOLUME),
        scale: gsap.utils.mapRange(0, 1, 1, 5)(VOLUME)
      })      
    }
    // render square
    drawSquare()
  }
  gsap.ticker.add(report)
}


const RECORD = () => {
  const toggleRecording = async () => {
    if (!recorder) {
      // Reset the audio tag
      AUDIO.removeAttribute('src')
      const CHUNKS = []
      const MEDIA_STREAM = await window.navigator.mediaDevices.getUserMedia({
        audio: true
      })
      recorder = new MediaRecorder(MEDIA_STREAM)
      recorder.ondataavailable = event => {
        // Update the UI
        TOGGLE.innerText = '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
      }
      TOGGLE.innerText = 'Stop Recording'
      recorder.start()
      ANALYSE(recorder.stream)
    } else {
      recorder.stop()
      gsap.to(SQUARE, {
        duration: CONFIG.duration,
        scale: 1,
        hue: 100,
        onComplete: () => {
          audioContext.close() 
          gsap.ticker.remove(report)
        }
      })
    }
  }
  toggleRecording()
}

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

Console