feat: added task 3 in r3f

This commit is contained in:
divyap 2026-04-03 15:34:54 +05:30
parent 34f4bc743d
commit 3970740f88
2 changed files with 72 additions and 11 deletions

View File

@ -1,10 +1,12 @@
* { .app-container {
margin: 0; width: 100vw;
padding: 0; height: 100vh;
box-sizing: border-box; position: relative;
background-color: white;
overflow: hidden;
} }
body { .canvas-wrapper {
overflow: hidden; width: 100%;
background-color: #111; height: 100%;
font-family: sans-serif; }

View File

@ -1,5 +1,64 @@
import React, { useState } from 'react';
import { Canvas } from '@react-three/fiber';
import { OrbitControls, PerspectiveCamera } from '@react-three/drei';
import './App.css';
const variants = {
black: {
geometry: <boxGeometry args={[2, 2, 2]} />,
material: { color: "black", roughness: 1, metalness: 0 }
},
silver: {
geometry: <sphereGeometry args={[1.5, 32, 32]} />,
material: { color: "#C0C0C0", roughness: 0.3, metalness: 1 }
},
gold: {
geometry: <torusGeometry args={[1.2, 0.4, 16, 100]} />,
material: { color: "#FFD700", roughness: 0.4, metalness: 1 }
}
};
function Product({ variant }) {
const { geometry, material } = variants[variant];
return (
<mesh>
{geometry}
<meshStandardMaterial {...material} />
</mesh>
);
}
export default function App() { export default function App() {
return ( const [activeVariant, setActiveVariant] = useState('black');
<></>
) return (
<div className="app-container">
<div id="controls">
{Object.keys(variants).map((variant) => (
<button
key={variant}
className={activeVariant === variant ? 'active' : ''}
onClick={() => setActiveVariant(variant)}
>
{variant.charAt(0).toUpperCase() + variant.slice(1)}
</button>
))}
</div>
<div className="canvas-wrapper">
<Canvas dpr={[1, 2]}>
<color attach="background" args={["white"]} />
<PerspectiveCamera makeDefault position={[0, 0, 5]} />
<ambientLight intensity={0.6 * Math.PI} />
<pointLight position={[5, 5, 5]} intensity={1.0 * Math.PI} />
<Product variant={activeVariant} />
<OrbitControls makeDefault enableDamping dampingFactor={0.05} />
</Canvas>
</div>
</div>
);
} }