npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-use-camera

v1.1.7

Published

Lightweight (1.9kb) camera component with photo capture functionality

Downloads

72

Readme

React Use Camera

The lightweight library (1.9kb minified + gzipped) to add camera & photo capture functionality in your React app.

Note: WebRTC is only supported over https so you will need an SSL certificate in production. For development, chrome supports WebRTC even without SSL / https as long as it's running on "localhost".

Install

npm

npm i react-use-camera

yarn

yarn add react-use-camera

Instructions

There are 2 ways to use this library:

  1. The <Camera /> component (recommended)
  2. The useCamera() hook.

The Camera Component

This gives you a raw camera interface without any opinionated UI elements or buttons. Draw your own UI over the camera stream.

import { useRef } from "react";
import { Camera, CameraElement } from "react-use-camera";

export default function App() {
  const cameraRef = useRef<CameraElement>(null);

  const handleCapture = async () => {
    const imageData = await cameraRef.current?.capture(); // Camera view will pause after capture
    // imageData.url is a base64 string that can also be used as src for an <img/> tag
    // imageData.blob is a blob string to send to your server

    // NOTES:
    // (i) Use `cameraRef.current?.capture({ mirror: true });` to flip the captured image (will be enabled by default on front camera)
    // (ii) Use `cameraRef.current?.capture({ width: 512 });` to capture image in 512px width (height will be auto calculated)
    // (iii) Use `cameraRef.current?.capture({ height: 512 });` to capture image in 512px height (width will be auto calculated)
    // (iv) If width or height is not specified, your captured image will be of the same size as the camera resolution
  };

  const handleClear = () => {
    cameraRef.current?.clear(); // Discards the captured photo and resumes the camera view
  };

  return (
    <div>
      <Camera
        ref={cameraRef}
        className="your-classes-here"
        style={/* width, height, etc */}
        errorLayout={<div>Oops!</div>}
        onReady={() => console.log("Camera is now visibile to the user")}
        onError={(e) => console.error("Camera couldn't load :(")}
      />

      {/* Add your own UI here... */}
      <button onClick={handleCapture}>Capture</button>
      <button onClick={handleClear}>Clear</button>
    </div>
  );
}

Props

  • fit

    • Type: fill | contain | cover | blur
    • Default: contain
    • Notes:
      • fill will stretch or squish the video to match the exact width or height that you give to camera component
      • contain will maintain its aspect ratio while fitting within the camera component's width and height. There might be empty spaces around the camera stream.
      • cover will keep the aspect ratio and crop the camera stream to fit the width and height that you give to the camera component.
      • blur will work similar to contain BUT instead of empty spaces around the camera, it will show a blurred version of the camera stream as the background.
  • constraints

    • Type: MediaTrackConstraints
    • Default: { facingMode: "user", width: { ideal: 1440 }, height: { ideal: 1080 }}
    • Notes:
      • If you want to select the front or back camera, you will need to pass facingMode as user or environment respectively. Default is set to user i.e. the front camera.
  • errorLayout

    • Type: ReactNode | JSX
    • Default: undefined
    • Notes: This layout will be shown instead of the camera stream in case of an error. For example:
      • Browser doesn't support the camera API
      • No camera found on the device
      • User denied the camera permission
  • onError

    • Type: (error: unknown) => void
  • onReady

    • Type: () => void

The useCamera() Hook

This give you no UI. You just get a MediaStream instance that you have to attach to a <video /> tag. To capture an image, call the hook's capture function with the MediaStream instance or a ref to the <video /> tag as a parameter.

import { useEffect, useState, useRef } from "react";
import { useCamera } from "react-use-camera";

export const MyCustomCameraComponent = () => {
  const videoRef = useRef<HTMLVideoElement>(null);

  const { startCamera, stopCamera, capture } = useCamera();
  const [stream, setStream] = useState<MediaStream>();

  const handleStartCamera = async () => {
    try {
      const stream = await startCamera({ /* MediaTrackConstraints */ })
      setStream(stream);
      videoRef.current!.srcObject = stream;
    } catch (e) {
      alert("Oops! Camera failed to start!");
      console.error(e);
    }
  }

  const handleStopCamera = () => {
    stopCamera(stream);
  }

  const handleCapture = async () => {
    if (!stream) return; // Don't capture if the stream isn't active!
    try {
      const capturedImage = await capture({ videoRef }, {
        mirror: false, // Pass true if you want to mirror the captured image (recommended for front camera)
      });

      // NOTES:
      // (i) To capture using MediaStream instead of a videoRef, use can use `await capture({ stream })`
      // (ii) To get the captured image in a custom width, use `await capture({...}, { width: YOUR_REQUIRED_WIDTH })` (height will be auto calculated)
      // (iii) To get the captured image in a custom height, use `await capture({...}, { height: YOUR_REQUIRED_HEIGHT })` (width will be auto calculated)
      // (iv) If width or height is not specified, your captured image will be of the same size as the camera resolution

      if (capturedImage) {
        console.log("URL:" + capturedImage.url);
        console.log("Blob: " + capturedImage.blob);
      }
    } catch {
      alert("Oops! Unable to capture image. Check if the camera stream is active.");
    }
  }

  return (
    <div>
      <video
        ref={videoRef}
        autoPlay
        playsInline
      />
      <button onClick={handleStartCamera}>Start Camera</button>
      <button onClick={handleStopCamera}>Stop Camera</button>
      <button onClick={handleCapture}>Capture</button>
    </div>
  );
}

License

MIT