feat: implement multi-object selection behavior audit for week 3 task 4 in r3f
This commit is contained in:
parent
d3989465e4
commit
1f72a26085
@ -4,6 +4,34 @@
|
|||||||
position: relative;
|
position: relative;
|
||||||
background-color: white;
|
background-color: white;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
|
font-family: 'Inter', system-ui, -apple-system, sans-serif;
|
||||||
|
}
|
||||||
|
|
||||||
|
#instructions {
|
||||||
|
position: absolute;
|
||||||
|
top: 20px;
|
||||||
|
left: 20px;
|
||||||
|
background: rgba(255, 255, 255, 0.85);
|
||||||
|
backdrop-filter: blur(12px);
|
||||||
|
padding: 24px;
|
||||||
|
border-radius: 16px;
|
||||||
|
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
|
||||||
|
border: 1px solid rgba(255, 255, 255, 0.3);
|
||||||
|
max-width: 300px;
|
||||||
|
z-index: 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
h2 {
|
||||||
|
margin: 0 0 12px 0;
|
||||||
|
font-size: 1.25rem;
|
||||||
|
color: #1a1a1a;
|
||||||
|
}
|
||||||
|
|
||||||
|
p {
|
||||||
|
margin: 0 0 12px 0;
|
||||||
|
font-size: 0.95rem;
|
||||||
|
color: #4a4a4a;
|
||||||
|
line-height: 1.5;
|
||||||
}
|
}
|
||||||
|
|
||||||
.canvas-wrapper {
|
.canvas-wrapper {
|
||||||
|
|||||||
@ -3,42 +3,65 @@ import { Canvas } from '@react-three/fiber';
|
|||||||
import { OrbitControls, PerspectiveCamera } from '@react-three/drei';
|
import { OrbitControls, PerspectiveCamera } from '@react-three/drei';
|
||||||
import './App.css';
|
import './App.css';
|
||||||
|
|
||||||
function InteractiveObject() {
|
function Selectable({ id, position, color, geometry, selectedId, onSelect }) {
|
||||||
const [selected, setSelected] = useState(false);
|
const isSelected = selectedId === id;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<mesh
|
<mesh
|
||||||
onClick={() => setSelected(!selected)}
|
position={position}
|
||||||
scale={selected ? 1.2 : 1}
|
onClick={(e) => {
|
||||||
rotation={[0, 0, 0]}
|
e.stopPropagation();
|
||||||
|
onSelect(isSelected ? null : id);
|
||||||
|
}}
|
||||||
|
scale={isSelected ? 1.2 : 1}
|
||||||
>
|
>
|
||||||
<torusKnotGeometry args={[1, 0.4, 100, 16]} />
|
{geometry}
|
||||||
<meshStandardMaterial
|
<meshStandardMaterial
|
||||||
color={selected ? "#3498db" : "#ccc"}
|
color={color}
|
||||||
emissive={selected ? "#1e3799" : "#000"}
|
emissive={isSelected ? "#444" : "black"}
|
||||||
emissiveIntensity={selected ? 0.5 : 0}
|
roughness={0.4}
|
||||||
roughness={0.5}
|
metalness={0.1}
|
||||||
/>
|
/>
|
||||||
</mesh>
|
</mesh>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function App() {
|
export default function App() {
|
||||||
|
const [selectedId, setSelectedId] = useState(null);
|
||||||
|
|
||||||
|
const objects = [
|
||||||
|
{ id: 'box', position: [-4, 0, 0], color: "#ff4f4f", geometry: <boxGeometry args={[2, 2, 2]} /> },
|
||||||
|
{ id: 'sphere', position: [0, 0, 0], color: "#4f8cff", geometry: <sphereGeometry args={[1.2, 32, 32]} /> },
|
||||||
|
{ id: 'torus', position: [4, 0, 0], color: "#4fff8c", geometry: <torusKnotGeometry args={[0.8, 0.3, 100, 16]} /> }
|
||||||
|
];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="app-container">
|
<div className="app-container">
|
||||||
<div id="overlay">
|
<div id="instructions">
|
||||||
<h1>Interaction Pattern — Click on the object</h1>
|
<h2>Task 4 Audit (R3F)</h2>
|
||||||
|
<p>Click objects to select/deselect them. State managed via React.</p>
|
||||||
|
<p><strong>Selected:</strong> {selectedId || 'None'}</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="canvas-wrapper">
|
<div className="canvas-wrapper">
|
||||||
<Canvas dpr={[1, 2]}>
|
<Canvas
|
||||||
|
dpr={[1, 2]}
|
||||||
|
onPointerMissed={() => setSelectedId(null)}
|
||||||
|
>
|
||||||
<color attach="background" args={["white"]} />
|
<color attach="background" args={["white"]} />
|
||||||
<PerspectiveCamera makeDefault position={[0, 0, 5]} />
|
<PerspectiveCamera makeDefault position={[0, 2, 8]} />
|
||||||
|
|
||||||
<ambientLight intensity={0.6 * Math.PI} />
|
<ambientLight intensity={0.5 * Math.PI} />
|
||||||
<pointLight position={[5, 5, 5]} intensity={1.0 * Math.PI} />
|
<directionalLight position={[5, 10, 5]} intensity={1.0 * Math.PI} />
|
||||||
|
|
||||||
<InteractiveObject />
|
{objects.map((obj) => (
|
||||||
|
<Selectable
|
||||||
|
key={obj.id}
|
||||||
|
{...obj}
|
||||||
|
selectedId={selectedId}
|
||||||
|
onSelect={setSelectedId}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
|
||||||
<OrbitControls makeDefault enableDamping dampingFactor={0.05} />
|
<OrbitControls makeDefault enableDamping dampingFactor={0.05} />
|
||||||
</Canvas>
|
</Canvas>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user