These three libraries enable developers to access user webcams and capture photos directly within web applications. react-webcam is a React component that provides direct access to the video stream and screenshot capabilities. react-html5-camera-photo is a React wrapper designed specifically for taking photos with built-in callbacks. jslib-html5-camera-photo is the underlying vanilla JavaScript library that powers the React wrapper, suitable for non-React projects or lower-level integration.
Accessing a user's camera in a web application requires handling permissions, video streams, and canvas rendering. The packages react-webcam, react-html5-camera-photo, and jslib-html5-camera-photo all solve this problem but approach it from different angles. Let's compare how they handle core tasks like initialization, capturing images, and configuration.
react-webcam is a pure React component.
<video> element internally.// react-webcam: Component usage
import Webcam from "react-webcam";
function MyComponent() {
const webcamRef = useRef(null);
return <Webcam ref={webcamRef} />;
}
react-html5-camera-photo is also a React component but wraps specific capture logic.
// react-html5-camera-photo: Component usage
import Camera from "react-html5-camera-photo";
function MyComponent() {
const handlePhoto = (dataUri) => console.log(dataUri);
return <Camera onTakePhoto={handlePhoto} />;
}
jslib-html5-camera-photo is a vanilla JavaScript library.
<video> tag).// jslib-html5-camera-photo: Vanilla JS usage
import { CameraPhoto } from "jslib-html5-camera-photo";
const cameraPhoto = new CameraPhoto("#video-element");
cameraPhoto.startCamera();
The way you retrieve the image data differs significantly between these tools.
react-webcam requires you to call a method on the ref.
// react-webcam: Manual capture
const takeShot = () => {
const imageSrc = webcamRef.current.getScreenshot();
console.log(imageSrc);
};
<button onClick={takeShot}>Capture</button>
react-html5-camera-photo uses a prop callback.
// react-html5-camera-photo: Callback capture
const handlePhoto = (dataUri) => {
console.log(dataUri);
};
<Camera onTakePhoto={handlePhoto} />;
jslib-html5-camera-photo uses a class method.
takePhoto() on the instance.// jslib-html5-camera-photo: Method capture
const photoData = cameraPhoto.takePhoto();
console.log(photoData);
Configuring resolution, aspect ratio, and image format is critical for performance and quality.
react-webcam passes constraints directly to the video element.
videoConstraints prop.// react-webcam: Video constraints
<Webcam
videoConstraints={{
width: 1280,
height: 720,
facingMode: "user"
}}
/>
react-html5-camera-photo uses specific props for image output.
imageType to set format (jpg/png).resultType to define output (base64/uint8array).// react-html5-camera-photo: Image settings
<Camera
imageType="jpg"
resultType="base64"
idealResolution={{ width: 1280, height: 720 }}
/>
jslib-html5-camera-photo configures via constructor or methods.
// jslib-html5-camera-photo: Initialization options
const options = {
width: 1280,
height: 720,
imageType: "jpg"
};
const cameraPhoto = new CameraPhoto("#video", options);
Long-term project health depends on library maintenance and community support.
react-webcam has consistent updates and wide adoption.
// react-webcam: Standard import
import Webcam from "react-webcam";
// Widely supported in current React versions
react-html5-camera-photo has had periods of low activity.
react-webcam.// react-html5-camera-photo: Potential compatibility check
import Camera from "react-html5-camera-photo";
// Verify React version compatibility before install
jslib-html5-camera-photo is stable but niche.
html5-camera-photo project.// jslib-html5-camera-photo: Vanilla stability
import { CameraPhoto } from "jslib-html5-camera-photo";
// Stable API for vanilla JS contexts
| Feature | react-webcam | react-html5-camera-photo | jslib-html5-camera-photo |
|---|---|---|---|
| Framework | ⚛️ React | ⚛️ React | 🌐 Vanilla JS |
| Capture Method | 🖱️ Manual (ref method) | 🔄 Callback (prop) | 🖱️ Manual (class method) |
| Configuration | 🎛️ videoConstraints | 🎛️ imageType/resultType | 🎛️ Constructor options |
| Maintenance | ✅ Active | ⚠️ Less Active | ✅ Stable |
| Best Use | 🏢 General React Apps | 📸 Photo-First Components | 🛠️ Non-React Projects |
react-webcam is the standard choice for React developers.
It balances flexibility with stability and fits naturally into the React component model. Use it for dashboards, profile uploads, or any feature where webcam access is part of a larger interface.
react-html5-camera-photo works well for simple photo booths.
If your sole goal is to take a picture and you want the data handed to you automatically, this reduces boilerplate. Just ensure you test it thoroughly with your specific React version.
jslib-html5-camera-photo is for vanilla JavaScript needs.
Reach for this when you are not using React or need to integrate camera logic into a legacy codebase without framework dependencies. It provides the foundation that the React wrappers are built upon.
Choose react-webcam for most React projects requiring stable, long-term support and active maintenance. It offers a straightforward API to access the video stream and capture screenshots on demand. This package is ideal when you need flexibility to customize the video constraints and handle the capture action separately from the component rendering.
Choose jslib-html5-camera-photo if you are building a vanilla JavaScript application or using a framework other than React. It provides direct access to the camera hardware without React-specific dependencies. This option gives you full control over the implementation but requires more boilerplate code to manage the video element and canvas rendering manually.
Choose react-html5-camera-photo if you need a React component that handles the photo capture logic internally via callbacks. It simplifies the process by exposing an onTakePhoto prop instead of requiring manual screenshot triggers. However, verify compatibility with your React version, as maintenance has been less consistent compared to other options.
DEMO: https://codepen.io/mozmorris/pen/JLZdoP
https://www.npmjs.com/package/react-webcam
Webcam component for React. See http://caniuse.com/#feat=stream for browser compatibility.
Note: Browsers will throw an error if the page is loaded from insecure origin. I.e. Use https.
# with npm
npm install react-webcam
# with yarn
yarn add react-webcam
https://codepen.io/mozmorris/pen/JLZdoP
import React from "react";
import Webcam from "react-webcam";
const WebcamComponent = () => <Webcam />;
The props here are specific to this component but one can pass any prop to the underlying video tag eg className, style, muted, etc
| prop | type | default | notes |
|---|---|---|---|
| audio | boolean | false | enable/disable audio |
| audioConstraints | object | MediaStreamConstraint(s) for the audio | |
| disablePictureInPicture | boolean | false | disable Picture-in-Picture |
| forceScreenshotSourceSize | boolean | false | uses size of underlying source video stream (and thus ignores other size related props) |
| imageSmoothing | boolean | true | pixel smoothing of the screenshot taken |
| mirrored | boolean | false | show camera preview and get the screenshot mirrored |
| minScreenshotHeight | number | min height of screenshot | |
| minScreenshotWidth | number | min width of screenshot | |
| onUserMedia | function | noop | callback for when component receives a media stream |
| onUserMediaError | function | noop | callback for when component can't receive a media stream with MediaStreamError param |
| screenshotFormat | string | 'image/webp' | format of screenshot |
| screenshotQuality | number | 0.92 | quality of screenshot(0 to 1) |
| videoConstraints | object | MediaStreamConstraints(s) for the video |
getScreenshot - Returns a base64 encoded string of the current webcam image. Example:
https://codepen.io/mozmorris/pen/gOOoqpw
You may also pass in an optional dimensions object:
getScreenshot({width: 1920, height: 1080});
We can build a constraints object by passing it to the videoConstraints prop. This gets passed into getUserMedia method. Please take a look at the MDN docs to get an understanding how this works.
https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia https://developer.mozilla.org/en-US/docs/Web/API/Media_Streams_API/Constraints
As an example take a look at this CodePen demo https://codepen.io/mozmorris/pen/GRpEQwK?editors=0010 which shows how to build a custom aspect ratio for the video.
const videoConstraints = {
width: 1280,
height: 720,
facingMode: "user"
};
const WebcamCapture = () => (
<Webcam
audio={false}
height={720}
screenshotFormat="image/jpeg"
width={1280}
videoConstraints={videoConstraints}
>
{({ getScreenshot }) => (
<button
onClick={() => {
const imageSrc = getScreenshot()
}}
>
Capture photo
</button>
)}
</Webcam>
);
const videoConstraints = {
width: 1280,
height: 720,
facingMode: "user"
};
const WebcamCapture = () => {
const webcamRef = React.useRef(null);
const capture = React.useCallback(
() => {
const imageSrc = webcamRef.current.getScreenshot();
},
[webcamRef]
);
return (
<>
<Webcam
audio={false}
height={720}
ref={webcamRef}
screenshotFormat="image/jpeg"
width={1280}
videoConstraints={videoConstraints}
/>
<button onClick={capture}>Capture photo</button>
</>
);
};
It is posible to capture video with <Webcam /> using the MediaStream Recording API.
You can find an example https://codepen.io/mozmorris/pen/yLYKzyp?editors=0010.
class WebcamCapture extends React.Component {
render() {
const videoConstraints = {
facingMode: "user"
};
return <Webcam videoConstraints={videoConstraints} />;
}
}
class WebcamCapture extends React.Component {
render() {
const videoConstraints = {
facingMode: { exact: "environment" }
};
return <Webcam videoConstraints={videoConstraints} />;
}
}
For more information on facingMode, please see the MDN web docs https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints/facingMode
const WebcamCapture = () => {
const [deviceId, setDeviceId] = React.useState({});
const [devices, setDevices] = React.useState([]);
const handleDevices = React.useCallback(
mediaDevices =>
setDevices(mediaDevices.filter(({ kind }) => kind === "videoinput")),
[setDevices]
);
React.useEffect(
() => {
navigator.mediaDevices.enumerateDevices().then(handleDevices);
},
[handleDevices]
);
return (
<>
{devices.map((device, key) => (
<div>
<Webcam audio={false} videoConstraints={{ deviceId: device.deviceId }} />
{device.label || `Device ${key + 1}`}
</div>
))}
</>
);
};
https://codepen.io/mozmorris/pen/yLYKzyp?editors=0011
The Webcam component will fail to load when used inside a cross-origin iframe in newer version of Chrome (> 64). In order to overcome this security restriction a special allow attribute needs to be added to the iframe tag specifying microphone and camera as the required permissions like in the below example:
<iframe src="https://my-website.com/page-with-webcam" allow="camera; microphone;"/>
Add mirrored prop to the component will make the video and the screenshot be mirrored, but sometimes you need to show a mirrored video but take a non-mirrored screenshot, to accomplish that, you just need to add this CSS to your project:
video {
transform: scaleX(-1);
}
You can find an example at https://codepen.io/mozmorris/pen/oNygXwz?editors=0110.
MIT