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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <select id="video-devices">
  <option>Video (Default)</option>
</select>
<select id="audio-devices">
  <option>Audio (Default)</option>
</select>

<button id="start">Start</button>
<button id="shot">Take a Screenshot</button>

<div class="results">
<div id="stream"></div>
<div id="screenshot"></div>
</div>

              
            
!

CSS

              
                body {
}

.results {
  display: flex;
}
.results div:first-child {
  flex-basis: 50%;
}
.results div:last-child {
  flex-basis: 50%;
}
.results video,
.results canvas{
  width: 100%;
  border: 1px solid #000;
}

              
            
!

JS

              
                const init = async () => {
  const startButton = document.querySelector("button#start");
  const shotButton = document.querySelector("button#shot");
  const audioDeviceSelect = document.querySelector<HTMLSelectElement>("select#audio-devices");
  const videoDeviceSelect = document.querySelector<HTMLSelectElement>("select#video-devices");
  const videoContainer = document.querySelector<HTMLDivElement>("div#stream");
  const canvasContainer = document.querySelector<HTMLDivElement>("div#screenshot");

  // 先に許可を貰わないとenumerateDevicesが返すデバイス名(label)を取得できない
  await navigator.mediaDevices.getUserMedia({video: true, audio: true});
  const devices = await navigator.mediaDevices.enumerateDevices();
  devices.forEach((device) => {
    console.log(device.kind + ": " + device.label + " id = " + device.deviceId);
    const option = document.createElement("option");
    option.text = device.label;
    option.value = device.deviceId;

    if (device.kind === "audioinput") {
      audioDeviceSelect.appendChild(option);
    } else if (device.kind === "videoinput") {
      videoDeviceSelect.appendChild(option);
    }
  });

  const getSelectedVideo = () => {
    return videoDeviceSelect.value;
  }

  const getSelectedAudio = () => {
    return audioDeviceSelect.value;
  }
  
  let captureStream: MediaStream;
  startButton.addEventListener("click", async () => {
    videoContainer.innerHTML = '';

    captureStream = await navigator.mediaDevices.getUserMedia({video: { deviceId: getSelectedVideo(), width: 1920, height: 1080 }, audio: {deviceId: getSelectedAudio()}});
    const video = document.createElement("video");
    video.autoplay = true;
    video.srcObject = captureStream;
    videoContainer.appendChild(video);
  });

  shotButton.addEventListener('click', async () => {
    const videoTrack = captureStream.getVideoTracks()[0];
    const imageCapture = new ImageCapture(videoTrack);
    const imageBitmap = await imageCapture.grabFrame();
    console.log('Grabbed frame:', imageBitmap);
    
    canvasContainer.innerHTML = '';
    const canvas = document.createElement("canvas");
    canvas.width = imageBitmap.width;
    canvas.height = imageBitmap.height;
    canvas.getContext('2d').drawImage(imageBitmap, 0, 0);
    canvasContainer.appendChild(canvas);
  });
};

init();

              
            
!
999px

Console