react-webcam-kit

React avatar capture

Build avatar capture with a React webcam.

Capture profile photos, convert them to Files, and upload them with `FormData` using the `Webcam` component or the lower-level `useWebcam()` hook.

Install from npm View GitHub

Upload a captured avatar

import { useRef } from 'react';
import { Webcam, type WebcamHandle } from 'react-webcam-kit';

export function AvatarCapture() {
  const webcamRef = useRef<WebcamHandle>(null);

  async function saveAvatar() {
    const blob = await webcamRef.current?.getScreenshotBlob({
      format: 'image/jpeg',
      quality: 0.9,
      width: 512,
      height: 512,
    });

    if (!blob) return;

    const form = new FormData();
    form.set('avatar', new File([blob], 'avatar.jpg', { type: blob.type }));

    await fetch('/api/avatar', {
      method: 'POST',
      body: form,
    });
  }

  return (
    <>
      <Webcam ref={webcamRef} audio={false} mirrored screenshotFormat="image/jpeg" />
      <button type="button" onClick={() => void saveAvatar()}>Save avatar</button>
    </>
  );
}

Good production defaults

Related guides