← Mike Higgins Home
Custom Code Experiment •

Klein Bottle

Source Code View HTML / CSS / JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Klein Bottle</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            background: linear-gradient(135deg, #1a0033, #000011, #330011);
            overflow: hidden;
            font-family: 'Courier New', monospace;
            touch-action: manipulation;
        }

        #canvas {
            display: block;
            background: transparent;
            cursor: crosshair;
            image-rendering: pixelated;
        }

        #canvas:active {
            cursor: grabbing;
        }

        .control-panel {
            position: fixed;
            top: 10px;
            right: 10px;
            background: rgba(0, 0, 0, 0.95);
            border: 2px solid #ffffff;
            box-shadow: 0 0 10px rgba(255, 255, 255, 0.3);
            padding: 12px;
            color: #ffffff;
            font-size: 11px;
            width: 250px;
            z-index: 1000;
            text-transform: uppercase;
            letter-spacing: 1px;
            transition: transform 0.3s ease, opacity 0.3s ease;
            border-radius: 8px;
        }

        .menu-toggle {
            position: fixed;
            top: 10px;
            right: 10px;
            width: 50px;
            height: 50px;
            background: rgba(0, 0, 0, 0.95);
            border: 2px solid #ffffff;
            color: #ffffff;
            font-size: 24px;
            cursor: pointer;
            z-index: 1001;
            border-radius: 8px;
            display: none;
            justify-content: center;
            align-items: center;
            box-shadow: 0 0 10px rgba(255, 255, 255, 0.3);
        }

        .menu-toggle:hover {
            background: rgba(255, 255, 255, 0.1);
        }

        .menu-toggle:active {
            background: #ffffff;
            color: #000000;
        }

        .control-group {
            margin-bottom: 10px;
        }

        .control-group:last-child {
            margin-bottom: 0;
        }

        label {
            display: block;
            margin-bottom: 4px;
            font-weight: bold;
            color: #ffffff;
            text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8);
        }

        label::before {
            content: "▶";
            margin-right: 6px;
            color: #ffffff;
        }

        input[type="range"] {
            width: 100%;
            height: 8px;
            background: linear-gradient(90deg, #666666, #999999, #cccccc, #ffffff);
            outline: none;
            -webkit-appearance: none;
            appearance: none;
            border: 1px solid #ffffff;
            border-radius: 4px;
        }

        input[type="range"]::-webkit-slider-thumb {
            appearance: none;
            width: 16px;
            height: 16px;
            background: #ffffff;
            cursor: pointer;
            border: 2px solid #000000;
            border-radius: 50%;
        }

        input[type="range"]::-moz-range-thumb {
            width: 16px;
            height: 16px;
            background: #ffffff;
            cursor: pointer;
            border: 2px solid #000000;
            border-radius: 50%;
        }

        .button-group {
            display: flex;
            gap: 6px;
            flex-wrap: wrap;
        }

        button {
            flex: 1;
            min-width: 50px;
            padding: 6px 8px;
            border: 2px solid #ffffff;
            background: rgba(0, 0, 0, 0.8);
            color: #ffffff;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.2s ease;
            font-size: 9px;
            text-transform: uppercase;
            font-family: 'Courier New', monospace;
            border-radius: 4px;
        }

        button:hover {
            background: rgba(255, 255, 255, 0.2);
            box-shadow: 0 0 8px rgba(255, 255, 255, 0.5);
        }

        button:active {
            background: #ffffff;
            color: #000000;
        }

        .value-display {
            font-size: 9px;
            color: #ffffff;
            text-align: right;
            margin-top: 2px;
            font-family: 'Courier New', monospace;
            text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8);
        }

        .value-display::before {
            content: "[";
            color: #ffffff;
            margin-right: 2px;
        }

        .value-display::after {
            content: "]";
            color: #ffffff;
            margin-left: 2px;
        }

        @media (min-width: 769px) {
            .control-panel {
                display: block !important;
                opacity: 1 !important;
                visibility: visible !important;
                transform: none !important;
                pointer-events: all !important;
            }
            .menu-toggle {
                display: none !important;
            }
        }

        @media (max-width: 768px) {
            .menu-toggle {
                display: flex !important;
            }

            .control-panel {
                top: 70px;
                right: 10px;
                left: 10px;
                width: auto;
                max-width: none;
                padding: 15px;
                font-size: 10px;
                background: rgba(0, 0, 0, 0.98);
                border: 2px solid #ffffff;
                transform: translateY(-20px);
                opacity: 0;
                visibility: hidden;
                pointer-events: none;
                transition: all 0.3s ease;
                border-radius: 8px;
                max-height: calc(100vh - 90px);
                overflow-y: auto;
            }

            .control-panel.mobile-visible {
                transform: translateY(0);
                opacity: 1;
                visibility: visible;
                pointer-events: all;
            }

            button {
                font-size: 10px;
                padding: 8px 10px;
                min-height: 36px;
            }

            input[type="range"] {
                height: 10px;
            }

            input[type="range"]::-webkit-slider-thumb {
                width: 20px;
                height: 20px;
            }

            input[type="range"]::-moz-range-thumb {
                width: 20px;
                height: 20px;
            }

            .value-display {
                font-size: 10px;
            }

            #canvas {
                touch-action: pan-x pan-y;
            }
        }

        @media (max-width: 480px) {
            .control-panel {
                padding: 12px;
                top: 65px;
            }

            .menu-toggle {
                width: 45px;
                height: 45px;
                font-size: 20px;
            }

            button {
                font-size: 9px;
                padding: 6px 8px;
                min-height: 32px;
            }

            .button-group {
                flex-direction: column;
            }

            .button-group button {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <canvas id="canvas"></canvas>

    <div class="menu-toggle" id="menuToggle">☰</div>

    <div class="control-panel" id="controlPanel">
        <div class="control-group">
            <label>Rotation Speed</label>
            <input type="range" id="rotationSpeed" min="0.1" max="3" value="1" step="0.1">
            <div class="value-display" id="rotationValue">1.0</div>
        </div>

        <div class="control-group">
            <label>Sphere Detail</label>
            <input type="range" id="sphereDetail" min="4" max="12" value="6" step="1">
            <div class="value-display" id="sphereValue">6</div>
        </div>

        <div class="control-group">
            <label>Trail Length</label>
            <input type="range" id="trailLength" min="0.5" max="0.9" value="0.7" step="0.05">
            <div class="value-display" id="trailValue">0.70</div>
        </div>

        <div class="control-group">
            <div class="button-group">
                <button onclick="resetRotation()">Reset</button>
                <button onclick="toggleGrid()">Grid</button>
                <button onclick="togglePause()">Pause</button>
            </div>
        </div>
    </div>

    <script>
        const canvas = document.getElementById('canvas');
        const ctx = canvas.getContext('2d');
        const menuToggle = document.getElementById('menuToggle');
        const controlPanel = document.getElementById('controlPanel');

        let isMenuVisible = false;

        let isDragging = false;
        let lastTouchX = 0;
        let lastTouchY = 0;

        let animationId;
        let isPaused = false;
        let time = 0;

        let rotationSpeed = 1;
        let sphereDetail = 6;
        let trailLength = 0.7;
        let showGrid = true;
        let gridDensity = 80;

        let gridBursts = new Map();
        const burstDuration = 40;

        let rotXY = 0, rotXZ = 0, rotXW = 0, rotYZ = 0, rotYW = 0, rotZW = 0;

        let colorOffset = 0;
        const pixelColors = [
            '#ff0080', '#ff8000', '#ffff00', '#80ff00', '#00ff80',
            '#00ffff', '#0080ff', '#8000ff', '#ff00ff', '#ff4040',
            '#40ff40', '#4040ff'
        ];

        function toggleMobileMenu() {
            isMenuVisible = !isMenuVisible;
            if (isMenuVisible) {
                controlPanel.classList.add('mobile-visible');
                menuToggle.textContent = '✕';
                menuToggle.style.borderColor = '#ffffff';
                menuToggle.style.boxShadow = '0 0 10px rgba(255, 255, 255, 0.5)';
            } else {
                controlPanel.classList.remove('mobile-visible');
                menuToggle.textContent = '☰';
                menuToggle.style.borderColor = '#ffffff';
                menuToggle.style.boxShadow = '0 0 10px rgba(255, 255, 255, 0.3)';
            }
        }

        function handleClickOutside(event) {
            if (window.innerWidth <= 768 && isMenuVisible) {
                if (!controlPanel.contains(event.target) && !menuToggle.contains(event.target)) {
                    toggleMobileMenu();
                }
            }
        }

        function handleResize() {
            if (window.innerWidth > 768) {
                menuToggle.style.display = 'none';
                controlPanel.classList.add('mobile-visible');
                isMenuVisible = false;
            } else {
                menuToggle.style.display = 'flex';
                controlPanel.classList.remove('mobile-visible');
                isMenuVisible = false;
                menuToggle.textContent = '☰';
                menuToggle.style.borderColor = '#ffffff';
                menuToggle.style.boxShadow = '0 0 10px rgba(255, 255, 255, 0.3)';
            }
        }

        function initMobileMenu() {
            if (window.innerWidth <= 768) {
                menuToggle.style.display = 'flex';
                controlPanel.classList.remove('mobile-visible');
                isMenuVisible = false;
            } else {
                menuToggle.style.display = 'none';
                controlPanel.classList.add('mobile-visible');
                isMenuVisible = true;
            }
        }

        menuToggle.addEventListener('click', toggleMobileMenu);
        document.addEventListener('click', handleClickOutside);
        window.addEventListener('resize', handleResize);

        function resizeCanvas() {
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;

            ctx.imageSmoothingEnabled = false;
            ctx.webkitImageSmoothingEnabled = false;
            ctx.mozImageSmoothingEnabled = false;
            ctx.msImageSmoothingEnabled = false;
        }

        function point4D(x, y, z, w) {
            return { x, y, z, w };
        }

        function rotateXY(p, angle) {
            const cos = Math.cos(angle);
            const sin = Math.sin(angle);
            return point4D(
                cos * p.x - sin * p.y,
                sin * p.x + cos * p.y,
                p.z, p.w
            );
        }

        function rotateXZ(p, angle) {
            const cos = Math.cos(angle);
            const sin = Math.sin(angle);
            return point4D(
                cos * p.x - sin * p.z,
                p.y,
                sin * p.x + cos * p.z,
                p.w
            );
        }

        function rotateXW(p, angle) {
            const cos = Math.cos(angle);
            const sin = Math.sin(angle);
            return point4D(
                cos * p.x - sin * p.w,
                p.y, p.z,
                sin * p.x + cos * p.w
            );
        }

        function project4Dto3D(p) {
            const distance = 4;
            const w = 1 / (distance - p.w);
            return {
                x: p.x * w,
                y: p.y * w,
                z: p.z * w
            };
        }

        function project3Dto2D(p) {
            const distance = 0.5;
            const scale = Math.min(canvas.width, canvas.height) * 1.8;
            const z = 1 / (distance - p.z);
            return {
                x: canvas.width/2 + p.x * scale * z,
                y: canvas.height/2 + p.y * scale * z,
                depth: p.z
            };
        }

        function drawNonagon(x, y, size) {
            ctx.beginPath();
            for (let i = 0; i < 9; i++) {
                const angle = (i * 2 * Math.PI) / 9;
                const px = x + Math.cos(angle) * size;
                const py = y + Math.sin(angle) * size;
                if (i === 0) {
                    ctx.moveTo(px, py);
                } else {
                    ctx.lineTo(px, py);
                }
            }
            ctx.closePath();
        }

        function getNonagonGridPosition(row, col) {
            const nonWidth = gridDensity;
            const nonHeight = gridDensity * 0.866;
            const x = col * nonWidth * 0.8;
            const y = row * nonHeight + (col % 2) * nonHeight / 2;
            return { x, y };
        }

        function getNearestNonagonNode(x, y) {
            const nonWidth = gridDensity;
            const nonHeight = gridDensity * 0.866;

            const col = Math.round(x / (nonWidth * 0.8));
            const row = Math.round((y - (col % 2) * nonHeight / 2) / nonHeight);

            const nonPos = getNonagonGridPosition(row, col);

            let closestDist = Infinity;
            let closestPos = nonPos;

            for (let dr = -1; dr <= 1; dr++) {
                for (let dc = -1; dc <= 1; dc++) {
                    const testPos = getNonagonGridPosition(row + dr, col + dc);
                    const dist = Math.sqrt((x - testPos.x) ** 2 + (y - testPos.y) ** 2);
                    if (dist < closestDist) {
                        closestDist = dist;
                        closestPos = testPos;
                    }
                }
            }

            return { pos: closestPos, distance: closestDist };
        }

        function checkNodeCollision(x, y) {
            const nonResult = getNearestNonagonNode(x, y);
            const distance = nonResult.distance;

            if (distance < gridDensity * 0.5) {
                const nodeKey = `${nonResult.pos.x.toFixed(0)},${nonResult.pos.y.toFixed(0)}`;
                if (!gridBursts.has(nodeKey)) {
                    gridBursts.set(nodeKey, {
                        x: nonResult.pos.x,
                        y: nonResult.pos.y,
                        frame: 0,
                        intensity: 1,
                        color: getPixelColor(Math.random() * pixelColors.length)
                    });
                }
            }
        }

        function updateBursts() {
            for (let [key, burst] of gridBursts) {
                burst.frame++;
                burst.intensity = Math.max(0, 1 - (burst.frame / burstDuration));

                if (burst.frame >= burstDuration) {
                    gridBursts.delete(key);
                }
            }
        }

        function drawBursts() {
            for (let [key, burst] of gridBursts) {
                const size = 10 + (1 - burst.intensity) * 20;
                const alpha = burst.intensity;

                ctx.strokeStyle = `${burst.color}${Math.floor(alpha * 255).toString(16).padStart(2, '0')}`;
                ctx.lineWidth = 3;
                drawNonagon(burst.x, burst.y, size);
                ctx.stroke();
                ctx.fillStyle = `#ffffff${Math.floor(alpha * 255).toString(16).padStart(2, '0')}`;
                ctx.fillRect(burst.x - 2, burst.y - 2, 4, 4);
            }
        }

        function getPixelColor(index) {
            const colorIndex = Math.floor((index + colorOffset) % pixelColors.length);
            return pixelColors[colorIndex];
        }

        function createKleinBottlePoints() {
            const points = [];
            const a = 2;

            for (let i = 0; i < sphereDetail; i++) {
                for (let j = 0; j < sphereDetail * 2; j++) {
                    const u = (i / sphereDetail) * Math.PI * 2;
                    const v = (j / (sphereDetail * 2)) * Math.PI * 2;

                    let x, y, z, w;

                    if (u < Math.PI) {
                        x = (a + Math.cos(u/2) * Math.sin(v) - Math.sin(u/2) * Math.sin(2*v)) * Math.cos(u);
                        y = (a + Math.cos(u/2) * Math.sin(v) - Math.sin(u/2) * Math.sin(2*v)) * Math.sin(u);
                        z = Math.sin(u/2) * Math.sin(v) + Math.cos(u/2) * Math.sin(2*v);
                        w = Math.cos(v) * 0.5;
                    } else {
                        x = (a + Math.cos(u/2) * Math.sin(v) + Math.sin(u/2) * Math.sin(2*v)) * Math.cos(u);
                        y = (a + Math.cos(u/2) * Math.sin(v) + Math.sin(u/2) * Math.sin(2*v)) * Math.sin(u);
                        z = Math.sin(u/2) * Math.sin(v) - Math.cos(u/2) * Math.sin(2*v);
                        w = Math.cos(v) * 0.5;
                    }

                    points.push(point4D(x * 0.15, y * 0.15, z * 0.15, w * 0.15));
                }
            }

            return points;
        }

        function drawGrid() {
            if (!showGrid) return;

            const nonSize = gridDensity * 0.2;
            const nonWidth = gridDensity;
            const nonHeight = gridDensity * 0.866;

            ctx.strokeStyle = '#330066';
            ctx.lineWidth = 1;

            const cols = Math.ceil(canvas.width / (nonWidth * 0.8)) + 2;
            const rows = Math.ceil(canvas.height / nonHeight) + 2;

            for (let row = -1; row < rows; row++) {
                for (let col = -1; col < cols; col++) {
                    const pos = getNonagonGridPosition(row, col);
                    if (pos.x > -nonWidth && pos.x < canvas.width + nonWidth &&
                        pos.y > -nonHeight && pos.y < canvas.height + nonHeight) {

                        drawNonagon(pos.x, pos.y, nonSize);
                        ctx.stroke();
                    }
                }
            }

            const nodeSize = 3;
            let nodeIndex = 0;

            for (let row = -1; row < rows; row++) {
                for (let col = -1; col < cols; col++) {
                    const pos = getNonagonGridPosition(row, col);
                    if (pos.x > -nonWidth && pos.x < canvas.width + nonWidth &&
                        pos.y > -nonHeight && pos.y < canvas.height + nonHeight) {

                        const color = getPixelColor(nodeIndex + time);
                        ctx.fillStyle = color;
                        ctx.fillRect(pos.x - nodeSize/2, pos.y - nodeSize/2, nodeSize, nodeSize);

                        nodeIndex++;
                    }
                }
            }
        }

        function animate() {
            if (isPaused) {
                animationId = requestAnimationFrame(animate);
                return;
            }

            time += 0.02 * rotationSpeed;
            colorOffset += 0.1 * rotationSpeed;

            rotXY += 0.006 * rotationSpeed;
            rotXZ += 0.008 * rotationSpeed;
            rotXW += 0.004 * rotationSpeed;
            rotYZ += 0.007 * rotationSpeed;
            rotYW += 0.005 * rotationSpeed;
            rotZW += 0.009 * rotationSpeed;

            ctx.fillStyle = `rgba(26, 0, 51, ${1 - trailLength})`;
            ctx.fillRect(0, 0, canvas.width, canvas.height);

            updateBursts();
            drawGrid();
            drawBursts();

            const kleinPoints = createKleinBottlePoints();
            const transformedPoints = kleinPoints.map((p, index) => {
                let point = p;

                point = rotateXY(point, rotXY);
                point = rotateXZ(point, rotXZ);
                point = rotateXW(point, rotXW);

                const p3d = project4Dto3D(point);
                const p2d = project3Dto2D(p3d);

                return { ...p2d, originalIndex: index };
            });

            transformedPoints.sort((a, b) => a.depth - b.depth);

            ctx.strokeStyle = 'rgba(255, 255, 255, 0.3)';
            ctx.lineWidth = 1;

            for (let i = 0; i < transformedPoints.length; i++) {
                const nextI = (i + 1) % transformedPoints.length;
                const nextRow = (i + sphereDetail * 2) % transformedPoints.length;

                if (i % (sphereDetail * 2) !== (sphereDetail * 2 - 1)) {
                    const p1 = transformedPoints[i];
                    const p2 = transformedPoints[nextI];

                    const distance = Math.sqrt((p1.x - p2.x) ** 2 + (p1.y - p2.y) ** 2);
                    if (distance < 150) {
                        const alpha = Math.max(0, 1 - distance / 150) * 0.25;
                        const color = getPixelColor(i * 0.1);
                        ctx.strokeStyle = `${color}${Math.floor(alpha * 255).toString(16).padStart(2, '0')}`;

                        ctx.beginPath();
                        ctx.moveTo(p1.x, p1.y);
                        ctx.lineTo(p2.x, p2.y);
                        ctx.stroke();
                    }
                }

                if (i < transformedPoints.length - sphereDetail * 2) {
                    const p1 = transformedPoints[i];
                    const p2 = transformedPoints[nextRow];

                    const distance = Math.sqrt((p1.x - p2.x) ** 2 + (p1.y - p2.y) ** 2);
                    if (distance < 150) {
                        const alpha = Math.max(0, 1 - distance / 150) * 0.25;
                        const color = getPixelColor(i * 0.1 + 3);
                        ctx.strokeStyle = `${color}${Math.floor(alpha * 255).toString(16).padStart(2, '0')}`;

                        ctx.beginPath();
                        ctx.moveTo(p1.x, p1.y);
                        ctx.lineTo(p2.x, p2.y);
                        ctx.stroke();
                    }
                }
            }

            transformedPoints.forEach(point => {
                checkNodeCollision(point.x, point.y);

                const size = 4 + (1 - point.depth) * 2;
                const color = getPixelColor(point.originalIndex + time * 2);

                ctx.fillStyle = color;
                ctx.fillRect(point.x - size/2, point.y - size/2, size, size);

                ctx.fillStyle = '#ffffff';
                ctx.fillRect(point.x - size/2, point.y - size/2, 1, 1);
            });

            animationId = requestAnimationFrame(animate);
        }

        document.getElementById('rotationSpeed').addEventListener('input', (e) => {
            rotationSpeed = parseFloat(e.target.value);
            document.getElementById('rotationValue').textContent = rotationSpeed.toFixed(1);
        });

        document.getElementById('sphereDetail').addEventListener('input', (e) => {
            sphereDetail = parseInt(e.target.value);
            document.getElementById('sphereValue').textContent = sphereDetail;
        });

        document.getElementById('trailLength').addEventListener('input', (e) => {
            trailLength = parseFloat(e.target.value);
            document.getElementById('trailValue').textContent = trailLength.toFixed(2);
        });

        function resetRotation() {
            rotXY = rotXZ = rotXW = rotYZ = rotYW = rotZW = 0;
            time = 0;
        }

        function toggleGrid() {
            showGrid = !showGrid;
            const gridButton = document.querySelector('button[onclick="toggleGrid()"]');
            gridButton.textContent = showGrid ? 'Grid On' : 'Grid Off';
            gridButton.style.borderColor = showGrid ? '#ffffff' : '#666666';
        }

        function togglePause() {
            isPaused = !isPaused;
            const pauseButton = document.querySelector('button[onclick="togglePause()"]');
            pauseButton.textContent = isPaused ? 'Play' : 'Pause';
            pauseButton.style.borderColor = isPaused ? '#666666' : '#ffffff';
        }

        canvas.addEventListener('touchstart', (e) => {
            e.preventDefault();
            isDragging = true;
            const touch = e.touches[0];
            lastTouchX = touch.clientX;
            lastTouchY = touch.clientY;

            if (isMenuVisible && window.innerWidth <= 768) {
                toggleMobileMenu();
            }
        });

        canvas.addEventListener('touchmove', (e) => {
            e.preventDefault();
            if (!isDragging) return;

            const touch = e.touches[0];
            const deltaX = (touch.clientX - lastTouchX) * 0.01;
            const deltaY = (touch.clientY - lastTouchY) * 0.01;

            rotXY += deltaX;
            rotXZ += deltaY;

            lastTouchX = touch.clientX;
            lastTouchY = touch.clientY;
        });

        canvas.addEventListener('touchend', (e) => {
            e.preventDefault();
            isDragging = false;
        });

        canvas.addEventListener('mousedown', (e) => {
            isDragging = true;
            lastTouchX = e.clientX;
            lastTouchY = e.clientY;
        });

        canvas.addEventListener('mousemove', (e) => {
            if (!isDragging) return;

            const deltaX = (e.clientX - lastTouchX) * 0.01;
            const deltaY = (e.clientY - lastTouchY) * 0.01;

            rotXY += deltaX;
            rotXZ += deltaY;

            lastTouchX = e.clientX;
            lastTouchY = e.clientY;
        });

        canvas.addEventListener('mouseup', () => {
            isDragging = false;
        });

        canvas.addEventListener('click', (e) => {
            const rect = canvas.getBoundingClientRect();
            const x = e.clientX - rect.left;
            const y = e.clientY - rect.top;

            const nodeKey = `${x.toFixed(0)},${y.toFixed(0)}`;
            if (!gridBursts.has(nodeKey)) {
                gridBursts.set(nodeKey, {
                    x: x,
                    y: y,
                    frame: 0,
                    intensity: 1,
                    color: getPixelColor(Math.random() * pixelColors.length)
                });
            }
        });

        window.addEventListener('resize', () => {
            resizeCanvas();
            handleResize();
        });

        resizeCanvas();
        initMobileMenu();
        animate();
    </script>
</body>
</html>