@handtrack3d/react

React hooks and components for hand tracking with MediaPipe


Keywords
react, hand-tracking, mediapipe, gestures, react-hooks, gesture-recognition
License
MIT
Install
npm install @handtrack3d/react@0.2.0-alpha.2

Documentation

HandTrack3D

npm version npm version npm version npm version

React Three.js TypeScript

Extensible Plugin Platform for 3D Hand Interaction

A modular SDK and showcase app that enables custom gesture detection, 3D interactions, and physics engine integration through a powerful plugin system.

Quick Start โ€ข SDK Packages โ€ข Plugin System โ€ข Features โ€ข Usage


Overview

HandTrack3D is both a modular SDK and showcase application for building natural user interfaces in 3D environments. The SDK provides an extensible plugin platform for custom gesture detection, 3D interactions, and physics engine integration, while the showcase app demonstrates these capabilities in action.

Key Features

SDK Features

  • ๐Ÿ”Œ Plugin System - Custom gestures, interactions, and physics adapters
  • ๐Ÿ“ฆ Modular Packages - Core, React, Three.js, and Rapier integrations
  • ๐ŸŽฏ Priority-Based Detection - Configure gesture matching order (0-100 scale)
  • โš™๏ธ Physics Abstraction - Engine-agnostic physics with adapters (Rapier, Cannon.js)
  • ๐Ÿ”ง Framework Agnostic - Use with any JavaScript framework or vanilla JS
  • ๐Ÿ“˜ TypeScript First - Full type safety with comprehensive definitions
  • ๐Ÿงช Well Tested - 40+ unit tests, integration tests, 90%+ coverage

Showcase App Features

  • โœ‹ Real-time hand tracking (30fps) with MediaPipe Hands
  • ๐ŸŽฏ 3D cursor mapping from 2D hand landmarks to 3D space
  • ๐Ÿ‘Œ Gesture recognition (pinch, open hand, fist, point)
  • ๐ŸŽฎ Object interaction (grab, drag, release, throw)
  • ๐Ÿ–๏ธ Multi-hand support (up to 2 hands simultaneously)
  • โš™๏ธ Physics simulation (gravity, collisions, realistic motion)
  • ๐ŸŽจ Visual feedback with color-coded cursors and trails
  • ๐Ÿ“Š Real-time stats (FPS, hand count, gestures)

SDK Packages

HandTrack3D is available as a set of npm packages for building your own hand tracking applications:

Installation

# Install all packages
npm install @handtrack3d/core@alpha
npm install @handtrack3d/react@alpha
npm install @handtrack3d/three@alpha
npm install @handtrack3d/rapier@alpha

Available Packages

Package Description Version
@handtrack3d/core Framework-agnostic hand tracking and gesture detection npm
@handtrack3d/react React hooks and components npm
@handtrack3d/three Three.js integration and 3D interactions npm
@handtrack3d/rapier Rapier physics adapter and grab plugin npm

Plugin System

HandTrack3D's plugin architecture allows you to extend functionality without modifying core code.

Creating a Custom Gesture

import { GestureDetector, GesturePlugin } from '@handtrack3d/core';

class ThumbsUpPlugin implements GesturePlugin {
  readonly name = 'custom:thumbs-up';
  readonly priority = 70;
  readonly gestureType = 'thumbs-up';

  detect(landmarks, settings) {
    const thumbUp = landmarks[4].y < landmarks[2].y;
    const fingersCurled = /* check other fingers */;
    return thumbUp && fingersCurled;
  }
}

const detector = new GestureDetector();
detector.registerGesture(new ThumbsUpPlugin());
const gesture = detector.detectGesture(landmarks); // Can detect 'thumbs-up'

Using Physics Abstraction

import { GrabPlugin, RapierAdapter } from '@handtrack3d/rapier';

const adapter = new RapierAdapter();
const grabPlugin = new GrabPlugin(adapter, {
  grabRadius: 0.5,
  throwVelocityScale: 60,
});

// In render loop
grabPlugin.update(hand, rigidBodies);

Plugin Types

  1. GesturePlugin - Custom gesture detection with priority-based matching
  2. InteractionPlugin - 3D interaction behaviors (point-select, custom controls)
  3. PhysicsAdapter - Physics engine abstraction (Rapier, Cannon.js, Ammo.js)

See examples/ for complete tutorials on building custom plugins.


Quick Start

Running the Showcase App

Prerequisites

  • Node.js 18+ or pnpm
  • Modern browser (Chrome/Edge recommended)
  • Webcam

Installation

cd ~/Projects/Active/HandTrack3D
pnpm install
pnpm dev

Navigate to http://localhost:5173 and allow webcam access.


Usage

Basic Interaction

  1. Show your hand to the webcam
  2. Pinch (touch thumb and index finger) near an object to grab it
  3. Move your hand to drag the object in 3D space
  4. Open your hand (spread all fingers) to release
  5. Drop or throw - released objects fall with gravity and can be thrown with velocity

Physics Features

  • Gravity (9.81 m/sยฒ) - Objects fall naturally when released
  • Collisions - Objects bounce off each other and the ground (0.5 restitution)
  • Throwing - Release objects while moving to launch them with velocity
  • Ground plane - Invisible floor prevents objects from falling forever
  • Realistic motion - Damping and friction create natural movement

Keyboard Shortcuts

Key Action
H Toggle status panel
Space Reset camera (planned)

Camera Controls

Input Action
Left click + drag Rotate camera
Right click + drag Pan camera
Scroll wheel Zoom in/out

Gestures

Gesture Detection Visual Feedback
Pinch Thumb-index distance < 0.05 Orange, smaller cursor
Open Hand All fingers extended (>160ยฐ) Larger cursor, dimmer glow
Fist All fingers curled near wrist Red, medium cursor
None Default state Base color (green/blue)

Tech Stack

Core Technologies

  • React 19 - UI framework
  • TypeScript 6 - Type safety
  • Vite 8 - Build tool and dev server
  • Three.js 0.180 - 3D rendering engine
  • React Three Fiber 9.7 - React renderer for Three.js
  • @react-three/drei - Three.js helpers and components
  • @react-three/rapier - Physics engine integration

Computer Vision

  • MediaPipe Hands - Hand tracking (21 3D landmarks per hand)
  • @mediapipe/camera_utils - Webcam integration
  • Loaded via CDN for compatibility

State Management

  • Zustand - Lightweight state management
    • handTrackingStore - Hand positions and tracking data
    • sceneStore - 3D objects and grab state
    • useGestureStore - Gesture detection state
    • useHandCursorStore - 3D cursor positions

Styling

  • Tailwind CSS 3 - Utility-first CSS
  • PostCSS + Autoprefixer - CSS processing

Architecture

Data Flow

Webcam (30fps)
    โ†“
MediaPipe Hands (CDN)
    โ†“
handTrackingStore (21 landmarks ร— 2 hands)
    โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ†“                 โ†“                  โ†“
Gesture Detection  Hand-to-3D Mapping  HandOverlay (2D)
(pinch/open/fist)  (screenโ†’world)      (skeleton viz)
    โ†“                 โ†“
useGestureStore    useHandCursorStore
    โ†“                 โ†“
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
             โ†“
    InteractiveObject
    (collision, grab, drag)
             โ†“
    sceneStore (object positions)
             โ†“
    Scene3D (R3F Canvas, 60fps)

Project Structure

HandTrack3D/
โ”œโ”€โ”€ packages/                      # SDK packages
โ”‚   โ”œโ”€โ”€ core/                      # @handtrack3d/core
โ”‚   โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ plugins/           # Plugin system
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ types.ts       # Plugin interfaces
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ registry.ts    # Plugin registry
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ gestures/          # Gesture detection
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ detector.ts    # GestureDetector class
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ plugins/       # Built-in gesture plugins
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ tracking/          # MediaPipe integration
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ utils/             # Coordinate mapping
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ types/             # TypeScript definitions
โ”‚   โ”‚   โ””โ”€โ”€ package.json
โ”‚   โ”œโ”€โ”€ react/                     # @handtrack3d/react
โ”‚   โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ hooks/             # React hooks
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ components/        # React components
โ”‚   โ”‚   โ””โ”€โ”€ package.json
โ”‚   โ”œโ”€โ”€ three/                     # @handtrack3d/three
โ”‚   โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ interactions/      # 3D interaction plugins
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ utils/             # Three.js utilities
โ”‚   โ”‚   โ””โ”€โ”€ package.json
โ”‚   โ””โ”€โ”€ rapier/                    # @handtrack3d/rapier
โ”‚       โ”œโ”€โ”€ src/
โ”‚       โ”‚   โ”œโ”€โ”€ adapters/          # Physics adapters
โ”‚       โ”‚   โ”œโ”€โ”€ interactions/      # Grab plugin
โ”‚       โ”‚   โ”œโ”€โ”€ hooks/             # React physics hooks
โ”‚       โ”‚   โ””โ”€โ”€ utils/             # Physics utilities
โ”‚       โ””โ”€โ”€ package.json
โ”œโ”€โ”€ src/                           # Showcase app
โ”‚   โ”œโ”€โ”€ components/
โ”‚   โ”‚   โ”œโ”€โ”€ HandTrackingCanvas/    # 3D scene and rendering
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ HandTrackingCanvas.tsx
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Scene3D.tsx
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ HandMesh.tsx       # 3D cursor with trails
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ InteractiveObject.tsx
โ”‚   โ”‚   โ”œโ”€โ”€ WebcamFeed/            # Webcam and overlay
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ WebcamFeed.tsx
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ HandOverlay.tsx    # 2D skeleton visualization
โ”‚   โ”‚   โ””โ”€โ”€ ControlPanel/          # Status UI
โ”‚   โ”œโ”€โ”€ hooks/
โ”‚   โ”‚   โ”œโ”€โ”€ useWebcam.ts           # Camera access
โ”‚   โ”‚   โ”œโ”€โ”€ useHandTracking.ts     # MediaPipe integration
โ”‚   โ”‚   โ”œโ”€โ”€ useHandTo3DMapping.ts  # 2Dโ†’3D coordinate mapping
โ”‚   โ”‚   โ”œโ”€โ”€ useGestureRecognition.ts
โ”‚   โ”‚   โ””โ”€โ”€ useKeyboardShortcuts.ts
โ”‚   โ”œโ”€โ”€ stores/
โ”‚   โ”‚   โ”œโ”€โ”€ handTrackingStore.ts
โ”‚   โ”‚   โ””โ”€โ”€ sceneStore.ts
โ”‚   โ””โ”€โ”€ utils/
โ”‚       โ””โ”€โ”€ collisionDetection.ts  # Proximity detection
โ”œโ”€โ”€ examples/                      # Plugin tutorials
โ”‚   โ”œโ”€โ”€ custom-gesture-plugin.md
โ”‚   โ”œโ”€โ”€ custom-interaction-plugin.md
โ”‚   โ””โ”€โ”€ custom-physics-adapter.md
โ””โ”€โ”€ public/                        # Static assets

Key Algorithms

Hand-to-3D Mapping

Converts MediaPipe's normalized 2D coordinates (0-1) to 3D world space:

  1. Convert to NDC (-1 to 1)
  2. Unproject through camera to get direction vector
  3. Use MediaPipe z-coordinate for depth (5-10 units from camera)
  4. Project along direction to final 3D position

Gesture Detection

  • Pinch: Distance between thumb tip (4) and index tip (8) < 0.05
  • Open Hand: All finger joint angles > 160ยฐ (extended)
  • Fist: All fingertips within 0.15 units of wrist (curled)
  • Debouncing: 100ms to prevent flicker

Collision Detection

  • Proximity: 3D distance between hand cursor and object < 1.5 units
  • Grab offset: Store vector from hand to object center
  • Drag: Update object position = hand position + grab offset

Browser Compatibility

Recommended

  • โœ… Chrome 90+ (full support, best performance)
  • โœ… Edge 90+ (full support)

Supported

  • โš ๏ธ Firefox 88+ (MediaPipe may have minor issues)
  • โš ๏ธ Safari 14+ (WebGL limitations, reduced performance)

Requirements

  • WebGL 2.0 support
  • WebRTC (getUserMedia) for webcam
  • ES2020+ JavaScript features

Troubleshooting

Webcam not working

  • Check permissions: Browser must have webcam access
  • HTTPS required: Some browsers block webcam on http:// (use localhost)
  • Try different browser: Chrome/Edge have best compatibility

Hand tracking not detecting

  • Lighting: Ensure good lighting on your hand
  • Distance: Keep hand 1-3 feet from webcam
  • Background: Plain background helps detection
  • Refresh page: MediaPipe may need reload if it fails to initialize

Low FPS / Performance

  • Close other tabs: MediaPipe is CPU-intensive
  • Reduce hands: Better performance with 1 hand vs 2
  • Lower quality: Check webcam settings (720p vs 1080p)
  • Hardware acceleration: Enable in browser settings

Objects not grabbable

  • Check gesture: Ensure pinch is detected (status panel shows gesture)
  • Distance: Move hand closer to object (within 1.5 unit sphere)
  • Open hand to release: Spread all fingers to drop object

MediaPipe fails to load

  • CDN issue: Check network tab for failed script loads
  • Use VPN: Some regions may block CDN
  • Local hosting: Download MediaPipe files for offline use

Performance Optimization

  • โœ… Hand tracking: 30fps target (debounced updates)
  • โœ… 3D rendering: 60fps (separate from tracking)
  • โœ… Gesture detection: 100ms debounce
  • โœ… Smooth cursor interpolation (lerp 0.3)
  • โœ… Trail effects optimized (10 segments)

Development

Scripts

pnpm dev          # Start dev server (port 5173)
pnpm build        # Build for production
pnpm preview      # Preview production build

Adding New Gestures

Using the Plugin System (Recommended):

import { GesturePlugin } from '@handtrack3d/core';

class MyCustomGesture implements GesturePlugin {
  readonly name = 'custom:my-gesture';
  readonly priority = 50;
  readonly gestureType = 'my-gesture';

  detect(landmarks, settings) {
    // Your detection logic
    return /* boolean */;
  }
}

// Register with detector
detector.registerGesture(new MyCustomGesture());

Direct Modification (Legacy):

  1. Add gesture type to src/types/gesture.types.ts
  2. Implement detection in src/services/gestureDetector.ts
  3. Add visual feedback in src/components/HandTrackingCanvas/HandMesh.tsx

Adding New Objects

  1. Define object in src/types/scene.types.ts
  2. Add to initial state in src/stores/sceneStore.ts
  3. Update InteractiveObject.tsx geometry rendering

Roadmap

Completed (v0.2.0-alpha.0)

  • โœ… Plugin system architecture
  • โœ… Physics simulation (gravity, collisions, throwing)
  • โœ… Rapier physics adapter
  • โœ… Custom gesture plugins (GesturePlugin interface)
  • โœ… Custom interaction plugins (InteractionPlugin interface)
  • โœ… Physics abstraction (PhysicsAdapter interface)
  • โœ… Multi-hand support (up to 2 hands)
  • โœ… npm packages published

Planned (v0.3.0+)

  • Plugin marketplace / discovery
  • Additional physics adapters (Cannon.js, Ammo.js official support)
  • More gesture plugins (swipe, rotate, pinch-to-zoom, two-hand gestures)
  • Performance profiling tools
  • Plugin debugging utilities
  • Settings panel UI (sensitivity, detection thresholds)
  • Custom object creation UI
  • Multi-user collaboration
  • VR/AR integration
  • Gesture recording and playback
  • Example scenes (playground, tutorials)

Links

Repository

npm Packages

Documentation


License

MIT License - See LICENSE file for details.


Acknowledgments

  • MediaPipe by Google for hand tracking ML models
  • Three.js community for 3D rendering
  • React Three Fiber for declarative Three.js
  • Rapier for high-performance physics simulation
  • Built with Claude Opus 4.6 assistance

v0.2.0-alpha.0 โ€ข Plugin System Complete โ€ข Built with โค๏ธ using TypeScript, React, Three.js, and MediaPipe