<button class="btn btn-primary" id="button">Display Webcam feed in Picture-in-Picture mode</button>
body {
padding: 20px;
}
const button = document.getElementById('button');
button.addEventListener('click', async () => {
const video= document.createElement('video');
video.muted = true;
video.srcObject = await navigator.mediaDevices.getUserMedia({ video: true });
video.play();
video.addEventListener('loadedmetadata', () => {
video.requestPictureInPicture()
.catch(console.error)
});
});
This Pen doesn't use any external JavaScript resources.