Skip to content

Instantly share code, notes, and snippets.

@michaelbromley
Created July 3, 2020 09:22
Show Gist options
  • Save michaelbromley/73a15611891ea807de95232f33b1438a to your computer and use it in GitHub Desktop.
Save michaelbromley/73a15611891ea807de95232f33b1438a to your computer and use it in GitHub Desktop.
Floating webcam script
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Display Webcam Stream</title>
<style>
</style>
</head>
<body>
<div id="container">
<video autoplay="true" id="videoElement"></video>
<button id="pipButtonElement">Float video frame</button>
</div>
<script>
var video = document.querySelector("#videoElement");
var pipButtonElement = document.querySelector("#pipButtonElement");
getMedia({ audio: false, video: true });
pipButtonElement.addEventListener('click', async function () {
pipButtonElement.disabled = true;
await videoElement.requestPictureInPicture();
pipButtonElement.disabled = false;
});
async function getMedia(constraints) {
let stream = null;
try {
stream = await navigator.mediaDevices.getUserMedia(constraints);
video.srcObject = stream;
video.onloadedmetadata = function (e) {
video.play();
};
} catch (err) {
console.error(err);
}
}
</script>
</body>
</html>
@michaelbromley
Copy link
Author

Open this file in a browser in latest Chrome, and you can display your webcam in a floating frame on your desktop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment