React Three Fiber support for react-spring animations
npx @tessl/cli install tessl/npm-react-spring--three@10.0.0@react-spring/three provides seamless integration between react-spring's spring-physics animation system and React Three Fiber (R3F) for 3D scenes. It enables developers to animate Three.js objects, materials, and properties using react-spring's declarative API with spring physics.
npm install @react-spring/three @react-three/fiber three@react-three/fiber >= 6.0three >= 0.126react ^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0import { animated, useSpring, useSpringValue } from "@react-spring/three";For shorter syntax:
import { a, useSpring } from "@react-spring/three";import { animated, useSpring } from "@react-spring/three";
import { Canvas } from "@react-three/fiber";
function AnimatedCube() {
const springs = useSpring({
scale: [1, 1, 1],
rotation: [0, Math.PI, 0],
from: { scale: [0, 0, 0], rotation: [0, 0, 0] },
});
return (
<animated.mesh {...springs}>
<boxGeometry args={[1, 1, 1]} />
<meshStandardMaterial color="orange" />
</animated.mesh>
);
}
function App() {
return (
<Canvas>
<AnimatedCube />
</Canvas>
);
}@react-spring/three is built around several key components:
animated.mesh, animated.material, etc.)Animated versions of all Three.js objects and components for use with react-spring animations. Automatically generated from Three.js class names.
const animated: WithAnimated;
const a: WithAnimated; // Short alias
type WithAnimated = {
<T extends ElementType>(wrappedComponent: T): AnimatedComponent<T>;
} & AnimatedPrimitives;
type AnimatedPrimitives = {
[P in Primitives]: AnimatedComponent<FC<JSX.IntrinsicElements[P]>>;
};Core react-spring hooks for creating and managing spring-based animations in Three.js scenes.
function useSpring(config: SpringConfig): SpringValues;
function useSprings(count: number, configs: SpringConfig[]): SpringValues[];
function useSpringRef(): SpringRef;
function useSpringValue(initial: any): SpringValue;
function useTrail(count: number, config: SpringConfig): SpringValues[];
function useTransition<T>(
items: T[],
config: TransitionConfig<T>
): TransitionValues<T>[];Declarative components for spring animations as an alternative to hooks.
function Spring(props: SpringProps): JSX.Element;
function Trail(props: TrailProps): JSX.Element;
function Transition<T>(props: TransitionProps<T>): JSX.Element;Advanced animation capabilities including chaining, imperative control, and utility hooks.
function useChain(refs: SpringRef[], timeSteps?: number[]): void;
function useScroll(config: ScrollConfig): SpringValues;
function useResize(config: ResizeConfig): SpringValues;
function useInView(config: InViewConfig): [SpringValues, RefCallback];Core type definitions used throughout the animated Three.js ecosystem.
type AnimatedComponent<T extends ElementType> =
ForwardRefExoticComponent<AnimatedProps<ComponentPropsWithRef<T>>>;
type AnimatedProps<Props extends object> = {
[P in keyof Props]: P extends 'ref' | 'key'
? Props[P]
: AnimatedProp<Props[P]>;
};
type AnimatedProp<T> = T | SpringValue<T>;
type Primitives = keyof JSX.IntrinsicElements;
type AnimatedPrimitives = {
[P in Primitives]: AnimatedComponent<FC<JSX.IntrinsicElements[P]>>;
};
interface SpringConfig {
from?: any;
to?: any;
config?: {
tension?: number;
friction?: number;
mass?: number;
clamp?: boolean;
precision?: number;
velocity?: number;
duration?: number;
easing?: (t: number) => number;
};
loop?: boolean | LoopConfig;
delay?: number;
immediate?: boolean;
reset?: boolean;
reverse?: boolean;
cancel?: boolean;
pause?: boolean;
onStart?: (result: AnimationResult) => void;
onChange?: (result: AnimationResult) => void;
onRest?: (result: AnimationResult) => void;
}
interface SpringValues {
[key: string]: SpringValue;
}
interface SpringRef {
start(): void;
stop(): void;
update(config: Partial<SpringConfig>): void;
set(values: any): void;
}
class SpringValue<T = any> {
get(): T;
set(value: T): void;
start(config?: SpringConfig): Promise<void>;
stop(): void;
to(value: T): SpringValue<T>;
}
interface LoopConfig {
reverse?: boolean;
reset?: boolean;
}
interface AnimationResult {
value: any;
finished: boolean;
cancelled: boolean;
}