create an interactive three js website. include everything in 1 single html file
- User
create an interactive three js website. include everything in 1 single html file
Assistant
Interactive Three.js Website (Single HTML File)
Here's a complete interactive Three.js website in a single HTML file. It includes a rotating 3D cube that changes color when clicked, with controls to move the camera around.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Interactive Three.js Demo</title> <style> body { margin: 0; overflow: hidden; font-family: Arial, sans-serif; } canvas { display: block; } #info { position: absolute; top: 10px; left: 10px; color: white; background-color: rgba(0,0,0,0.5); padding: 10px; border-radius: 5px; font-size: 14px; } #controls { position: absolute; bottom: 10px; left: 10px; color: white; background-color: rgba(0,0,0,0.5); padding: 10px; border-radius: 5px; font-size: 14px; } </style> </head> <body> <div id="info"> <h2>Interactive Three.js Demo</h2> <p>Click the cube to change its color</p> <p>Use mouse to rotate, scroll to zoom, right-click to pan</p> </div> <div id="controls"> <button id="changeBg">Change Background</button> <button id="toggleRotation">Toggle Rotation</button> </div> <script src="https://cdn.jsdelivr.net/npm/three@0.132.2/build/three.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/three@0.132.2/examples/js/controls/OrbitControls.js"></script> <script> // Scene setup const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); const renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(window.innerWidth, window.innerHeight); renderer.setPixelRatio(window.devicePixelRatio); document.body.appendChild(renderer.domElement); // Add lights const ambientLight = new THREE.AmbientLight(0xffffff, 0.5); scene.add(ambientLight); const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8); directionalLight.position.set(1, 1, 1); scene.add(directionalLight); // Create a cube const geometry = new THREE.BoxGeometry(2, 2, 2); const material = new THREE.MeshPhongMaterial({ color: 0x00ff00, shininess: 100 }); const cube = new THREE.Mesh(geometry, material); scene.add(cube); // Position camera camera.position.z = 5; // Add orbit controls const controls = new THREE.OrbitControls(camera, renderer.domElement); controls.enableDamping = true; controls.dampingFactor = 0.05; // Raycaster for mouse interaction const raycaster = new THREE.Raycaster(); const mouse = new THREE.Vector2(); // Handle window resize window.addEventListener('resize', () => { camera.aspect = window.innerWidth / window.innerHeight; camera.updateProjectionMatrix(); renderer.setSize(window.innerWidth, window.innerHeight); }); // Handle mouse click window.addEventListener('click', (event) => { // Calculate mouse position in normalized device coordinates mouse.x = (event.clientX / window.innerWidth) * 2 - 1; mouse.y = -(event.clientY / window.innerHeight) * 2 + 1; // Update the raycaster raycaster.setFromCamera(mouse, camera); // Check for intersections with the cube const intersects = raycaster.intersectObject(cube); if (intersects.length > 0) { // Change cube color to a random color cube.material.color.setHex(Math.random() * 0xffffff); } }); // UI Controls document.getElementById('changeBg').addEventListener('click', () => { scene.background = new THREE.Color(Math.random() * 0xffffff); }); let rotating = true; document.getElementById('toggleRotation').addEventListener('click', () => { rotating = !rotating; }); // Animation loop function animate() { requestAnimationFrame(animate); if (rotating) { cube.rotation.x += 0.01; cube.rotation.y += 0.01; } controls.update(); renderer.render(scene, camera); } animate(); </script> </body> </html>Features:
-
Interactive 3D Cube:
- Rotates automatically
- Changes color when clicked
- Phong material with lighting effects
-
Camera Controls:
- OrbitControls for intuitive navigation
- Left-click + drag to rotate view
- Right-click + drag to pan
- Scroll to zoom
-
UI Elements:
- Information panel explaining controls
- Buttons to change background color
- Button to toggle cube rotation
-
Responsive Design:
- Automatically adjusts to window size
- Handles high-DPI displays
-
All in One File:
- No external dependencies (CDN for Three.js)
- Complete solution in a single HTML file
To use this, simply copy the entire code into a file named
index.htmland open it in a modern web browser. No additional files or setup required!-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.