Skip to content
The Chat Archive
mistral-medium-2505· Mistral AI LMArena Human Preference 140K

create an interactive three js website. include everything in 1 single html file

  1. User

    create an interactive three js website. include everything in 1 single html file

  2. Assistant

    Interactive Three.js Website in a Single HTML File

    Here's a complete interactive Three.js website in a single HTML file. This example includes:

    • A 3D scene with a rotating cube
    • Interactive controls (orbit controls)
    • Responsive design
    • Basic lighting
    • A simple UI with buttons to change cube color
    <!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 Website</title>
        <style>
            body {
                margin: 0;
                padding: 0;
                overflow: hidden;
                font-family: Arial, sans-serif;
            }
            #container {
                position: relative;
                width: 100%;
                height: 100vh;
            }
            #info {
                position: absolute;
                top: 10px;
                left: 10px;
                color: white;
                background-color: rgba(0,0,0,0.5);
                padding: 10px;
                border-radius: 5px;
                z-index: 100;
            }
            #controls {
                position: absolute;
                bottom: 20px;
                left: 50%;
                transform: translateX(-50%);
                display: flex;
                gap: 10px;
                z-index: 100;
            }
            button {
                padding: 8px 15px;
                background-color: #4CAF50;
                color: white;
                border: none;
                border-radius: 4px;
                cursor: pointer;
                transition: background-color 0.3s;
            }
            button:hover {
                background-color: #45a049;
            }
        </style>
    </head>
    <body>
        <div id="container">
            <div id="info">
                <h2>Interactive Three.js Demo</h2>
                <p>Drag to rotate, scroll to zoom</p>
            </div>
            <div id="controls">
                <button id="redBtn">Red</button>
                <button id="blueBtn">Blue</button>
                <button id="greenBtn">Green</button>
                <button id="randomBtn">Random Color</button>
            </div>
        </div>
    
        <!-- Three.js library -->
        <script src="https://cdn.jsdelivr.net/npm/three@0.132.2/build/three.min.js"></script>
        <!-- OrbitControls -->
        <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();
            scene.background = new THREE.Color(0x111111);
    
            // Camera setup
            const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
            camera.position.z = 5;
    
            // Renderer setup
            const renderer = new THREE.WebGLRenderer({ antialias: true });
            renderer.setSize(window.innerWidth, window.innerHeight);
            renderer.shadowMap.enabled = true;
            document.getElementById('container').prepend(renderer.domElement);
    
            // Add orbit controls
            const controls = new THREE.OrbitControls(camera, renderer.domElement);
            controls.enableDamping = true;
            controls.dampingFactor = 0.05;
    
            // Lighting
            const ambientLight = new THREE.AmbientLight(0x404040);
            scene.add(ambientLight);
    
            const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
            directionalLight.position.set(1, 1, 1);
            directionalLight.castShadow = true;
            scene.add(directionalLight);
    
            // Create cube
            const geometry = new THREE.BoxGeometry(1, 1, 1);
            const material = new THREE.MeshPhongMaterial({ color: 0x00ff00 });
            const cube = new THREE.Mesh(geometry, material);
            cube.castShadow = true;
            scene.add(cube);
    
            // Add a plane for shadows
            const planeGeometry = new THREE.PlaneGeometry(10, 10);
            const planeMaterial = new THREE.MeshStandardMaterial({
                color: 0x333333,
                roughness: 0.8,
                metalness: 0.2
            });
            const plane = new THREE.Mesh(planeGeometry, planeMaterial);
            plane.rotation.x = -Math.PI / 2;
            plane.position.y = -1.5;
            plane.receiveShadow = true;
            scene.add(plane);
    
            // Animation loop
            function animate() {
                requestAnimationFrame(animate);
    
                // Rotate the cube
                cube.rotation.x += 0.01;
                cube.rotation.y += 0.01;
    
                controls.update();
                renderer.render(scene, camera);
            }
            animate();
    
            // Handle window resize
            window.addEventListener('resize', () => {
                camera.aspect = window.innerWidth / window.innerHeight;
                camera.updateProjectionMatrix();
                renderer.setSize(window.innerWidth, window.innerHeight);
            });
    
            // Button event listeners
            document.getElementById('redBtn').addEventListener('click', () => {
                cube.material.color.set(0xff0000);
            });
    
            document.getElementById('blueBtn').addEventListener('click', () => {
                cube.material.color.set(0x0000ff);
            });
    
            document.getElementById('greenBtn').addEventListener('click', () => {
                cube.material.color.set(0x00ff00);
            });
    
            document.getElementById('randomBtn').addEventListener('click', () => {
                const randomColor = Math.floor(Math.random() * 16777215);
                cube.material.color.set(randomColor);
            });
        </script>
    </body>
    </html>
    

    Features of this implementation:

    1. Complete in one file: All HTML, CSS, and JavaScript are contained in a single HTML file.

    2. Interactive elements:

      • Orbit controls for rotating/zooming the scene
      • Color change buttons for the cube
      • Responsive design that works on different screen sizes
    3. 3D elements:

      • A rotating cube with shadows
      • Ambient and directional lighting
      • A ground plane for context
    4. User interface:

      • Information panel
      • Control buttons
      • Clean styling

    To use this, simply copy the entire code into a single HTML file and open it in a modern browser. No additional files or dependencies are needed as it loads Three.js and OrbitControls from CDN.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.