PluginBench
Skill
Pass
Audit score 90

r3f-interaction

enzed/r3f-skills

Implement pointer events, picking, dragging, keyboard input, and camera controls in React Three Fiber scenes.

What is r3f-interaction?

This skill adds interactive controls to R3F scenes, including object selection, dragging, camera movement, and keyboard input handling. Use it when you need users to interact with 3D objects or navigate the scene, rather than for animation or physics simulation.

  • Handle pointer events (click, drag, hover) on 3D objects with proper event propagation and raycasting
  • Implement object dragging with plane projection and transform management
  • Set up camera controls (OrbitControls, CameraControls) for scene navigation
  • Add keyboard input handling via KeyboardControls with frame-based input reading
  • Manage pointer capture, focus loss, and touch interactions correctly
  • Use hit proxies and layer filtering for efficient picking on complex scenes

How to install r3f-interaction

npx skills add https://github.com/enzed/r3f-skills --skill r3f-interaction
Prerequisites
  • React Three Fiber 9+ and React 19+
  • @react-three/drei for controls (OrbitControls, CameraControls, DragControls, KeyboardControls)
  • Understanding of R3F event system and raycasting basics
Claude Code
Cursor
Windsurf
Cline

How to use r3f-interaction

  1. 1.Check your installed Fiber, Drei, and input-library versions
  2. 2.Choose which component owns camera movement (OrbitControls vs CameraControls) and object movement (DragControls vs custom)
  3. 3.Mount interactive meshes with pointer event handlers (onClick, onPointerDown, etc.) beneath Canvas
  4. 4.Use event.stopPropagation() to prevent event bubbling and manage hit detection order
  5. 5.For dragging, store initial transforms, capture the pointer, project onto a plane, and release on pointerup
  6. 6.Add KeyboardControls context and read input state inside useFrame for continuous movement
  7. 7.Test overlapping meshes, background deselection, dragging outside canvas, touch, and focus loss scenarios

Use cases

Good for
  • Build interactive 3D model viewers where users can select and drag objects
  • Create scene navigation with orbit or scripted camera controls
  • Implement keyboard-driven character or camera movement in 3D environments
  • Add clickable UI elements within a 3D scene with proper event handling
  • Handle multi-touch and pointer capture for complex interaction patterns
Who it's for
  • React Three Fiber developers building interactive 3D applications
  • Game developers using R3F for scene interaction
  • 3D visualization and modeling tool creators
  • Developers implementing custom camera and object manipulation controls

r3f-interaction FAQ

How do I select and deselect 3D objects with clicks?

Mount a mesh with an onClick handler that calls event.stopPropagation() to prevent bubbling, then update state to track selection. Use Canvas onPointerMissed for background deselection.

What's the difference between OrbitControls and CameraControls?

OrbitControls orbits around a target point; CameraControls supports scripted transitions and richer camera behavior. Choose based on your interaction needs and check Drei version for available props.

How do I drag objects in 3D space?

Store the initial transform, capture the pointer, project the 2D drag onto a chosen plane (using event.point and local space conversion), then apply the delta to the object's position.

Why isn't my keyboard input working?

Wrap your scene in KeyboardControls context, read input inside useFrame using the provided getter, and ensure the canvas has focus. Clear held input on focus loss.

How do I prevent expensive picking on complex scenes?

Use simpler hit proxies (invisible planes or boxes), layer filtering, or raycasting only on specific object groups instead of relying on visual transparency.

Full instructions (SKILL.md)

Source of truth, from enzed/r3f-skills.


name: r3f-interaction description: Implement React Three Fiber pointer events, picking, dragging, keyboard input, and camera controls. Use for scene interaction and event propagation, rather than animation or physics simulation itself.

React Three Fiber interaction

Check installed Fiber, Drei, and input-library versions first. Examples target Fiber 9 / React 19. Choose camera movement, object movement, and UI input owners before connecting handlers.

Selectable object

Mount beneath Canvas. Keep accessible DOM controls outside Canvas for important actions; mesh pointer handlers alone do not provide keyboard access.

import { useState } from 'react'
import { OrbitControls } from '@react-three/drei'

export default function Example() {
  const [selected, setSelected] = useState(false)
  return (
    <>
      <mesh
        name="selectable-box"
        onClick={(event) => {
          event.stopPropagation()
          setSelected((value) => !value)
        }}
      >
        <boxGeometry />
        <meshStandardMaterial color={selected ? 'gold' : 'coral'} />
      </mesh>
      <OrbitControls makeDefault />
    </>
  )
}

Event semantics

  • R3F raycasts objects with handlers and delivers hits by distance, then bubbles through ancestors. event.object is the hit object; event.eventObject is the object owning the handler.
  • stopPropagation() blocks delivery to farther hits as well as ancestors. It changes event delivery; it does not avoid raycasts already performed. Calling it can immediately trigger pointerout on previously hovered objects behind the hit.
  • event.point is world space. Clone values you retain and convert into the parent's local space before assigning local transforms. A plane intersection or depth reference is needed to map a 2D drag into 3D.
  • Handle Canvas onPointerMissed for background deselection; distinguish clicks from drags using the event's movement information and the application's gesture policy.
  • Pointer capture is additive to hit testing in R3F. Capture/release via event.target.setPointerCapture(event.pointerId) and the matching release method; also handle cancellation/lost capture.
  • Camera movement beneath a stationary pointer may require state.events.update() to refresh hover results. Call it only when needed, not as a blanket extra raycast every frame.
  • Use simpler hit proxies or layer filtering for expensive picking. Do not rely on visual transparency alone to prevent intersections.

Controls and drag ownership

  • Use OrbitControls for orbiting; use CameraControls when scripted transitions and richer camera behavior are needed. Check the installed Drei/underlying controls version before copying props or methods.
  • Drei controls manage frame updates and demand invalidation. Avoid mounting multiple active controls on the same camera without explicit coordination.
  • makeDefault exposes a controls instance to helpers that coordinate with it. TransformControls can suspend default controls while dragging; verify that custom controls are also disabled/restored correctly.
  • For object dragging, choose Drei DragControls/PivotControls/TransformControls according to required constraints. Distinguish controlled matrices from automatic transforms; do not apply both to the same object.
  • For custom drags, store initial transforms, capture the pointer, project onto a chosen plane, and restore camera controls on pointerup, cancellation, and unmount. Honor touch-action requirements on the actual event target.
  • Shared DOM event sources need a coordinate prefix consistent with the event target and canvas rectangle. Test scrolling and overlays; blindly selecting client coordinates can introduce offsets.

Keyboard and continuous input

  • Drei KeyboardControls provides named actions. Read its getter inside useFrame for movement; subscribe only for discrete events and clean up subscriptions.
  • Normalize diagonal movement if speed must remain constant; multiply visual movement by delta. For a physics body, send input to physics-step logic instead of moving its mesh directly.
  • Clear held input on focus loss and respect text-field focus. Pointer lock/fullscreen/audio may require an explicit user gesture.
  • Use refs for high-frequency pointer positions and React state for semantic events. Avoid adding lodash or a store solely to throttle one handler; clean up timers if throttling is warranted.
  • ScrollControls creates a scroll context; useScroll belongs below it. Its normalized offset/delta are not pixel distances.

Verify

Test overlapping meshes, nested groups, background deselection, dragging outside the canvas, touch, cancellation, focus loss, and keyboard alternatives. Verify camera controls recover after dragging.

Sources