I'm using the npm package react-webcam in my react app,
the <Webcam />
component is wrapped in a div that has {width: 100vw, height 100vh}
I want the video element to take after the parent div's dimensions and take up 100% of the screen view at all times, including when browser is resized, but there seems to be a certain ratio restriction.
^ the video element in the middle should take up the whole space that's coloured black.
Things I've tried:
- set
<Webcam />
's height and width to 100% - set
<Webcam />
's height and width to 100vh and 100vw - set
videoConstraint
's height and width to auto (this does nothing) - set
videoConstraint
's height and width to 100% (this just gives me error) - remove
videoConstraint
's height and width specification altogether
I've come to a speculation that videoConstraint
must have some default specifications but i'm not sure how to overwrite it.
Code:
const videoConstraints = {
width: 1280,
height: 720,
facingMode: "user"
}
ReactDOM.render(
<div className = 'webcam' >
<Webcam
audio = {false}
height = {100 + '%'}
width = {100 + '%'}
screenshotFormat = 'image/jpeg'
videoConstraints = {videoConstraints}
/>
</div>,
document.getElementById('root')
);
.webcam {
padding: 2.5rem 3.5rem;
}
video {
background-color: #000000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/prop-types@15.6.1/prop-types.js"></script>
<script src="https://unpkg.com/react-webcam/dist/react-webcam.min.js"></script>
<div id="root"></div>
official react-webcam npm page