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

              
                <p class="countdown" id="countdown"></p>
<div class="screen-recorder">
  <h1>Capture your screen</h1>
  <video controls preload="metadata" id="video"></video>
  <div>
    <button type="button" class="btn" id="btn">Start Recording</button>
    <a href="" class="btn" id="link">download video</a>
  </div>
</div>
              
            
!

CSS

              
                * {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

body {
  width: 100vw;
  min-height: 100vh;
  position: relative;
  display: grid;
  place-items: center;
}

.countdown {
  width: 100%;
  height: 100%;
  display: none;
  place-items: center;
  color: green;
  font-size: 10rem;
  font-weight: 900;
  background-color: rgba(0, 0, 0, 0.5);
  position: absolute;
  inset: 0 0 0 0;
  z-index: 10000;
}

.screen-recorder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2rem;
  width: 100%;
  height: 100%;
  background-color: #f7fafc;
  color: #202020;

  h1 {
    text-transform: capitalize;
    font-size: 4rem;
    font-weight: bold;
    color: #202020;
  }

  video {
    width: 782px;
    max-height: 440px;
    box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.2),
      -4px -4px 10px rgba(0, 0, 0, 0.2);
  }
}

.btn {
  background-color: #428bca;
  color: #fff;
  font-weight: bold;
  padding: 0.75rem 1.5rem;
  border-radius: 0.5rem;
  box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.08);
  transition: background-color 0.3s ease;
  border: 1px solid #428bca;
  cursor: pointer;
  text-transform: capitalize;
  font-weight: 900;

  &:hover {
    background-color: #357ec7;
  }
}

a {
  text-decoration: none;
  margin-inline-start: 20px;
}

              
            
!

JS

              
                const startBtn = document.getElementById("btn");
const downloadLink = document.getElementById("link");
let blob = null;
let videoStream = null;

startBtn.addEventListener("click", startScreenCapturing);

async function startScreenCapturing() {
  if (!navigator.mediaDevices.getDisplayMedia) {
    return alert("Screen capturing not supported in your browser.");
  }

  try {
    if (!videoStream?.active) {
      videoStream = await navigator.mediaDevices.getDisplayMedia({
        audio: true,
        surfaceSwitching: "include"
      });

      const audioStream = await navigator.mediaDevices.getUserMedia({
        audio: {
          echoCancellation: true,
          noiseSuppression: true,
          sampleRate: 44100,
          suppressLocalAudioPlayback: false
        }
      });

      const audioTrack = audioStream.getTracks()[0];
      videoStream.addTrack(audioTrack);

      recordStream(videoStream);
    } else {
      throw new Error(
        "There is an ongoing recording. Please, stop it before recording a new one"
      );
    }
  } catch (error) {
    console.error(error);
    alert(error);
  }
}

function recordStream(stream) {
  countdown();
  const mediaRecorder = new MediaRecorder(stream, {
    mimeType: "video/webm; codecs=vp8,opus"
  });

  const recordedChunks = [];
  mediaRecorder.addEventListener("dataavailable", (e) =>
    recordedChunks.push(e.data)
  );

  // Stop recording and audio streaming when video streaming stops
  stream.getVideoTracks()[0].addEventListener("ended", () => {
    mediaRecorder.stop();
    stream.getAudioTracks()[0].stop();
  });

  mediaRecorder.addEventListener("stop", () => {
    createVideoBlob(recordedChunks);
    showRecordedVideo(blob);
  });

  setTimeout(() => mediaRecorder.start(1000), 4000);
}

function countdown() {
  const countDownElement = document.getElementById("countdown");
  countDownElement.style.display = "grid";
  let count = 3;

  function reduceCount() {
    countDownElement.textContent = count;
    count--;

    if (count >= 0) {
      setTimeout(reduceCount, 1000);
    } else {
      countDownElement.style.display = "none";
    }
  }

  reduceCount();
}

function createVideoBlob(recordedChunks) {
  blob = new Blob(recordedChunks, {
    type: recordedChunks[0].type
  });
}

function showRecordedVideo() {
  const video = document.getElementById("video");
  video.src = URL.createObjectURL(blob);
  calculateVideoDuration(video);
}

// Recalculates video duration
function calculateVideoDuration(videoElement) {
  videoElement.addEventListener("loadedmetadata", () => {
    if (videoElement.duration === Infinity) {
      videoElement.currentTime = 1e101;
      videoElement.addEventListener(
        "timeupdate",
        () => {
          videoElement.currentTime = 0;
        },
        { once: true }
      );
    }
  });
}

downloadLink.addEventListener("click", () => {
  downloadLink.href = URL.createObjectURL(blob);
  const fileName = prompt("What is the name of your video?");
  downloadLink.download = `${fileName}.webm`;
  downloadLink.type = "video/webm";
});

              
            
!
999px

Console