feat: added task 3 in vanilla

This commit is contained in:
divyap 2026-04-03 15:34:28 +05:30
parent b3133aae0e
commit 34f4bc743d
2 changed files with 103 additions and 4 deletions

View File

@ -1,11 +1,18 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Task 3 — UI-Controlled Product Option</title>
<title>Task 3 - UI-Controlled Product Option (Configurator Pattern)</title> <link rel="stylesheet" href="./style.css">
</head> </head>
<body> <body>
<script type="module" src="main.js"></script>
<div id="controls">
<button data-variant="black" class="active">Black</button>
<button data-variant="silver">Silver</button>
<button data-variant="gold">Gold</button>
</div>
<script type="module" src="./main.js"></script>
</body> </body>
</html> </html>

View File

@ -0,0 +1,92 @@
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
const scene = new THREE.Scene();
scene.background = new THREE.Color("white");
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.position.z = 5;
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const geometries = {
black: new THREE.BoxGeometry(2, 2, 2),
silver: new THREE.SphereGeometry(1.5, 32, 32),
gold: new THREE.TorusGeometry(1.2, 0.4, 16, 100),
};
const materials = {
black: new THREE.MeshStandardMaterial({
color: "black",
roughness: 1,
metalness: 0,
}),
silver: new THREE.MeshStandardMaterial({
color: "#C0C0C0",
roughness: 0.3,
metalness: 1,
}),
gold: new THREE.MeshStandardMaterial({
color: "#FFD700",
roughness: 0.4,
metalness: 1,
}),
};
let currentVariant = "black";
let mesh = new THREE.Mesh(
geometries[currentVariant],
materials[currentVariant]
);
scene.add(mesh);
scene.add(new THREE.AmbientLight(0xffffff, 0.6));
const pointLight = new THREE.PointLight(0xffffff, 1);
pointLight.position.set(5, 5, 5);
scene.add(pointLight);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
function updateScene() {
mesh.geometry = geometries[currentVariant];
mesh.material = materials[currentVariant];
}
const buttons = document.querySelectorAll("button");
buttons.forEach((btn) => {
btn.addEventListener("click", () => {
const variant = btn.getAttribute("data-variant");
currentVariant = variant;
updateScene();
buttons.forEach((b) => b.classList.remove("active"));
btn.classList.add("active");
});
});
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);
});