const video = document.getElementById('videoElement');
console.log(video)
// Check if the browser supports media devices
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
// Access the webcam
navigator.mediaDevices.getUserMedia({ video: true })
.then((stream) => {
// Set the video source to the webcam stream
video.srcObject = stream;
// When the video metadata is loaded
video.onloadedmetadata = () => {
// Calculate the aspect ratio of the video
const aspectRatio = video.videoWidth / video.videoHeight;
// Rotate the video element to portrait mode
video.style.transform = 'rotate(90deg)';
video.style.transformOrigin = 'center center';
// Set the video dimensions to fill the screen
if (window.innerWidth / window.innerHeight > aspectRatio) {
video.style.width = '100%';
video.style.height = 'auto';
} else {
video.style.width = 'auto';
video.style.height = '100%';
}
};
// Play the video
video.play();
})
.catch((error) => {
console.error('Error accessing the webcam:', error);
});
} else {
console.error('getUserMedia is not supported by this browser');
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.