Skip to content
TheChatArchiveThe Chat Archive
Geminigemini-2.5-flash-lite-preview-06-17-thinking· GoogleHugging Face LMArena Human Preference 140K

Create a JavaScript simulation of a rotating 3D sphere made up of alphabets. The closest…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with DeepSeekdeepseek-v3-0324’s answer.
  1. User

    Create a JavaScript simulation of a rotating 3D sphere made up of alphabets. The closest letters should be brighter, while the ones farthest away should appear grey.

  2. Gemini Assistant

    Okay, let's create a JavaScript simulation for a 3D rotating alphabet sphere using the HTML Canvas API.

    This simulation will:

    1. Generate points: Distribute letters randomly on the surface of a sphere using spherical coordinates.
    2. Rotate: Apply 3D rotation matrices to these points.
    3. Project: Use perspective projection to map the 3D points onto the 2D canvas.
    4. Color: Determine the brightness of each letter based on its Z-depth (distance from the viewer). Closer letters will be brighter, farther ones grey.
    5. Animate: Use requestAnimationFrame for smooth animation.

    1. index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>3D Alphabet Sphere</title>
        <style>
            body {
                margin: 0;
                overflow: hidden; /* Hide scrollbars */
                background-color: #000; /* Black background */
                display: flex;
                justify-content: center;
                align-items: center;
                min-height: 100vh;
                cursor: grab; /* Indicate interactivity */
            }
            canvas {
                display: block; /* Remove extra space below canvas */
                border: 1px solid #333; /* Optional border */
            }
        </style>
    </head>
    <body>
        <canvas id="sphereCanvas"></canvas>
        <script src="script.js"></script>
    </body>
    </html>
    

    2. script.js

    const canvas = document.getElementById('sphereCanvas');
    const ctx = canvas.getContext('2d');
    
    // --- Configuration ---
    const NUM_POINTS = 600; // Number of letters to display
    const SPHERE_RADIUS = 150; // Radius of the sphere
    const FOCAL_LENGTH = 300; // Affects perspective (higher = less perspective distortion)
    const ROTATION_SPEED_X = 0.005; // Speed of rotation around X-axis
    const ROTATION_SPEED_Y = 0.005; // Speed of rotation around Y-axis
    const LETTER_FONT = '16px Arial'; // Font for the letters
    const MIN_BRIGHTNESS = 80; // Minimum brightness (0-255) for farthest letters (grey)
    const MAX_BRIGHTNESS = 255; // Maximum brightness (0-255) for closest letters (white)
    
    const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+';
    let points = []; // Array to hold our 3D points (letters)
    
    let rotationX = 0;
    let rotationY = 0;
    
    let isDragging = false;
    let previousMousePosition = { x: 0, y: 0 };
    
    // --- Helper Functions ---
    
    // Resize canvas to fill window
    function resizeCanvas() {
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
    }
    
    // Initialize sphere points using a uniform distribution method
    function initSphere() {
        points = [];
        const radius = SPHERE_RADIUS;
        const n = NUM_POINTS;
    
        // Use golden ratio spiral distribution for more uniform point placement on sphere surface
        for (let i = 0; i < n; i++) {
            // Spherical coordinates
            const theta = Math.acos(-1 + (2 * i) / n); // Polar angle (inclination)
            const phi = Math.PI * (1 + Math.sqrt(5)) * i; // Azimuthal angle (longitude)
    
            const x = radius * Math.sin(theta) * Math.cos(phi);
            const y = radius * Math.sin(theta) * Math.sin(phi);
            const z = radius * Math.cos(theta);
    
            points.push({
                x: x,
                y: y,
                z: z,
                char: alphabet[i % alphabet.length] // Cycle through alphabet characters
            });
        }
    }
    
    // Rotate a 3D point around the X and Y axes
    function rotatePoint(point, angleX, angleY) {
        const cosX = Math.cos(angleX);
        const sinX = Math.sin(angleX);
        const cosY = Math.cos(angleY);
        const sinY = Math.sin(angleY);
    
        let { x, y, z } = point;
    
        // Rotate around Y axis first
        const rotatedY_x = x * cosY - z * sinY;
        const rotatedY_z = x * sinY + z * cosY;
        x = rotatedY_x;
        z = rotatedY_z;
    
        // Rotate around X axis
        const rotatedX_y = y * cosX - z * sinX;
        const rotatedX_z = y * sinX + z * cosX;
        y = rotatedX_y;
        z = rotatedX_z;
    
        // Update point's coordinates
        point.x = x;
        point.y = y;
        point.z = z;
    }
    
    // Project a 3D point onto the 2D canvas plane
    function projectPoint(point) {
        const centerX = canvas.width / 2;
        const centerY = canvas.height / 2;
    
        // Perspective projection formula
        // Ensure point.z + FOCAL_LENGTH is positive to avoid division by zero or negative numbers
        // Also means points behind the viewer are not drawn
        const projectionFactor = FOCAL_LENGTH / (FOCAL_LENGTH + point.z);
    
        const screenX = centerX + point.x * projectionFactor;
        const screenY = centerY + point.y * projectionFactor;
    
        return {
            x: screenX,
            y: screenY,
            depth: point.z // The actual Z coordinate after rotation, used for coloring
        };
    }
    
    // Calculate color based on Z-depth
    function getColor(z) {
        // Map Z from [-SPHERE_RADIUS, SPHERE_RADIUS] to a brightness level [0, 255]
        // The farthest points (lowest Z) should be grey (e.g., MIN_BRIGHTNESS)
        // The closest points (highest Z) should be brightest (e.g., MAX_BRIGHTNESS)
    
        const range = 2 * SPHERE_RADIUS;
        // Normalize Z to a 0-1 range, where 0 is farthest (-R) and 1 is closest (+R)
        const normalizedZ = (z + SPHERE_RADIUS) / range;
    
        // Interpolate between MIN_BRIGHTNESS and MAX_BRIGHTNESS
        const brightness = Math.round(MIN_BRIGHTNESS + normalizedZ * (MAX_BRIGHTNESS - MIN_BRIGHTNESS));
    
        // Clamp brightness to ensure it's within the valid 0-255 range
        const clampedBrightness = Math.max(0, Math.min(255, brightness));
    
        // Return grayscale RGB color
        return `rgb(${clampedBrightness}, ${clampedBrightness}, ${clampedBrightness})`;
    }
    
    // --- Animation Loop ---
    function animate() {
        // Clear the canvas with black background
        ctx.fillStyle = '#000';
        ctx.fillRect(0, 0, canvas.width, canvas.height);
    
        // Update rotation angles based on manual drag or automatic speed
        rotationX += ROTATION_SPEED_X;
        rotationY += ROTATION_SPEED_Y;
    
        // Set drawing properties
        ctx.font = LETTER_FONT;
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';
    
        // Draw each point
        for (const point of points) {
            // Rotate the point in 3D space
            rotatePoint(point, rotationX, rotationY);
    
            // Project the 3D point to 2D screen coordinates
            const projected = projectPoint(point);
    
            // Only draw if the point is in front of the viewer (projectionFactor denominator > 0)
            if (projected.depth + FOCAL_LENGTH > 0) {
                // Get the color based on the point's depth (original Z after rotation)
                const color = getColor(projected.depth);
                ctx.fillStyle = color;
    
                // Draw the letter
                ctx.fillText(point.char, projected.x, projected.y);
            }
        }
    
        // Request the next frame for animation
        requestAnimationFrame(animate);
    }
    
    // --- Event Listeners ---
    
    // Handle window resizing
    window.addEventListener('resize', resizeCanvas);
    
    // Handle mouse down event for dragging
    canvas.addEventListener('mousedown', (e) => {
        isDragging = true;
        previousMousePosition = { x: e.clientX, y: e.clientY };
        canvas.style.cursor = 'grabbing'; // Change cursor to indicate grabbing
    });
    
    // Handle mouse move event for dragging
    canvas.addEventListener('mousemove', (e) => {
        if (!isDragging) return;
    
        const deltaX = e.clientX - previousMousePosition.x;
        const deltaY = e.clientY - previousMousePosition.y;
    
        // Adjust rotation based on mouse movement
        // Sensitivity might need tweaking based on screen size and preferences
        rotationY += deltaX * 0.005; // Rotate around Y axis based on horizontal mouse movement
        rotationX -= deltaY * 0.005; // Rotate around X axis based on vertical mouse movement
    
        previousMousePosition = { x: e.clientX, y: e.clientY };
    });
    
    // Handle mouse up event to stop dragging
    canvas.addEventListener('mouseup', () => {
        isDragging = false;
        canvas.style.cursor = 'grab'; // Revert cursor
    });
    
    // Handle mouse leaving the canvas to stop dragging
    canvas.addEventListener('mouseleave', () => {
        isDragging = false;
        canvas.style.cursor = 'grab'; // Revert cursor
    });
    
    
    // --- Initialization ---
    resizeCanvas(); // Set initial canvas size
    initSphere();   // Populate the sphere with letters
    animate();      // Start the animation loop
    

    How to Use:

    1. Save the HTML code as index.html.
    2. Save the JavaScript code as script.js in the same directory.
    3. Open index.html in your web browser.

    You should see a sphere made of letters rotating. You can also click and drag your mouse on the canvas to manually rotate the sphere. Closer letters will appear brighter white, fading to grey as they move further away.

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