useCamera

Provides imperative camera controls — shake, smooth transitions, and look-at. Used internally by the Camera component, but available directly for advanced use cases.

Import #

tsx
import { useCamera } from "@carverjs/core/hooks";

Signature #

ts
function useCamera(options?: UseCameraOptions): UseCameraReturn;

Parameters #

ParameterTypeDescription
optionsUseCameraOptionsOptional configuration

UseCameraOptions #

FieldTypeDescription
followCameraFollowConfigFollow a target object. See Camera — CameraFollowConfig

Return Value (UseCameraReturn) #

MethodSignatureDescription
shake(intensity?: number, duration?: number) => voidTrigger a camera shake. Default: intensity 0.3, duration 0.3s
moveTo(position: [x, y, z], duration?: number) => voidSmoothly transition to a position. Default duration: 1s. Uses smooth-step easing
lookAt(target: [x, y, z]) => voidImmediately point the camera at a world position

Usage #

Shake on hit #

tsx
const { shake } = useCamera();

function onPlayerHit() {
  shake(0.5, 0.3);
}

Cinematic pan #

tsx
const { moveTo, lookAt } = useCamera();

function startCutscene() {
  moveTo([10, 5, 0], 2); // glide to position over 2 seconds
  lookAt([0, 0, 0]);      // look at the origin
}

Follow a player #

tsx
const playerRef = useRef<Group>(null);

const { shake } = useCamera({
  follow: {
    target: playerRef,
    offset: [0, 8, 12],
    smoothing: 0.05,
  },
});

Type Definitions #

See Types for UseCameraOptions, UseCameraReturn, and CameraFollowConfig.