web3d-integration-patterns by freshtechbro/claudedesignskills
npx skills add https://github.com/freshtechbro/claudedesignskills --skill web3d-integration-patterns此元技能为在 Web 应用程序中组合多个 3D 和动画库提供了架构模式、最佳实践和集成策略。它综合了来自 threejs-webgl、gsap-scrolltrigger、react-three-fiber、motion-framer 和 react-spring-physics 技能的知识,形成了用于构建复杂、高性能 3D Web 体验的连贯模式。
何时使用此技能:
核心集成组合:
使用场景: 带有叠加 UI、滚动驱动动画的 3D 场景
架构:
├── 3D 层 (Three.js)
│ ├── 场景管理
│ ├── 相机控制
│ └── 渲染循环
├── 动画层 (GSAP)
│ ├── 用于 3D 属性的 ScrollTrigger
│ ├── 序列时间线
│ └── UI 过渡
└── UI 层 (React + Motion)
├── HTML 叠加层
├── 状态管理
└── 用户交互
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
实现:
// App.jsx - React 根组件
import { useEffect, useRef } from 'react'
import { initThreeScene } from './three/scene'
import { initScrollAnimations } from './animations/scroll'
import { motion } from 'framer-motion'
function App() {
const canvasRef = useRef()
const sceneRef = useRef()
useEffect(() => {
// 初始化 Three.js 场景
sceneRef.current = initThreeScene(canvasRef.current)
// 初始化 GSAP ScrollTrigger 动画
initScrollAnimations(sceneRef.current)
// 清理
return () => {
sceneRef.current.dispose()
}
}, [])
return (
<div className="app">
<canvas ref={canvasRef} />
<motion.div
className="overlay"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
>
<section className="hero">
<h1>3D 体验</h1>
</section>
<section className="content">
{/* 可滚动内容 */}
</section>
</motion.div>
</div>
)
}
// three/scene.js - Three.js 设置
import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
export function initThreeScene(canvas) {
const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true, alpha: true })
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
const controls = new OrbitControls(camera, canvas)
controls.enableDamping = true
// 设置场景对象
const geometry = new THREE.BoxGeometry(2, 2, 2)
const material = new THREE.MeshStandardMaterial({ color: 0x00ff00 })
const cube = new THREE.Mesh(geometry, material)
scene.add(cube)
// 光照
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5)
scene.add(ambientLight)
const directionalLight = new THREE.DirectionalLight(0xffffff, 1)
directionalLight.position.set(5, 10, 7.5)
scene.add(directionalLight)
camera.position.set(0, 2, 5)
// 动画循环
function animate() {
requestAnimationFrame(animate)
controls.update()
renderer.render(scene, camera)
}
animate()
// 调整大小处理程序
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
renderer.setSize(window.innerWidth, window.innerHeight)
})
return { scene, camera, renderer, cube }
}
// animations/scroll.js - GSAP ScrollTrigger 集成
import gsap from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger'
gsap.registerPlugin(ScrollTrigger)
export function initScrollAnimations(sceneRefs) {
const { camera, cube } = sceneRefs
// 滚动时动画相机
gsap.to(camera.position, {
x: 5,
y: 3,
z: 10,
scrollTrigger: {
trigger: '.content',
start: 'top top',
end: 'bottom center',
scrub: 1,
onUpdate: () => camera.lookAt(cube.position)
}
})
// 动画网格旋转
gsap.to(cube.rotation, {
y: Math.PI * 2,
x: Math.PI,
scrollTrigger: {
trigger: '.content',
start: 'top bottom',
end: 'bottom top',
scrub: true
}
})
// 动画材质属性
gsap.to(cube.material, {
opacity: 0.3,
scrollTrigger: {
trigger: '.content',
start: 'top center',
end: 'center center',
scrub: 1
}
})
}
优点:
权衡:
使用场景: 以 React 为先的架构,带有声明式 3D 和动画
架构:
React 组件树
├── <Canvas> (R3F)
│ ├── 3D 场景组件
│ ├── 灯光
│ ├── 相机
│ └── 效果
└── <motion.div> (UI 叠加层)
├── HTML 内容
└── 动画
实现:
// App.jsx - 统一的 React 方法
import { Canvas } from '@react-three/fiber'
import { Suspense } from 'react'
import { motion } from 'framer-motion'
import { Scene } from './components/Scene'
import { Loader } from './components/Loader'
function App() {
return (
<div className="app">
<Canvas
camera={{ position: [0, 2, 5], fov: 75 }}
dpr={[1, 2]}
shadows
>
<Suspense fallback={<Loader />}>
<Scene />
</Suspense>
</Canvas>
<motion.div
className="ui-overlay"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 1 }}
>
<h1>以 React 为先的 3D 体验</h1>
</motion.div>
</div>
)
}
// components/Scene.jsx - R3F 场景
import { useRef, useState } from 'react'
import { useFrame } from '@react-three/fiber'
import { OrbitControls, Environment } from '@react-three/drei'
import { motion } from 'framer-motion-3d'
export function Scene() {
return (
<>
<ambientLight intensity={0.5} />
<directionalLight position={[5, 10, 7.5]} castShadow />
<AnimatedCube />
<Floor />
<OrbitControls enableDamping dampingFactor={0.05} />
<Environment preset="sunset" />
</>
)
}
function AnimatedCube() {
const [hovered, setHovered] = useState(false)
const [active, setActive] = useState(false)
return (
<motion.mesh
scale={active ? 1.5 : 1}
onClick={() => setActive(!active)}
onPointerOver={() => setHovered(true)}
onPointerOut={() => setHovered(false)}
animate={{
rotateY: hovered ? Math.PI * 2 : 0
}}
transition={{ type: 'spring', stiffness: 200, damping: 20 }}
>
<boxGeometry args={[2, 2, 2]} />
<meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
</motion.mesh>
)
}
function Floor() {
return (
<mesh rotation={[-Math.PI / 2, 0, 0]} position={[0, -1, 0]} receiveShadow>
<planeGeometry args={[100, 100]} />
<meshStandardMaterial color="#222" />
</mesh>
)
}
优点:
权衡:
使用场景: 具有 React 状态管理的复杂动画序列
实现:
// components/AnimatedScene.jsx
import { useRef, useEffect } from 'react'
import { useFrame } from '@react-three/fiber'
import gsap from 'gsap'
export function AnimatedScene() {
const groupRef = useRef()
const timelineRef = useRef()
useEffect(() => {
// 为复杂序列创建 GSAP 时间线
const tl = gsap.timeline({ repeat: -1, yoyo: true })
tl.to(groupRef.current.position, {
y: 2,
duration: 1,
ease: 'power2.inOut'
})
.to(groupRef.current.rotation, {
y: Math.PI * 2,
duration: 2,
ease: 'none'
}, 0) // 同时开始
timelineRef.current = tl
return () => tl.kill()
}, [])
return (
<group ref={groupRef}>
<mesh>
<boxGeometry />
<meshStandardMaterial color="cyan" />
</mesh>
</group>
)
}
使用场景: 自然的、物理驱动的 3D 交互
实现:
// components/PhysicsCube.jsx
import { useRef } from 'react'
import { useFrame } from '@react-three/fiber'
import { useSpring, animated, config } from '@react-spring/three'
const AnimatedMesh = animated('mesh')
export function PhysicsCube() {
const [springs, api] = useSpring(() => ({
scale: 1,
position: [0, 0, 0],
config: config.wobbly
}), [])
const handleClick = () => {
api.start({
scale: 1.5,
position: [0, 2, 0]
})
// 延迟后返回原始状态
setTimeout(() => {
api.start({
scale: 1,
position: [0, 0, 0]
})
}, 1000)
}
return (
<AnimatedMesh
scale={springs.scale}
position={springs.position}
onClick={handleClick}
>
<boxGeometry />
<meshStandardMaterial color="orange" />
</AnimatedMesh>
)
}
Three.js + GSAP:
import gsap from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger'
gsap.registerPlugin(ScrollTrigger)
// 通过多个点的平滑相机路径
const cameraPath = [
{ x: 0, y: 2, z: 5, lookAt: { x: 0, y: 0, z: 0 } },
{ x: 5, y: 3, z: 10, lookAt: { x: 0, y: 0, z: 0 } },
{ x: -3, y: 1, z: 8, lookAt: { x: 0, y: 0, z: 0 } }
]
const tl = gsap.timeline({
scrollTrigger: {
trigger: '#container',
start: 'top top',
end: 'bottom bottom',
scrub: 1,
pin: true
}
})
cameraPath.forEach((point, i) => {
tl.to(camera.position, {
x: point.x,
y: point.y,
z: point.z,
duration: 1,
onUpdate: () => camera.lookAt(point.lookAt.x, point.lookAt.y, point.lookAt.z)
}, i)
})
R3F + ScrollControls (Drei):
import { ScrollControls, Scroll, useScroll } from '@react-three/drei'
import { useFrame } from '@react-three/fiber'
function CameraRig() {
const scroll = useScroll()
useFrame((state) => {
const offset = scroll.offset
state.camera.position.x = Math.sin(offset * Math.PI * 2) * 5
state.camera.position.z = Math.cos(offset * Math.PI * 2) * 5
state.camera.lookAt(0, 0, 0)
})
return null
}
export function App() {
return (
<Canvas>
<ScrollControls pages={3} damping={0.5}>
<CameraRig />
<Scroll>
<Scene />
</Scroll>
</ScrollControls>
</Canvas>
)
}
R3F + Motion (Framer Motion 3D):
import { motion } from 'framer-motion-3d'
function DraggableObject() {
return (
<motion.mesh
drag
dragElastic={0.1}
dragConstraints={{ left: -5, right: 5, top: 5, bottom: -5 }}
whileHover={{ scale: 1.1 }}
whileTap={{ scale: 0.9 }}
animate={{
rotateY: [0, Math.PI * 2],
transition: { repeat: Infinity, duration: 4, ease: 'linear' }
}}
>
<sphereGeometry args={[1, 32, 32]} />
<meshStandardMaterial color="hotpink" />
</motion.mesh>
)
}
R3F + Zustand + GSAP:
// store.js
import create from 'zustand'
export const useStore = create((set) => ({
selectedObject: null,
cameraMode: 'orbit',
setSelectedObject: (obj) => set({ selectedObject: obj }),
setCameraMode: (mode) => set({ cameraMode: mode })
}))
// components/InteractiveObject.jsx
import { useRef, useEffect } from 'react'
import { useStore } from '../store'
import gsap from 'gsap'
export function InteractiveObject({ id }) {
const meshRef = useRef()
const selectedObject = useStore((state) => state.selectedObject)
const setSelectedObject = useStore((state) => state.setSelectedObject)
const isSelected = selectedObject === id
useEffect(() => {
if (isSelected) {
gsap.to(meshRef.current.scale, {
x: 1.2,
y: 1.2,
z: 1.2,
duration: 0.3,
ease: 'back.out'
})
gsap.to(meshRef.current.material, {
emissiveIntensity: 0.5,
duration: 0.3
})
} else {
gsap.to(meshRef.current.scale, {
x: 1,
y: 1,
z: 1,
duration: 0.3,
ease: 'power2.inOut'
})
gsap.to(meshRef.current.material, {
emissiveIntensity: 0,
duration: 0.3
})
}
}, [isSelected])
return (
<mesh
ref={meshRef}
onClick={() => setSelectedObject(isSelected ? null : id)}
>
<boxGeometry />
<meshStandardMaterial color="cyan" emissive="cyan" />
</mesh>
)
}
最适合: 跨 3D 场景和 UI 的共享状态
// store/scene.js
import create from 'zustand'
export const useSceneStore = create((set, get) => ({
// 状态
camera: { position: [0, 2, 5], target: [0, 0, 0] },
objects: {},
selectedId: null,
isAnimating: false,
// 操作
updateCamera: (updates) => set((state) => ({
camera: { ...state.camera, ...updates }
})),
addObject: (id, object) => set((state) => ({
objects: { ...state.objects, [id]: object }
})),
selectObject: (id) => set({ selectedId: id }),
setAnimating: (isAnimating) => set({ isAnimating })
}))
在 R3F 中的使用:
import { useSceneStore } from '../store/scene'
function Object3D({ id }) {
const selectedId = useSceneStore((state) => state.selectedId)
const selectObject = useSceneStore((state) => state.selectObject)
const isSelected = selectedId === id
return (
<mesh onClick={() => selectObject(id)}>
<boxGeometry />
<meshStandardMaterial color={isSelected ? 'hotpink' : 'orange'} />
</mesh>
)
}
协调 Three.js 和动画库之间的渲染循环:
// 带有条件渲染的统一渲染循环
import { Clock } from 'three'
const clock = new Clock()
let needsRender = true
function animate() {
requestAnimationFrame(animate)
const delta = clock.getDelta()
const elapsed = clock.getElapsedTime()
// 仅在需要时渲染
if (needsRender || controls.enabled) {
// 更新 GSAP 动画(自动处理)
// 更新 Three.js
controls.update()
renderer.render(scene, camera)
// 重置标志
needsRender = false
}
}
// 在交互时触发重新渲染
ScrollTrigger.addEventListener('update', () => {
needsRender = true
})
import { Canvas } from '@react-three/fiber'
function App() {
return (
<Canvas
frameloop="demand" // 仅在需要时渲染
dpr={[1, 2]} // 自适应像素比
>
<Scene />
</Canvas>
)
}
function Scene() {
const invalidate = useThree((state) => state.invalidate)
// 状态变化时触发渲染
const handleClick = () => {
// 更新状态...
invalidate() // 手动触发渲染
}
return <mesh onClick={handleClick}>...</mesh>
}
问题: 多个库尝试动画化同一属性
// ❌ 错误:GSAP 和 React Spring 都在动画化位置
gsap.to(meshRef.current.position, { x: 5 })
api.start({ position: [10, 0, 0] }) // 冲突!
解决方案: 为每个属性选择一个库或协调时间
// ✅ 正确:分离属性
gsap.to(meshRef.current.position, { x: 5 }) // GSAP 处理位置
api.start({ scale: 1.5 }) // Spring 处理缩放
问题: React 状态与 Three.js 场景不同步
// ❌ 错误:更新 Three.js 而不更新 React 状态
mesh.position.x = 5 // Three.js 已更新
// 但 React 状态仍显示旧值!
解决方案: 使用引用或状态管理
// ✅ 正确:同时更新两者
const updatePosition = (x) => {
mesh.position.x = x
setPosition(x) // 更新 React 状态
}
问题: 卸载时未清理动画
// ❌ 错误:没有清理
useEffect(() => {
gsap.to(meshRef.current.rotation, { y: Math.PI * 2, repeat: -1 })
}, [])
解决方案: 始终在 useEffect 返回中清理
// ✅ 正确:卸载时清理
useEffect(() => {
const tween = gsap.to(meshRef.current.rotation, { y: Math.PI * 2, repeat: -1 })
return () => {
tween.kill()
}
}, [])
| 使用场景 | 推荐技术栈 | 理由 |
|---|---|---|
| 带有滚动驱动 3D 的营销落地页 | Three.js + GSAP + React UI | GSAP 擅长滚动编排 |
| 带有交互式 3D 产品查看器的 React 应用 | R3F + Motion | 声明式、状态驱动、基于组件 |
| 复杂动画序列(基于时间线) | R3F + GSAP | GSAP 时间线控制与 R3F 组件 |
| 基于物理的交互(拖拽、动量) | R3F + React Spring | Spring 物理对于手势感觉自然 |
| 高性能粒子系统 | Three.js + GSAP | 命令式控制、实例化、最小开销 |
| 快速原型设计,快速迭代 | R3F + Drei + Motion | 高级抽象,快速开发 |
| 具有物理效果的类游戏体验 | R3F + React Spring + Cannon (物理) | 物理引擎 + 基于 Spring 的 UI 反馈 |
此技能包含用于多库集成的捆绑资源:
architecture_patterns.md - 详细的架构模式和权衡performance_optimization.md - 跨技术栈的性能策略state_management.md - 3D 应用程序的状态管理模式integration_helper.py - 为库组合生成集成样板代码pattern_generator.py - 搭建常见集成模式starter_unified/ - 结合了 R3F + GSAP + Motion 的完整入门模板examples/ - 真实世界的集成示例基础技能(使用这些获取库特定细节):
何时参考基础技能:
threejs-webglgsap-scrolltriggerreact-three-fibermotion-framerreact-spring-physics此元技能涵盖:
在构建集成了多个动画和渲染库的复杂 3D Web 应用程序时使用此技能。有关库特定的实现细节,请参考各个基础技能。
每周安装次数
83
仓库
GitHub 星标数
11
首次出现
2026年2月27日
安全审计
安装在
opencode82
codex81
cursor80
github-copilot80
amp80
kimi-cli80
This meta-skill provides architectural patterns, best practices, and integration strategies for combining multiple 3D and animation libraries in web applications. It synthesizes knowledge from the threejs-webgl, gsap-scrolltrigger, react-three-fiber, motion-framer, and react-spring-physics skills into cohesive patterns for building complex, performant 3D web experiences.
When to use this skill:
Core Integration Combinations:
Use case: 3D scene with overlaid UI, scroll-driven animations
Architecture:
├── 3D Layer (Three.js)
│ ├── Scene management
│ ├── Camera controls
│ └── Render loop
├── Animation Layer (GSAP)
│ ├── ScrollTrigger for 3D properties
│ ├── Timelines for sequences
│ └── UI transitions
└── UI Layer (React + Motion)
├── HTML overlays
├── State management
└── User interactions
Implementation:
// App.jsx - React root
import { useEffect, useRef } from 'react'
import { initThreeScene } from './three/scene'
import { initScrollAnimations } from './animations/scroll'
import { motion } from 'framer-motion'
function App() {
const canvasRef = useRef()
const sceneRef = useRef()
useEffect(() => {
// Initialize Three.js scene
sceneRef.current = initThreeScene(canvasRef.current)
// Initialize GSAP ScrollTrigger animations
initScrollAnimations(sceneRef.current)
// Cleanup
return () => {
sceneRef.current.dispose()
}
}, [])
return (
<div className="app">
<canvas ref={canvasRef} />
<motion.div
className="overlay"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
>
<section className="hero">
<h1>3D Experience</h1>
</section>
<section className="content">
{/* Scrollable content */}
</section>
</motion.div>
</div>
)
}
// three/scene.js - Three.js setup
import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
export function initThreeScene(canvas) {
const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true, alpha: true })
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
const controls = new OrbitControls(camera, canvas)
controls.enableDamping = true
// Setup scene objects
const geometry = new THREE.BoxGeometry(2, 2, 2)
const material = new THREE.MeshStandardMaterial({ color: 0x00ff00 })
const cube = new THREE.Mesh(geometry, material)
scene.add(cube)
// Lighting
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5)
scene.add(ambientLight)
const directionalLight = new THREE.DirectionalLight(0xffffff, 1)
directionalLight.position.set(5, 10, 7.5)
scene.add(directionalLight)
camera.position.set(0, 2, 5)
// Animation loop
function animate() {
requestAnimationFrame(animate)
controls.update()
renderer.render(scene, camera)
}
animate()
// Resize handler
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
renderer.setSize(window.innerWidth, window.innerHeight)
})
return { scene, camera, renderer, cube }
}
// animations/scroll.js - GSAP ScrollTrigger integration
import gsap from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger'
gsap.registerPlugin(ScrollTrigger)
export function initScrollAnimations(sceneRefs) {
const { camera, cube } = sceneRefs
// Animate camera on scroll
gsap.to(camera.position, {
x: 5,
y: 3,
z: 10,
scrollTrigger: {
trigger: '.content',
start: 'top top',
end: 'bottom center',
scrub: 1,
onUpdate: () => camera.lookAt(cube.position)
}
})
// Animate mesh rotation
gsap.to(cube.rotation, {
y: Math.PI * 2,
x: Math.PI,
scrollTrigger: {
trigger: '.content',
start: 'top bottom',
end: 'bottom top',
scrub: true
}
})
// Animate material properties
gsap.to(cube.material, {
opacity: 0.3,
scrollTrigger: {
trigger: '.content',
start: 'top center',
end: 'center center',
scrub: 1
}
})
}
Benefits:
Trade-offs:
Use case: React-first architecture with declarative 3D and animations
Architecture:
React Component Tree
├── <Canvas> (R3F)
│ ├── 3D Scene Components
│ ├── Lights
│ ├── Camera
│ └── Effects
└── <motion.div> (UI overlays)
├── HTML content
└── Animations
Implementation:
// App.jsx - Unified React approach
import { Canvas } from '@react-three/fiber'
import { Suspense } from 'react'
import { motion } from 'framer-motion'
import { Scene } from './components/Scene'
import { Loader } from './components/Loader'
function App() {
return (
<div className="app">
<Canvas
camera={{ position: [0, 2, 5], fov: 75 }}
dpr={[1, 2]}
shadows
>
<Suspense fallback={<Loader />}>
<Scene />
</Suspense>
</Canvas>
<motion.div
className="ui-overlay"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 1 }}
>
<h1>React-First 3D Experience</h1>
</motion.div>
</div>
)
}
// components/Scene.jsx - R3F scene
import { useRef, useState } from 'react'
import { useFrame } from '@react-three/fiber'
import { OrbitControls, Environment } from '@react-three/drei'
import { motion } from 'framer-motion-3d'
export function Scene() {
return (
<>
<ambientLight intensity={0.5} />
<directionalLight position={[5, 10, 7.5]} castShadow />
<AnimatedCube />
<Floor />
<OrbitControls enableDamping dampingFactor={0.05} />
<Environment preset="sunset" />
</>
)
}
function AnimatedCube() {
const [hovered, setHovered] = useState(false)
const [active, setActive] = useState(false)
return (
<motion.mesh
scale={active ? 1.5 : 1}
onClick={() => setActive(!active)}
onPointerOver={() => setHovered(true)}
onPointerOut={() => setHovered(false)}
animate={{
rotateY: hovered ? Math.PI * 2 : 0
}}
transition={{ type: 'spring', stiffness: 200, damping: 20 }}
>
<boxGeometry args={[2, 2, 2]} />
<meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
</motion.mesh>
)
}
function Floor() {
return (
<mesh rotation={[-Math.PI / 2, 0, 0]} position={[0, -1, 0]} receiveShadow>
<planeGeometry args={[100, 100]} />
<meshStandardMaterial color="#222" />
</mesh>
)
}
Benefits:
Trade-offs:
Use case: Complex animation sequences with React state management
Implementation:
// components/AnimatedScene.jsx
import { useRef, useEffect } from 'react'
import { useFrame } from '@react-three/fiber'
import gsap from 'gsap'
export function AnimatedScene() {
const groupRef = useRef()
const timelineRef = useRef()
useEffect(() => {
// Create GSAP timeline for complex sequence
const tl = gsap.timeline({ repeat: -1, yoyo: true })
tl.to(groupRef.current.position, {
y: 2,
duration: 1,
ease: 'power2.inOut'
})
.to(groupRef.current.rotation, {
y: Math.PI * 2,
duration: 2,
ease: 'none'
}, 0) // Start at same time
timelineRef.current = tl
return () => tl.kill()
}, [])
return (
<group ref={groupRef}>
<mesh>
<boxGeometry />
<meshStandardMaterial color="cyan" />
</mesh>
</group>
)
}
Use case: Natural, physics-driven 3D interactions
Implementation:
// components/PhysicsCube.jsx
import { useRef } from 'react'
import { useFrame } from '@react-three/fiber'
import { useSpring, animated, config } from '@react-spring/three'
const AnimatedMesh = animated('mesh')
export function PhysicsCube() {
const [springs, api] = useSpring(() => ({
scale: 1,
position: [0, 0, 0],
config: config.wobbly
}), [])
const handleClick = () => {
api.start({
scale: 1.5,
position: [0, 2, 0]
})
// Return to original after delay
setTimeout(() => {
api.start({
scale: 1,
position: [0, 0, 0]
})
}, 1000)
}
return (
<AnimatedMesh
scale={springs.scale}
position={springs.position}
onClick={handleClick}
>
<boxGeometry />
<meshStandardMaterial color="orange" />
</AnimatedMesh>
)
}
Three.js + GSAP:
import gsap from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger'
gsap.registerPlugin(ScrollTrigger)
// Smooth camera path through multiple points
const cameraPath = [
{ x: 0, y: 2, z: 5, lookAt: { x: 0, y: 0, z: 0 } },
{ x: 5, y: 3, z: 10, lookAt: { x: 0, y: 0, z: 0 } },
{ x: -3, y: 1, z: 8, lookAt: { x: 0, y: 0, z: 0 } }
]
const tl = gsap.timeline({
scrollTrigger: {
trigger: '#container',
start: 'top top',
end: 'bottom bottom',
scrub: 1,
pin: true
}
})
cameraPath.forEach((point, i) => {
tl.to(camera.position, {
x: point.x,
y: point.y,
z: point.z,
duration: 1,
onUpdate: () => camera.lookAt(point.lookAt.x, point.lookAt.y, point.lookAt.z)
}, i)
})
R3F + ScrollControls (Drei):
import { ScrollControls, Scroll, useScroll } from '@react-three/drei'
import { useFrame } from '@react-three/fiber'
function CameraRig() {
const scroll = useScroll()
useFrame((state) => {
const offset = scroll.offset
state.camera.position.x = Math.sin(offset * Math.PI * 2) * 5
state.camera.position.z = Math.cos(offset * Math.PI * 2) * 5
state.camera.lookAt(0, 0, 0)
})
return null
}
export function App() {
return (
<Canvas>
<ScrollControls pages={3} damping={0.5}>
<CameraRig />
<Scroll>
<Scene />
</Scroll>
</ScrollControls>
</Canvas>
)
}
R3F + Motion (Framer Motion 3D):
import { motion } from 'framer-motion-3d'
function DraggableObject() {
return (
<motion.mesh
drag
dragElastic={0.1}
dragConstraints={{ left: -5, right: 5, top: 5, bottom: -5 }}
whileHover={{ scale: 1.1 }}
whileTap={{ scale: 0.9 }}
animate={{
rotateY: [0, Math.PI * 2],
transition: { repeat: Infinity, duration: 4, ease: 'linear' }
}}
>
<sphereGeometry args={[1, 32, 32]} />
<meshStandardMaterial color="hotpink" />
</motion.mesh>
)
}
R3F + Zustand + GSAP:
// store.js
import create from 'zustand'
export const useStore = create((set) => ({
selectedObject: null,
cameraMode: 'orbit',
setSelectedObject: (obj) => set({ selectedObject: obj }),
setCameraMode: (mode) => set({ cameraMode: mode })
}))
// components/InteractiveObject.jsx
import { useRef, useEffect } from 'react'
import { useStore } from '../store'
import gsap from 'gsap'
export function InteractiveObject({ id }) {
const meshRef = useRef()
const selectedObject = useStore((state) => state.selectedObject)
const setSelectedObject = useStore((state) => state.setSelectedObject)
const isSelected = selectedObject === id
useEffect(() => {
if (isSelected) {
gsap.to(meshRef.current.scale, {
x: 1.2,
y: 1.2,
z: 1.2,
duration: 0.3,
ease: 'back.out'
})
gsap.to(meshRef.current.material, {
emissiveIntensity: 0.5,
duration: 0.3
})
} else {
gsap.to(meshRef.current.scale, {
x: 1,
y: 1,
z: 1,
duration: 0.3,
ease: 'power2.inOut'
})
gsap.to(meshRef.current.material, {
emissiveIntensity: 0,
duration: 0.3
})
}
}, [isSelected])
return (
<mesh
ref={meshRef}
onClick={() => setSelectedObject(isSelected ? null : id)}
>
<boxGeometry />
<meshStandardMaterial color="cyan" emissive="cyan" />
</mesh>
)
}
Best for: Shared state across 3D scene and UI
// store/scene.js
import create from 'zustand'
export const useSceneStore = create((set, get) => ({
// State
camera: { position: [0, 2, 5], target: [0, 0, 0] },
objects: {},
selectedId: null,
isAnimating: false,
// Actions
updateCamera: (updates) => set((state) => ({
camera: { ...state.camera, ...updates }
})),
addObject: (id, object) => set((state) => ({
objects: { ...state.objects, [id]: object }
})),
selectObject: (id) => set({ selectedId: id }),
setAnimating: (isAnimating) => set({ isAnimating })
}))
Usage in R3F:
import { useSceneStore } from '../store/scene'
function Object3D({ id }) {
const selectedId = useSceneStore((state) => state.selectedId)
const selectObject = useSceneStore((state) => state.selectObject)
const isSelected = selectedId === id
return (
<mesh onClick={() => selectObject(id)}>
<boxGeometry />
<meshStandardMaterial color={isSelected ? 'hotpink' : 'orange'} />
</mesh>
)
}
Coordinate render loops between Three.js and animation libraries:
// Unified render loop with conditional rendering
import { Clock } from 'three'
const clock = new Clock()
let needsRender = true
function animate() {
requestAnimationFrame(animate)
const delta = clock.getDelta()
const elapsed = clock.getElapsedTime()
// Only render when needed
if (needsRender || controls.enabled) {
// Update GSAP animations (handled automatically)
// Update Three.js
controls.update()
renderer.render(scene, camera)
// Reset flag
needsRender = false
}
}
// Trigger re-render on interactions
ScrollTrigger.addEventListener('update', () => {
needsRender = true
})
import { Canvas } from '@react-three/fiber'
function App() {
return (
<Canvas
frameloop="demand" // Only renders when needed
dpr={[1, 2]} // Adaptive pixel ratio
>
<Scene />
</Canvas>
)
}
function Scene() {
const invalidate = useThree((state) => state.invalidate)
// Trigger render on state change
const handleClick = () => {
// Update state...
invalidate() // Manually trigger render
}
return <mesh onClick={handleClick}>...</mesh>
}
Problem: Multiple libraries trying to animate the same property
// ❌ Wrong: GSAP and React Spring both animating position
gsap.to(meshRef.current.position, { x: 5 })
api.start({ position: [10, 0, 0] }) // Conflict!
Solution: Choose one library per property or coordinate timing
// ✅ Correct: Separate properties
gsap.to(meshRef.current.position, { x: 5 }) // GSAP handles position
api.start({ scale: 1.5 }) // Spring handles scale
Problem: React state out of sync with Three.js scene
// ❌ Wrong: Updating Three.js without updating React state
mesh.position.x = 5 // Three.js updated
// But React state still shows old value!
Solution: Use refs or state management
// ✅ Correct: Update both
const updatePosition = (x) => {
mesh.position.x = x
setPosition(x) // Update React state
}
Problem: Not cleaning up animations on unmount
// ❌ Wrong: No cleanup
useEffect(() => {
gsap.to(meshRef.current.rotation, { y: Math.PI * 2, repeat: -1 })
}, [])
Solution: Always cleanup in useEffect return
// ✅ Correct: Cleanup on unmount
useEffect(() => {
const tween = gsap.to(meshRef.current.rotation, { y: Math.PI * 2, repeat: -1 })
return () => {
tween.kill()
}
}, [])
| Use Case | Recommended Stack | Rationale |
|---|---|---|
| Marketing landing page with scroll-driven 3D | Three.js + GSAP + React UI | GSAP excels at scroll orchestration |
| React app with interactive 3D product viewer | R3F + Motion | Declarative, state-driven, component-based |
| Complex animation sequences (timeline-based) | R3F + GSAP | GSAP timeline control with R3F components |
| Physics-based interactions (drag, momentum) | R3F + React Spring | Spring physics feel natural for gestures |
| High-performance particle systems | Three.js + GSAP | Imperative control, instancing, minimal overhead |
| Rapid prototyping, quick iterations | R3F + Drei + Motion | High-level abstractions, fast development |
| Game-like experiences with physics | R3F + React Spring + Cannon (physics) | Physics engine + spring-based UI feedback |
This skill includes bundled resources for multi-library integration:
architecture_patterns.md - Detailed architectural patterns and trade-offsperformance_optimization.md - Performance strategies across the stackstate_management.md - State management patterns for 3D applicationsintegration_helper.py - Generate integration boilerplate for library combinationspattern_generator.py - Scaffold common integration patternsstarter_unified/ - Complete starter template combining R3F + GSAP + Motionexamples/ - Real-world integration examplesFoundation Skills (use these for library-specific details):
When to Reference Foundation Skills:
threejs-webglgsap-scrolltriggerreact-three-fibermotion-framerreact-spring-physicsThis Meta-Skill Covers:
Use this skill when building complex 3D web applications that integrate multiple animation and rendering libraries. For library-specific implementation details, reference the individual foundation skills.
Weekly Installs
83
Repository
GitHub Stars
11
First Seen
Feb 27, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode82
codex81
cursor80
github-copilot80
amp80
kimi-cli80
HeroUI v2 到 v3 迁移指南:破坏性变更、复合组件、Tailwind v4 升级
533 周安装