Give me a replica of spline.design canvas using React three fiber and three js
- User
Give me a replica of spline.design canvas using React three fiber and three js
Assistant
import React, { useRef, useState } from 'react' import { Canvas, useFrame, extend, useThree } from '@react-three/fiber' import { OrbitControls, Grid, Environment, PerspectiveCamera, useHelper } from '@react-three/drei' import * as THREE from 'three' import { TransformControls } from 'three/examples/jsm/controls/TransformControls' // Extend transform controls to use it in JSX extend({ TransformControls }) // Simple component for a selectable object const SelectableObject = ({ position, color, onClick, selected }) => { const mesh = useRef() return ( <mesh ref={mesh} position={position} onClick={(e) => { e.stopPropagation(); onClick(); }} > <boxGeometry args={[1, 1, 1]} /> <meshStandardMaterial color={selected ? '#ff8888' : color} /> </mesh> ) } // Transform controls component const ObjectTransformControls = ({ object, mode }) => { const { camera, gl } = useThree() const controls = useRef() useFrame(() => { if (controls.current) { controls.current.setMode(mode) } }) return object ? ( <transformControls ref={controls} args={[camera, gl.domElement]} object={object} /> ) : null } // Main canvas component const SplineCanvas = () => { const [objects, setObjects] = useState([ { id: 1, position: [0, 0, 0], color: '#ff0000' }, { id: 2, position: [2, 0, 0], color: '#00ff00' }, { id: 3, position: [0, 0, 2], color: '#0000ff' } ]) const [selectedObject, setSelectedObject] = useState(null) const [transformMode, setTransformMode] = useState('translate') // 'translate', 'rotate', 'scale' const selectedRef = useRef() const handleAddCube = () => { const newId = objects.length + 1 setObjects([ ...objects, { id: newId, position: [Math.random() * 4 - 2, 0, Math.random() * 4 - 2], color: `#${Math.floor(Math.random()*16777215).toString(16)}` } ]) } return ( <div style={{ width: '100vw', height: '100vh', position: 'relative' }}> <Canvas shadows> <PerspectiveCamera makeDefault position={[5, 5, 5]} /> <OrbitControls enableDamping dampingFactor={0.05} rotateSpeed={0.5} /> {/* Environment and lighting */} <ambientLight intensity={0.3} /> <directionalLight position={[10, 10, 5]} intensity={1} castShadow shadow-mapSize-width={2048} shadow-mapSize-height={2048} /> <Environment preset="studio" /> {/* Grid */} <Grid cellSize={1} cellThickness={0.5} cellColor="#6f6f6f" sectionSize={3} sectionThickness={1} sectionColor="#9d4b4b" fadeDistance={30} infiniteGrid /> {/* Objects */} {objects.map((obj) => ( <SelectableObject key={obj.id} position={obj.position} color={obj.color} selected={selectedObject === obj.id} onClick={() => { setSelectedObject(obj.id); selectedRef.current = obj; }} /> ))} {/* Transform controls for selected object */} {selectedObject && ( <ObjectTransformControls object={selectedRef.current} mode={transformMode} /> )} </Canvas> {/* UI Controls */} <div style={{ position: 'absolute', top: 10, left: 10 }}> <button onClick={handleAddCube}>Add Cube</button> <div style={{ marginTop: 10 }}> <button onClick={() => setTransformMode('translate')} style={{ backgroundColor: transformMode === 'translate' ? '#aaffaa' : 'white' }} > Move </button> <button onClick={() => setTransformMode('rotate')} style={{ backgroundColor: transformMode === 'rotate' ? '#aaffaa' : 'white' }} > Rotate </button> <button onClick={() => setTransformMode('scale')} style={{ backgroundColor: transformMode === 'scale' ? '#aaffaa' : 'white' }} > Scale </button> </div> </div> </div> ) } // Root component export default function App() { return <SplineCanvas /> }Would you like me to explain or break down this code?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.