In my React app, I'm utilizing the npm package react-webcam.
The <Webcam />
component is enclosed within a div with a dimension of {width: 100vw, height 100vh}
I aim for the video element to adjust to the parent div's size and cover 100% of the screen always, even during browser resizing. However, there appears to be a certain ratio limitation.
https://i.sstatic.net/AcYhA.jpg
^ The black-colored space in the middle should be fully occupied by the video element.
Attempted Solutions:
- Set
<Webcam />
's height and width to 100% - Assign 100vh and 100vw as height and width for
<Webcam />
- Specified auto for
videoConstraint
's height and width (had no effect) - Assigned 100% for
videoConstraint
's height and width (resulted in an error) - Removed height and width specifications altogether from
videoConstraint
I have a theory that videoConstraint
may have default settings that need to be overridden, but I am unsure how to do so.
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/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="59292b3629742d20293c2a19686c776f7768">[email protected]</a>/prop-types.js"></script>
<script src="https://unpkg.com/react-webcam/dist/react-webcam.min.js"></script>
<div id="root"></div>
Check out the official page for react-webcam on npm: