{"id":13469722,"url":"https://github.com/mozmorris/react-webcam","last_synced_at":"2025-05-14T02:08:31.950Z","repository":{"id":24550802,"uuid":"27957693","full_name":"mozmorris/react-webcam","owner":"mozmorris","description":"Webcam component","archived":false,"fork":false,"pushed_at":"2024-05-30T15:51:09.000Z","size":2150,"stargazers_count":1712,"open_issues_count":67,"forks_count":287,"subscribers_count":24,"default_branch":"master","last_synced_at":"2025-05-04T07:02:02.107Z","etag":null,"topics":["getusermedia","react","reactjs","webcam","webcam-component"],"latest_commit_sha":null,"homepage":"https://codepen.io/mozmorris/pen/JLZdoP","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mozmorris.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-12-13T10:50:11.000Z","updated_at":"2025-04-30T16:49:43.000Z","dependencies_parsed_at":"2023-01-14T07:46:07.328Z","dependency_job_id":"1ed56407-1f1b-4f68-8e1a-35d8cd36902b","html_url":"https://github.com/mozmorris/react-webcam","commit_stats":{"total_commits":274,"total_committers":53,"mean_commits":5.169811320754717,"dds":0.5474452554744526,"last_synced_commit":"97745ee6488015762d20f4ce05f5dcaa1c48e38d"},"previous_names":[],"tags_count":50,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mozmorris%2Freact-webcam","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mozmorris%2Freact-webcam/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mozmorris%2Freact-webcam/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mozmorris%2Freact-webcam/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mozmorris","download_url":"https://codeload.github.com/mozmorris/react-webcam/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254053176,"owners_count":22006717,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["getusermedia","react","reactjs","webcam","webcam-component"],"created_at":"2024-07-31T15:01:52.877Z","updated_at":"2025-05-14T02:08:26.935Z","avatar_url":"https://github.com/mozmorris.png","language":"TypeScript","funding_links":[],"categories":["TypeScript","🧰 React Toolkit"],"sub_categories":["Audio \u0026 Video"],"readme":"# react-webcam\n\n[![Build Status][build-badge]][build]\n![downloads][downloads-badge]\n\n\u003cimg src=\"Logotype primary.png\" width=\"70%\" height=\"70%\" /\u003e\n\n**DEMO:** https://codepen.io/mozmorris/pen/JLZdoP\n\nhttps://www.npmjs.com/package/react-webcam\n\nWebcam component for React. See [http://caniuse.com/#feat=stream](http://caniuse.com/#feat=stream)\nfor browser compatibility.\n\n**Note: Browsers will throw an error if the page is loaded from insecure origin. I.e. Use https.**\n\n## Installation\n\n```shell\n# with npm\nnpm install react-webcam\n\n# with yarn\nyarn add react-webcam\n```\n\n## Demo\n\nhttps://codepen.io/mozmorris/pen/JLZdoP\n\n## Usage\n\n```jsx\nimport React from \"react\";\nimport Webcam from \"react-webcam\";\n\nconst WebcamComponent = () =\u003e \u003cWebcam /\u003e;\n```\n\n### Props\n\nThe props here are specific to this component but one can pass any prop to the underlying video tag eg `className`, `style`, `muted`, etc\n\n| prop                      | type     | default      | notes                                                                                   |\n|---------------------------|----------|--------------|-----------------------------------------------------------------------------------------|\n| audio                     | boolean  | false        | enable/disable audio                                                                    |\n| audioConstraints          | object   |              | MediaStreamConstraint(s) for the audio                                                  |\n| disablePictureInPicture   | boolean  | false        | disable Picture-in-Picture                                                              |\n| forceScreenshotSourceSize | boolean  | false        | uses size of underlying source video stream (and thus ignores other size related props) |\n| imageSmoothing            | boolean  | true         | pixel smoothing of the screenshot taken                                                 |\n| mirrored                  | boolean  | false        | show camera preview and get the screenshot mirrored                                     |\n| minScreenshotHeight       | number   |              | min height of screenshot                                                                |\n| minScreenshotWidth        | number   |              | min width of screenshot                                                                 |\n| onUserMedia               | function | noop         | callback for when component receives a media stream                                     |\n| onUserMediaError          | function | noop         | callback for when component can't receive a media stream with MediaStreamError param    |\n| screenshotFormat          | string   | 'image/webp' | format of screenshot                                                                    |\n| screenshotQuality         | number   | 0.92         | quality of screenshot(0 to 1)                                                           |\n| videoConstraints          | object   |              | MediaStreamConstraints(s) for the video                                                 |\n\n### Methods\n\n`getScreenshot` - Returns a base64 encoded string of the current webcam image. Example:\n\n[https://codepen.io/mozmorris/pen/gOOoqpw](https://codepen.io/mozmorris/pen/gOOoqpw)\n\nYou may also pass in an optional `dimensions` object:\n\n```\ngetScreenshot({width: 1920, height: 1080});\n```\n\n### The Constraints\n\nWe 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.\n\nhttps://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia\nhttps://developer.mozilla.org/en-US/docs/Web/API/Media_Streams_API/Constraints\n\nAs an example take a look at this [CodePen demo https://codepen.io/mozmorris/pen/GRpEQwK?editors=0010](https://codepen.io/mozmorris/pen/GRpEQwK?editors=0010) which shows how to build a custom aspect ratio for the video.\n\n## Screenshot (via render props)\n\n```jsx\nconst videoConstraints = {\n  width: 1280,\n  height: 720,\n  facingMode: \"user\"\n};\n\nconst WebcamCapture = () =\u003e (\n  \u003cWebcam\n    audio={false}\n    height={720}\n    screenshotFormat=\"image/jpeg\"\n    width={1280}\n    videoConstraints={videoConstraints}\n  \u003e\n    {({ getScreenshot }) =\u003e (\n      \u003cbutton\n        onClick={() =\u003e {\n          const imageSrc = getScreenshot()\n        }}\n      \u003e\n        Capture photo\n      \u003c/button\u003e\n    )}\n  \u003c/Webcam\u003e\n);\n```\n\n## Screenshot (via ref)\n\n```jsx\nconst videoConstraints = {\n  width: 1280,\n  height: 720,\n  facingMode: \"user\"\n};\n\nconst WebcamCapture = () =\u003e {\n  const webcamRef = React.useRef(null);\n  const capture = React.useCallback(\n    () =\u003e {\n      const imageSrc = webcamRef.current.getScreenshot();\n    },\n    [webcamRef]\n  );\n  return (\n    \u003c\u003e\n      \u003cWebcam\n        audio={false}\n        height={720}\n        ref={webcamRef}\n        screenshotFormat=\"image/jpeg\"\n        width={1280}\n        videoConstraints={videoConstraints}\n      /\u003e\n      \u003cbutton onClick={capture}\u003eCapture photo\u003c/button\u003e\n    \u003c/\u003e\n  );\n};\n```\n\n## Capturing video\n\nIt is possible to capture video with `\u003cWebcam /\u003e` using the [MediaStream Recording API](https://developer.mozilla.org/en-US/docs/Web/API/MediaStream_Recording_API).\n\nYou can find an example [https://codepen.io/mozmorris/pen/yLYKzyp?editors=0010](https://codepen.io/mozmorris/pen/yLYKzyp?editors=0010).\n\n## Choosing a camera\n\n### User/Selfie/forward facing camera\n\n```jsx\nclass WebcamCapture extends React.Component {\n  render() {\n    const videoConstraints = {\n      facingMode: \"user\"\n    };\n\n    return \u003cWebcam videoConstraints={videoConstraints} /\u003e;\n  }\n}\n```\n\n### Environment/Facing-Out camera\n\n```jsx\nclass WebcamCapture extends React.Component {\n  render() {\n    const videoConstraints = {\n      facingMode: { exact: \"environment\" }\n    };\n\n    return \u003cWebcam videoConstraints={videoConstraints} /\u003e;\n  }\n}\n```\n\nFor more information on `facingMode`, please see the MDN web docs [https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints/facingMode](https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints/facingMode)\n\n## Show all cameras by deviceId\n\n```jsx\nconst WebcamCapture = () =\u003e {\n  const [deviceId, setDeviceId] = React.useState({});\n  const [devices, setDevices] = React.useState([]);\n\n  const handleDevices = React.useCallback(\n    mediaDevices =\u003e\n      setDevices(mediaDevices.filter(({ kind }) =\u003e kind === \"videoinput\")),\n    [setDevices]\n  );\n\n  React.useEffect(\n    () =\u003e {\n      navigator.mediaDevices.enumerateDevices().then(handleDevices);\n    },\n    [handleDevices]\n  );\n\n  return (\n    \u003c\u003e\n      {devices.map((device, key) =\u003e (\n          \u003cdiv\u003e\n            \u003cWebcam audio={false} videoConstraints={{ deviceId: device.deviceId }} /\u003e\n            {device.label || `Device ${key + 1}`}\n          \u003c/div\u003e\n\n        ))}\n    \u003c/\u003e\n  );\n};\n```\n\n## Recording a stream\n\nhttps://codepen.io/mozmorris/pen/yLYKzyp?editors=0011\n\n## Using within an iframe\n\nThe Webcam component will fail to load when used inside a cross-origin iframe in newer version of Chrome (\u003e 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:\n\n```html\n\u003ciframe src=\"https://my-website.com/page-with-webcam\" allow=\"camera; microphone;\"/\u003e\n```\n\n## Mirrored video but non-mirrored screenshot\n\nAdd `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:\n\n```css\nvideo {\n  transform: scaleX(-1);\n}\n```\nYou can find an example at [https://codepen.io/mozmorris/pen/oNygXwz?editors=0110](https://codepen.io/mozmorris/pen/oNygXwz?editors=0110).\n\n## License\n\nMIT\n\n[build-badge]: https://img.shields.io/travis/com/mozmorris/react-webcam.svg?style=flat-square\n[build]: https://travis-ci.com/mozmorris/react-webcam\n[downloads-badge]: https://img.shields.io/npm/dw/react-webcam.svg?style=flat-square\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmozmorris%2Freact-webcam","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmozmorris%2Freact-webcam","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmozmorris%2Freact-webcam/lists"}