Back to the app
The code
Build a Cell, Part by Part by GPT-6.1 Terra
Exactly as the AI wrote it. Shown as text, so nothing runs on this page.
index.html
works · auto-checked (Checked by an automated pass. Not hand-played yet.)<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<meta name="theme-color" content="#eff4ed">
<link rel="icon" href="data:,">
<title>Little Life Lab — Animal Cell</title>
<style>
:root{--ink:#193d37;--muted:#7c9185;--line:#e6eee6;--progress:0%}*{box-sizing:border-box}html{background:#eff4ed;min-height:100%}body{margin:0;color:var(--ink);font-family:ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;-webkit-font-smoothing:antialiased}button,input{font:inherit}button{cursor:pointer}button:focus-visible,input:focus-visible{outline:3px solid #a8ca70;outline-offset:3px}.app{max-width:1700px;margin:auto;padding:0 24px 24px}.header{height:85px;display:flex;align-items:center;justify-content:space-between;gap:16px}.brand{display:flex;align-items:center;gap:11px}.brand-mark{width:38px;height:38px;display:grid;place-items:center;background:#1b413b;border-radius:13px;position:relative;box-shadow:0 3px 8px #1739361a}.brand-mark:before{content:"";width:22px;height:22px;border:1.5px solid #bfe8bc;border-radius:48% 52% 54% 46%;transform:rotate(-22deg)}.brand-mark:after{content:"";position:absolute;width:7px;height:7px;border-radius:50%;background:#dbefb2;box-shadow:6px -6px 0 -2px #b9e0cd,-7px 5px 0 -2px #b9e0cd}.brand-name{font-size:14px;font-weight:800;letter-spacing:-.035em}.brand-sub{font-size:10px;letter-spacing:.12em;text-transform:uppercase;color:#8ba098;margin-top:2px;font-weight:700}.header-right{display:flex;align-items:center;gap:12px;color:#a7b9ac;font-size:10px;font-weight:800;letter-spacing:.12em;text-transform:uppercase}.header-pill{border:1px solid #d9e5dc;border-radius:100px;padding:9px 12px;color:#6e8979;white-space:nowrap}.layout{display:grid;grid-template-columns:355px minmax(0,1fr);gap:16px;align-items:stretch}.sidebar{background:#fff;border:1px solid #e4ede5;border-radius:25px;padding:27px 25px 22px;min-width:0;box-shadow:0 14px 45px rgba(39,78,58,.04)}.eyebrow,.section-label{font-size:10px;font-weight:800;letter-spacing:.15em;text-transform:uppercase;color:#8ba096}.eyebrow{display:flex;align-items:center;gap:7px}.eyebrow .spark{color:#78ac8c;font-size:15px;line-height:1}h1{font-family:Georgia,"Times New Roman",serif;font-size:clamp(38px,3vw,48px);line-height:1.02;font-weight:400;letter-spacing:-.052em;margin:15px 0 12px}h1 em{font-weight:400;color:#638e79}.intro{font-size:13px;line-height:1.6;color:#7b8e84;max-width:280px;margin:0 0 25px}.section-label{display:flex;align-items:center;justify-content:space-between;gap:8px;margin-bottom:10px}.section-label .number{font-size:10px;color:#afbeb4;letter-spacing:.1em}.detail{border:1px solid #e7eee7;background:#f7faf5;border-radius:16px;padding:15px;min-height:132px}.detail-top{display:flex;align-items:center;gap:9px;margin-bottom:10px}.detail-dot{width:26px;height:26px;border-radius:9px;background:#a5dcc8;box-shadow:inset 0 0 0 5px #ffffff66;flex:none}.detail-kicker{font-size:9px;letter-spacing:.13em;color:#91a399;font-weight:800;text-transform:uppercase}.detail h2{font-size:17px;letter-spacing:-.035em;line-height:1.15;margin:0 0 6px}.detail p{font-size:12px;line-height:1.5;color:#788c81;margin:0}.build{margin-top:24px}.build-header{display:flex;align-items:center;justify-content:space-between;gap:10px}.build-header .section-label{margin:0}.play{border:0;background:#eef5ed;color:#476e59;border-radius:100px;padding:8px 11px;min-height:36px;font-size:11px;font-weight:800;display:flex;align-items:center;gap:6px}.play span:first-child{font-size:10px}.slider-row{display:flex;align-items:center;gap:11px;margin:12px 1px 4px}.slider-row span{color:#9aac9f;font-size:10px;font-weight:800;font-variant-numeric:tabular-nums}.slider{appearance:none;-webkit-appearance:none;width:100%;height:5px;border-radius:20px;background:linear-gradient(to right,#83b894 0 var(--progress),#e4ece5 var(--progress) 100%);margin:0;cursor:pointer;touch-action:pan-y}.slider::-webkit-slider-thumb{-webkit-appearance:none;width:19px;height:19px;border-radius:50%;background:#fff;border:5px solid #6ca585;box-shadow:0 1px 4px #315c4433}.slider::-moz-range-thumb{width:10px;height:10px;border-radius:50%;background:#fff;border:5px solid #6ca585;box-shadow:0 1px 4px #315c4433}.build-note{font-size:10px;color:#a3b2a8;margin:12px 0 0;line-height:1.4}.parts-section{margin-top:25px}.parts-grid{display:grid;grid-template-columns:1fr 1fr;gap:6px}.part-button{border:1px solid #edf2ed;background:#fff;color:#526e5e;border-radius:10px;min-height:36px;text-align:left;padding:7px 8px;display:flex;align-items:center;gap:7px;font-size:10px;font-weight:700;line-height:1.2;transition:background .2s,border-color .2s,opacity .2s}.part-button:disabled{opacity:.37;cursor:default}.part-button:not(:disabled):hover,.part-button.active{background:#f2f8ee;border-color:#cfe2ce;color:#294d3b}.part-dot{width:9px;height:9px;border-radius:50%;background:var(--part-color);flex:none}.sidebar-foot{border-top:1px solid #edf2ed;margin-top:22px;padding-top:15px;display:flex;align-items:center;justify-content:space-between;color:#a4b5a8;font-size:10px}.sidebar-foot strong{font-weight:700;color:#88a28f}.stage{position:relative;overflow:hidden;min-height:650px;height:calc(100svh - 109px);max-height:1000px;border-radius:25px;background:radial-gradient(ellipse at 51% 43%,#285f64 0%,#173f45 37%,#102e35 72%,#0c252d 100%);box-shadow:0 16px 40px #254c3b12;isolation:isolate}.stage:before{content:"";position:absolute;inset:0;background-image:radial-gradient(#c2e8d6 1px,transparent 1px),radial-gradient(#b9e8df 1px,transparent 1px);background-size:113px 113px,173px 173px;background-position:24px 38px,76px 92px;opacity:.12;pointer-events:none}.stage:after{content:"";position:absolute;inset:0;border:1px solid #ffffff18;border-radius:25px;pointer-events:none}canvas{display:block;width:100%;height:100%;touch-action:none;cursor:grab;position:relative}canvas:active{cursor:grabbing}.stage-top{position:absolute;top:23px;left:24px;right:24px;display:flex;justify-content:space-between;align-items:flex-start;gap:12px;pointer-events:none}.stage-heading{color:#d5ebe2;font-size:10px;letter-spacing:.15em;font-weight:800;text-transform:uppercase}.stage-heading small{display:block;color:#91bab0;font-size:10px;letter-spacing:.03em;text-transform:none;font-weight:500;margin-top:7px}.live-dot{display:inline-block;width:7px;height:7px;background:#bbebbc;border-radius:50%;box-shadow:0 0 12px #b5f4b3;margin-right:6px}.stage-top-right{display:flex;gap:7px;align-items:center;pointer-events:auto}.glass-btn,.demo-badge{border:1px solid #ffffff25;background:#ffffff10;color:#d8eee5;border-radius:100px;min-height:39px;padding:0 14px;font-size:10px;font-weight:800;letter-spacing:.025em;backdrop-filter:blur(8px)}.glass-btn:hover{background:#ffffff20}.demo-badge{display:flex;align-items:center;color:#d8efbf}.demo-badge[hidden]{display:none}.point-label{position:absolute;z-index:2;pointer-events:none;display:none;align-items:center;gap:7px;padding:8px 11px;border:1px solid #ffffff50;background:#173d3be8;color:#f2fbf1;border-radius:8px;font-size:10px;font-weight:800;white-space:nowrap;box-shadow:0 5px 20px #071b2155;transform:translate(0,-50%)}.point-label:before{content:"";width:7px;height:7px;flex:none;border-radius:50%;background:var(--label-color,#d4f0b6);box-shadow:0 0 9px var(--label-color,#d4f0b6)}.stage-bottom{position:absolute;left:24px;right:24px;bottom:24px;display:flex;align-items:flex-end;justify-content:space-between;gap:14px;pointer-events:none}.stage-help{color:#d9eee4;max-width:265px}.stage-help strong{font-size:12px;display:block;letter-spacing:-.01em}.stage-help span{font-size:11px;line-height:1.5;color:#9dbfb3;display:block;margin-top:5px}.stage-controls{display:flex;align-items:center;gap:8px;pointer-events:auto}.control{min-height:48px;border:1px solid #ffffff30;border-radius:12px;background:#163b3ee8;color:#e7f5ec;padding:0 14px;font-weight:750;font-size:11px;display:flex;align-items:center;justify-content:center;gap:10px;white-space:nowrap;backdrop-filter:blur(12px)}.control:hover{background:#285350}.control.rebuild{background:#d9edac;border-color:#d9edac;color:#244332}.rebuild-icon{font-size:19px;font-weight:400;line-height:1}.switch{width:32px;height:18px;border-radius:100px;background:#77938b;position:relative;transition:background .2s}.switch:after{content:"";position:absolute;left:3px;top:3px;width:12px;height:12px;border-radius:50%;background:white;transition:transform .2s}.control.on .switch{background:#a6d885}.control.on .switch:after{transform:translateX(14px)}.load-error{position:absolute;inset:0;display:flex;align-items:center;justify-content:center;text-align:center;padding:30px;background:#103037ed;color:#eaf8ef;z-index:5}.load-error[hidden]{display:none}.error-card{max-width:320px;line-height:1.6}.error-card strong{display:block;font-family:Georgia,serif;font-weight:400;font-size:26px;margin-bottom:8px}.error-card span{font-size:13px;color:#b7d5c8}@media(max-width:900px){.app{padding:0 16px 16px}.header{height:72px}.layout{grid-template-columns:300px minmax(0,1fr);gap:12px}.sidebar{padding:24px 20px}.stage{height:calc(100svh - 88px);min-height:620px}.stage-bottom{left:18px;right:18px;bottom:18px;flex-wrap:wrap}.stage-top{left:18px;right:18px;top:18px}.stage-help{max-width:none;width:100%}.stage-controls{margin-left:auto}.header-number{display:none}}@media(max-width:680px){.app{padding:0 12px 18px}.header{height:64px}.brand-mark{width:35px;height:35px}.header-pill{padding:8px 10px;font-size:9px}.layout{display:flex;flex-direction:column;gap:12px}.stage{order:-1;height:min(70svh,570px);min-height:430px;border-radius:20px}.stage:after{border-radius:20px}.sidebar{border-radius:20px;padding:23px 20px}.sidebar h1{font-size:38px;margin-top:12px}.intro{max-width:100%;margin-bottom:20px}.detail{min-height:116px}.parts-grid{grid-template-columns:1fr 1fr 1fr}.part-button{min-height:40px}.stage-bottom{gap:9px}.stage-help strong{font-size:11px}.stage-help span{font-size:10px;margin-top:3px}.stage-controls{margin-left:0;width:100%}.control{flex:1;min-height:45px}.glass-btn,.demo-badge{min-height:37px;padding:0 11px}.point-label{font-size:10px}}@media(max-width:390px){.parts-grid{grid-template-columns:1fr 1fr}.header-pill{font-size:8px}.stage{min-height:410px}.stage-top{left:15px;right:15px}.stage-bottom{left:15px;right:15px;bottom:15px}}@media(prefers-reduced-motion:reduce){*{scroll-behavior:auto!important;transition:none!important}}
</style>
</head>
<body>
<div class="app">
<header class="header">
<div class="brand"><span class="brand-mark" aria-hidden="true"></span><div><div class="brand-name">little life lab</div><div class="brand-sub">A world worth exploring</div></div></div>
<div class="header-right"><span class="header-number">Field guide / 01</span><span class="header-pill">The animal cell</span></div>
</header>
<main class="layout">
<aside class="sidebar">
<div class="eyebrow"><span class="spark">✳</span> A closer look at life</div>
<h1>Meet your<br><em>animal cell.</em></h1>
<p class="intro">A tiny world with a big job. Build it piece by piece, then take a look around.</p>
<div class="section-label"><span>Your field guide</span><span class="number" id="detail-count">00 / 11</span></div>
<div class="detail" aria-live="polite" aria-atomic="true">
<div class="detail-top"><span class="detail-dot" id="detail-dot"></span><span class="detail-kicker" id="detail-kicker">The starting point</span></div>
<h2 id="part-name">Cell membrane</h2>
<p id="part-description">A soft, flexible skin holds the cell together and controls what goes in and out.</p>
</div>
<div class="build">
<div class="build-header"><div class="section-label">Build timeline</div><button class="play" id="play" type="button"><span id="play-symbol">Ⅱ</span><span id="play-text">Pause</span></button></div>
<div class="slider-row"><span>00</span><input class="slider" id="timeline" type="range" min="0" max="11" step="1" value="0" aria-label="Scrub through the cell build"><span>11</span></div>
<p class="build-note">Slide to travel back and forth through the build.</p>
</div>
<section class="parts-section" aria-label="Cell parts">
<div class="section-label"><span>The parts</span><span class="number">Tap to discover</span></div>
<div class="parts-grid" id="parts-grid"></div>
</section>
<div class="sidebar-foot"><strong>Small things. Big discoveries.</strong><span>✳ LIFE SCIENCE</span></div>
</aside>
<section class="stage" id="stage" aria-label="Interactive 3D animal cell">
<canvas id="cell-canvas" aria-label="3D animal cell. Drag to rotate, pinch to zoom, and tap a cell part."></canvas>
<div class="stage-top">
<div class="stage-heading"><span class="live-dot"></span><span id="stage-status">Building your cell</span><small id="stage-step">A fresh start · 00 / 11</small></div>
<div class="stage-top-right"><span class="demo-badge" id="demo-badge" hidden>Demo mode</span><button class="glass-btn" id="reset-view" type="button" aria-label="Reset the camera view">↗ Reset view</button></div>
</div>
<div class="point-label" id="point-label"></div>
<div class="stage-bottom">
<div class="stage-help"><strong id="help-title">A little world is taking shape.</strong><span id="help-text">Watch each part appear, then explore.</span></div>
<div class="stage-controls"><button class="control" id="cut" type="button" aria-pressed="false">Cut open <span class="switch" aria-hidden="true"></span></button><button class="control rebuild" id="rebuild" type="button"><span class="rebuild-icon" aria-hidden="true">↻</span> Rebuild</button></div>
</div>
<div class="load-error" id="load-error" hidden><div class="error-card"><strong>Our tiny world couldn't load.</strong><span>The 3D library needs a connection, and your browser needs to support WebGL. Please check your connection and reload.</span></div></div>
</section>
</main>
</div>
<script src="https://cdn.jsdelivr.net/npm/three@0.158.0/build/three.min.js"></script>
<script>
(function(){
'use strict';
const $ = id => document.getElementById(id);
const canvas = $('cell-canvas'), stage = $('stage');
if (!window.THREE) { $('load-error').hidden = false; return; }
let renderer;
try { renderer = new THREE.WebGLRenderer({canvas:canvas, antialias:true, alpha:true, powerPreference:'high-performance'}); }
catch (error) { $('load-error').hidden = false; return; }
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 1.6));
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.localClippingEnabled = true;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(38, 1, .1, 100);
scene.add(new THREE.HemisphereLight(0xe2fff2, 0x365052, 1.3));
const light = new THREE.DirectionalLight(0xffffff, 2.1); light.position.set(-4, 6, 10); scene.add(light);
const fill = new THREE.DirectionalLight(0x9be1dc, .7); fill.position.set(5, -3, -6); scene.add(fill);
const data = [
{name:'Nucleus', short:'Nucleus', color:'#c49ce3', description:"The nucleus is the cell's control center and holds its DNA.", anchor:[-.48,.12,-.12]},
{name:'Nucleolus', short:'Nucleolus', color:'#ea8fb5', description:'The nucleolus makes the pieces used to build ribosomes.', anchor:[-.45,.22,.42]},
{name:'Rough endoplasmic reticulum', short:'Rough ER', color:'#f6c88c', description:'The rough ER folds and carries proteins, with ribosomes on its surface.', anchor:[-1.84,-.1,.3]},
{name:'Smooth endoplasmic reticulum', short:'Smooth ER', color:'#91d6cf', description:'The smooth ER makes fats and helps break down harmful substances.', anchor:[-1.34,1.56,.15]},
{name:'Ribosomes', short:'Ribosomes', color:'#f5e7b2', description:'Ribosomes are tiny builders that make proteins for the cell.', anchor:[-1.82,.12,.48]},
{name:'Golgi apparatus', short:'Golgi apparatus', color:'#f5a997', description:'The Golgi apparatus sorts, packages, and sends materials around the cell.', anchor:[1.39,.32,.42]},
{name:'Mitochondria', short:'Mitochondria', color:'#f5a263', description:'Mitochondria release energy from food to power the cell.', anchor:[1.35,-1.4,.7]},
{name:'Lysosomes', short:'Lysosomes', color:'#f2d174', description:'Lysosomes break down waste and worn-out cell parts.', anchor:[2.24,-.12,.52]},
{name:'Vesicles', short:'Vesicles', color:'#8ddfe4', description:'Vesicles are tiny bubbles that carry materials around the cell.', anchor:[.4,1.85,1.0]},
{name:'Centrioles', short:'Centrioles', color:'#8bb9e8', description:'Centrioles help organize the fibers used when a cell divides.', anchor:[.25,-1.93,.52]},
{name:'Cytoskeleton', short:'Cytoskeleton', color:'#b3dfc1', description:'The cytoskeleton is a network of fibers that gives the cell shape and helps things move.', anchor:[-2.2,-.78,-.7]}
];
const total = data.length, clamp = (n,a,b) => Math.max(a,Math.min(b,n));
const sphere = new THREE.SphereGeometry(1, 28, 20);
const dotSphere = new THREE.SphereGeometry(1, 12, 9);
const material = (color,extra={}) => new THREE.MeshStandardMaterial(Object.assign({color:color,roughness:.39,metalness:0},extra));
const mats = {
nucleus:material(0xb891d5,{transparent:true,opacity:.53,depthWrite:false,roughness:.24,side:THREE.DoubleSide}),
pores:material(0xe9cbf3), nucleolus:material(0xe891b4,{roughness:.3}),
rough:material(0xe7b985), smooth:material(0x80cfc7), ribo:material(0xffedb6),
golgi:material(0xf09e91), golgiLight:material(0xffcec0), mito:material(0xf19a62),
mitoLine:material(0xffddb1), lyso:material(0xeeca69), vesicle:material(0x80dbe0,{transparent:true,opacity:.65,roughness:.15,depthWrite:false}),
centriole:material(0x8cbde8), fiber:material(0xb8e4c9,{transparent:true,opacity:.61,depthWrite:false}),
proxy:new THREE.MeshBasicMaterial({transparent:true,opacity:0,depthWrite:false,colorWrite:false})
};
function ball(parent, xyz, scale, mat, geometry=sphere){const m=new THREE.Mesh(geometry,mat);m.position.set(xyz[0],xyz[1],xyz[2]);m.scale.set(scale[0],scale[1],scale[2]);parent.add(m);return m;}
function curveTube(parent, points, radius, mat, proxy){const path=new THREE.CatmullRomCurve3(points.map(p=>new THREE.Vector3(p[0],p[1],p[2])));const m=new THREE.Mesh(new THREE.TubeGeometry(path,Math.max(24,points.length*4),radius,7,false),mat);if(proxy)m.userData.isProxy=true;parent.add(m);return m;}
function dots(parent, points, radius, mat){const m=new THREE.InstancedMesh(dotSphere,mat,points.length),o=new THREE.Object3D();points.forEach((p,i)=>{o.position.set(p[0],p[1],p[2]);o.scale.setScalar(radius*(p[3] || 1));o.updateMatrix();m.setMatrixAt(i,o.matrix);});m.instanceMatrix.needsUpdate=true;parent.add(m);return m;}
const groups = data.map((p,i)=>{const g=new THREE.Group();g.userData.partIndex=i;g.userData.anchor=new THREE.Vector3(...p.anchor);g.userData.revealedAt=0;g.visible=false;scene.add(g);return g;});
// A translucent, gently glowing membrane is present from the very beginning.
const membraneGeometry=new THREE.SphereGeometry(3.12,64,48);
const membranePos=membraneGeometry.attributes.position;
for(let i=0;i<membranePos.count;i++){const x=membranePos.getX(i),y=membranePos.getY(i),z=membranePos.getZ(i);const wobble=1+.014*Math.sin(x*2.3+y*1.7)*Math.cos(z*2.1-y*.9);membranePos.setXYZ(i,x*wobble,y*wobble,z*wobble);}
membraneGeometry.computeVertexNormals();
const membrane=new THREE.Group();scene.add(membrane);
membrane.add(new THREE.Mesh(membraneGeometry,new THREE.MeshPhongMaterial({color:0x91dfc9,transparent:true,opacity:.085,side:THREE.DoubleSide,depthWrite:false,shininess:90})));
const vertexShader=[
'#include <clipping_planes_pars_vertex>',
'varying vec3 vNormal; varying vec3 vView;',
'void main(){',
'vNormal=normalize(normalMatrix*normal);',
'vec4 mvPosition=modelViewMatrix*vec4(position,1.0);',
'vView=normalize(-mvPosition.xyz);',
'#include <clipping_planes_vertex>',
'gl_Position=projectionMatrix*mvPosition;',
'}'
].join('\n');
const fragmentShader=[
'#include <clipping_planes_pars_fragment>',
'varying vec3 vNormal; varying vec3 vView;',
'void main(){',
'#include <clipping_planes_fragment>',
'float rim=pow(1.0-abs(dot(normalize(vNormal),normalize(vView))),2.3);',
'gl_FragColor=vec4(0.54,0.95,0.81,0.022+rim*0.36);',
'}'
].join('\n');
membrane.add(new THREE.Mesh(membraneGeometry,new THREE.ShaderMaterial({transparent:true,side:THREE.DoubleSide,depthWrite:false,clipping:true,vertexShader:vertexShader,fragmentShader:fragmentShader})));
const cutPlane=new THREE.Plane(new THREE.Vector3(0,0,-1),0);
const cutRim=new THREE.Mesh(new THREE.TorusGeometry(3.08,.023,8,120),new THREE.MeshBasicMaterial({color:0xa5ead1,transparent:true,opacity:.7,depthWrite:false}));cutRim.visible=false;scene.add(cutRim);
// Nucleus and nucleolus.
ball(groups[0],[-.48,.12,-.12],[1.1,1.03,1.02],mats.nucleus);
const pores=[];for(let i=0;i<54;i++){const y=1-2*(i+.5)/54,rr=Math.sqrt(1-y*y),a=i*2.39996;pores.push([-.48+1.105*rr*Math.cos(a),.12+1.035*y,-.12+1.025*rr*Math.sin(a),.8+(i%3)*.12]);}dots(groups[0],pores,.035,mats.pores);
ball(groups[1],[-.45,.22,.42],[.39,.37,.36],mats.nucleolus);
ball(groups[1],[-.55,.32,.72],[.11,.075,.025],material(0xffc7d6,{transparent:true,opacity:.62,depthWrite:false}));
// Two networks of folded endoplasmic reticulum.
const riboPoints=[];
for(let k=0;k<6;k++){const points=[];for(let j=0;j<=20;j++){const t=j/20,x=-2.43+t*1.22,y=-.85+k*.30+.085*Math.sin(t*Math.PI*2+k*.7),z=.22+.13*Math.sin(t*Math.PI*2+k*.8);points.push([x,y,z]);if(j%3===0)riboPoints.push([x,y+.10,z+.08,.75+(j%4)*.12]);}curveTube(groups[2],points,.072,mats.rough);}
for(let k=0;k<5;k++){const points=[];for(let j=0;j<=18;j++){const t=j/18;points.push([-1.93+k*.29+.12*Math.sin(t*7+k),1.05+t*.98,.04+.17*Math.cos(t*6+k*.7)]);}curveTube(groups[3],points,.067,mats.smooth);}
[[-2.38,-.25,.9],[-1.5,-1.3,1.02],[-.9,-1.55,.85],[.15,1.47,1.1],[.3,-1.34,1.35],[1.03,1.35,.9],[-2.15,.8,.95],[-1.08,.95,1.2]].forEach((p,i)=>riboPoints.push([p[0],p[1],p[2],1+(i%2)*.2]));
dots(groups[4],riboPoints,.052,mats.ribo);
riboPoints.slice(-8).forEach(p=>{const m=ball(groups[4],p,[.13,.13,.13],mats.proxy);m.userData.isProxy=true;});
// A stack of curved Golgi sacs.
for(let k=0;k<5;k++){const points=[];for(let j=0;j<=16;j++){const t=j/16,s=t*2-1;points.push([1.4+s*.64,-.2+k*.22+.23*s*s,.40+.07*Math.cos(s*Math.PI)]);}curveTube(groups[5],points,.105,mats.golgi);curveTube(groups[5],points.map(p=>[p[0],p[1]+.025,p[2]+.085]),.025,mats.golgiLight);}
function mitochondrion(x,y,z,rotation,scale){const g=new THREE.Group();g.position.set(x,y,z);g.rotation.z=rotation;g.scale.setScalar(scale);groups[6].add(g);ball(g,[0,0,0],[.67,.32,.30],mats.mito);const p=[];for(let j=0;j<=14;j++){const xx=-.49+j*.07,yy=(j%2?1:-1)*.16*Math.sqrt(Math.max(.2,1-(xx/.67)**2)),zz=.30*Math.sqrt(Math.max(.1,1-(xx/.67)**2-(yy/.32)**2))+.018;p.push([xx,yy,zz]);}curveTube(g,p,.025,mats.mitoLine);}
mitochondrion(1.35,-1.4,.70,-.45,1);mitochondrion(1.85,1.42,-.38,.35,.88);mitochondrion(-1.95,-1.4,-.38,.6,.83);
[[2.24,-.12,.52,.24],[-.23,2.03,.55,.20],[-1.32,-1.43,1.03,.19]].forEach(p=>{ball(groups[7],p,[p[3],p[3],p[3]],mats.lyso);ball(groups[7],[p[0]-.06,p[1]+.07,p[2]+p[3]*.82],[.045,.035,.018],mats.golgiLight);});
[[-2.36,.20,.66,.18],[.40,1.85,1,.20],[2.06,-.91,.45,.16],[-.25,-2.35,.12,.15],[1.95,.75,1.03,.13]].forEach(p=>{ball(groups[8],p,[p[3],p[3],p[3]],mats.vesicle);ball(groups[8],[p[0]-.04,p[1]+.05,p[2]+p[3]*.86],[.035,.025,.012],mats.golgiLight);});
function barrel(x,y,z,angle){const g=new THREE.Group();g.position.set(x,y,z);g.rotation.z=angle;groups[9].add(g);for(let i=0;i<9;i++){const a=i*Math.PI*2/9,m=new THREE.Mesh(new THREE.CylinderGeometry(.04,.04,.68,8),mats.centriole);m.position.set(.16*Math.cos(a),0,.16*Math.sin(a));g.add(m);}}
barrel(.04,-1.94,.55,-.18);barrel(.42,-2.02,.55,1.28);
const fiberPaths=[
[[-2.6,-.7,-.85],[-1.8,-1.35,-1.2],[-.3,-1.85,-1.4],[1.2,-1.6,-1.1],[2.25,-.95,-.8]],
[[-2.4,.85,-.95],[-1.8,1.8,-1.15],[-.6,2.34,-1.0],[.8,2.2,-.8],[1.8,1.7,-.6]],
[[-2.38,-.95,-1.15],[-1.9,.0,-1.7],[-.9,.85,-1.9],[.5,1.05,-1.9],[1.8,.5,-1.35],[2.4,-.2,-.85]],
[[-2.35,.65,-.5],[-1.95,-.25,-1.35],[-1.25,-1.35,-1.65],[.1,-2.05,-1.2],[1.15,-1.95,-.65]],
[[-1.5,2.05,-.4],[-.45,1.8,-1.2],[.7,1,-1.8],[1.55,-.1,-1.6],[1.9,-1.25,-.8]]
];
fiberPaths.forEach(p=>{curveTube(groups[10],p,.021,mats.fiber);curveTube(groups[10],p,.10,mats.proxy,true);});
let progress=0,selected=-1,playing=true,cutOpen=false;
let demoActive=location.hash==='#demo',demoStart=null,demoPhase=-1;
if(demoActive)playing=false;
$('demo-badge').hidden=!demoActive;
const grid=$('parts-grid');
const buttons=data.map((part,i)=>{const b=document.createElement('button');b.type='button';b.className='part-button';b.style.setProperty('--part-color',part.color);b.setAttribute('aria-label',part.name);const dot=document.createElement('span');dot.className='part-dot';dot.setAttribute('aria-hidden','true');const text=document.createElement('span');text.textContent=part.short;b.append(dot,text);b.addEventListener('click',()=>{if(i>=progress)return;playing=false;selectPart(i,true);});grid.appendChild(b);return b;});
let baseDistance=11.1;
const current={theta:.2,phi:1.36,distance:11.1,target:new THREE.Vector3()};
const goal={theta:.2,phi:1.36,distance:11.1,target:new THREE.Vector3()};
let lastStep=performance.now()-400,previousFrame=0;
const reduceMotion=window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
function resize(){const w=Math.max(1,stage.clientWidth),h=Math.max(1,stage.clientHeight);renderer.setSize(w,h,false);camera.aspect=w/h;camera.updateProjectionMatrix();const old=baseDistance;baseDistance=Math.max(11.1,11.1/Math.min(1,camera.aspect));if(Math.abs(goal.distance-old)<.5)goal.distance=baseDistance;}
resize();current.distance=goal.distance=baseDistance;
window.addEventListener('resize',resize);
if(window.ResizeObserver){const observer=new ResizeObserver(resize);observer.observe(stage);}
function resetView(){goal.theta=.2;goal.phi=1.36;goal.distance=baseDistance;goal.target.set(0,0,0);}
function setCut(on){cutOpen=!!on;renderer.clippingPlanes=cutOpen?[cutPlane]:[];cutRim.visible=cutOpen;$('cut').classList.toggle('on',cutOpen);$('cut').setAttribute('aria-pressed',String(cutOpen));}
function syncUI(){
const p=selected<0?null:data[selected],pct=progress/total*100;
$('part-name').textContent=p?p.name:'Cell membrane';
$('part-description').textContent=p?p.description:'A soft, flexible skin holds the cell together and controls what goes in and out.';
$('detail-dot').style.background=p?p.color:'#a5dcc8';
$('detail-kicker').textContent=p?(progress===total?'Selected part':'Now showing'):'The starting point';
$('detail-count').textContent=String(progress).padStart(2,'0')+' / '+total;
$('timeline').value=progress;$('timeline').style.setProperty('--progress',pct+'%');$('timeline').setAttribute('aria-valuetext',progress+' of '+total+' parts added');
$('play-symbol').textContent=playing?'Ⅱ':'▶';$('play-text').textContent=playing?'Pause':(progress===total?'Replay':'Play');
$('play').setAttribute('aria-label',playing?'Pause the build':(progress===total?'Replay the build':'Continue the build'));
$('stage-status').textContent=progress===total?'Ready to explore':(playing||demoActive?'Building your cell':'Build paused');
$('stage-step').textContent=(progress===0?'A fresh start':progress===total?'All parts are here':'Piece by piece')+' · '+String(progress).padStart(2,'0')+' / '+total;
$('help-title').textContent=progress===total?'Your cell is ready to explore.':'A little world is taking shape.';
$('help-text').textContent=progress===total?'Drag to orbit · Pinch or scroll to zoom · Tap a part':'Watch each part appear, then explore.';
buttons.forEach((b,i)=>{b.disabled=i>=progress;b.classList.toggle('active',i===selected);b.setAttribute('aria-pressed',String(i===selected));});
const label=$('point-label');label.textContent=p?p.short:'';label.style.setProperty('--label-color',p?p.color:'#a5dcc8');label.style.display=p?'flex':'none';
}
function setProgress(value){const n=clamp(Math.round(value),0,total),old=progress;progress=n;if(n!==old)selected=n?n-1:-1;groups.forEach((g,i)=>{const show=i<n;if(show&&!g.visible)g.userData.revealedAt=performance.now();g.visible=show;});if(n===total)playing=false;syncUI();}
function selectPart(i,fly){if(i<0||i>=progress)return;selected=i;if(fly){goal.target.copy(groups[i].userData.anchor);goal.distance=clamp(baseDistance*.73,6.6,17);}syncUI();}
function stopDemo(){if(!demoActive)return;demoActive=false;$('demo-badge').hidden=true;playing=progress<total;lastStep=performance.now();syncUI();}
window.addEventListener('pointerdown',stopDemo,true);
window.addEventListener('wheel',stopDemo,{capture:true,passive:true});
$('play').addEventListener('click',()=>{stopDemo();if(progress===total){setCut(false);resetView();setProgress(0);playing=true;}else playing=!playing;lastStep=performance.now();syncUI();});
$('timeline').addEventListener('input',e=>{stopDemo();playing=false;setProgress(Number(e.target.value));syncUI();});
$('cut').addEventListener('click',()=>{stopDemo();setCut(!cutOpen);});
$('reset-view').addEventListener('click',()=>{stopDemo();resetView();});
$('rebuild').addEventListener('click',()=>{stopDemo();setCut(false);resetView();setProgress(0);playing=true;lastStep=performance.now()-400;syncUI();});
const raycaster=new THREE.Raycaster(),pointer=new THREE.Vector2();
function partOf(object){let item=object;while(item&&typeof item.userData.partIndex!=='number')item=item.parent;return item?item.userData.partIndex:-1;}
function pick(clientX,clientY){const r=canvas.getBoundingClientRect();pointer.set((clientX-r.left)/r.width*2-1,-((clientY-r.top)/r.height)*2+1);raycaster.setFromCamera(pointer,camera);const found=raycaster.intersectObjects(groups,true).map(hit=>({hit:hit,index:partOf(hit.object)})).filter(x=>x.index>=0&&x.index<progress&&(!cutOpen||cutPlane.distanceToPoint(x.hit.point)>=-.01));let visible=found.filter(x=>!x.hit.object.userData.isProxy);if(!visible.length)visible=found;if(!visible.length)return;let chosen=visible[0];if(chosen.index===0){const other=visible.find(x=>x.index!==0);if(other)chosen=other;}playing=false;selectPart(chosen.index,true);}
const pointers=new Map();let hadMultiple=false,lastPinch=0;
function fingerDistance(){const p=Array.from(pointers.values());return p.length<2?0:Math.hypot(p[0].x-p[1].x,p[0].y-p[1].y);}
canvas.addEventListener('pointerdown',e=>{if(e.pointerType==='mouse'&&e.button!==0)return;pointers.set(e.pointerId,{x:e.clientX,y:e.clientY,sx:e.clientX,sy:e.clientY,moved:false});try{canvas.setPointerCapture(e.pointerId);}catch(error){}if(pointers.size>1){hadMultiple=true;lastPinch=fingerDistance();}});
canvas.addEventListener('pointermove',e=>{const p=pointers.get(e.pointerId);if(!p)return;const dx=e.clientX-p.x,dy=e.clientY-p.y;p.x=e.clientX;p.y=e.clientY;if(Math.hypot(p.x-p.sx,p.y-p.sy)>6)p.moved=true;if(progress!==total)return;if(pointers.size===1&&!hadMultiple){goal.theta-=dx*.007;goal.phi=clamp(goal.phi-dy*.007,.18,Math.PI-.18);}else if(pointers.size>=2){const next=fingerDistance();if(lastPinch>0&&next>0)goal.distance=clamp(goal.distance*lastPinch/next,5.5,24);lastPinch=next;}});
function releasePointer(e){const p=pointers.get(e.pointerId);if(p&&!p.moved&&!hadMultiple&&progress===total)pick(e.clientX,e.clientY);pointers.delete(e.pointerId);if(!pointers.size){hadMultiple=false;lastPinch=0;}}
canvas.addEventListener('pointerup',releasePointer);canvas.addEventListener('pointercancel',e=>{pointers.delete(e.pointerId);if(!pointers.size){hadMultiple=false;lastPinch=0;}});
canvas.addEventListener('wheel',e=>{if(progress!==total)return;e.preventDefault();goal.distance=clamp(goal.distance*Math.exp(e.deltaY*.001),5.5,24);},{passive:false});
function demoTick(now){if(demoStart===null)demoStart=now;let t=(now-demoStart)/1000;if(t>=20){demoStart=now;t=0;demoPhase=-1;setCut(false);resetView();setProgress(0);}let phase=t<8.6?0:t<11.4?1:t<13?2:t<15?3:t<16.2?4:t<18.3?5:6;if(phase===0){const n=Math.min(total,Math.floor(t/.72));if(n!==progress)setProgress(n);}if(phase!==demoPhase){demoPhase=phase;if(phase===1)selectPart(6,true);if(phase===2)setCut(true);if(phase===3)selectPart(5,true);if(phase===4)setProgress(5);if(phase===5){setCut(false);setProgress(total);}if(phase===6){setCut(false);resetView();setProgress(0);}}if(phase>=1&&phase<=3)goal.theta=.2+(t-8.6)*.32;}
function placeLabel(){if(selected<0)return;const world=groups[selected].localToWorld(groups[selected].userData.anchor.clone());world.project(camera);const w=stage.clientWidth,h=stage.clientHeight,label=$('point-label');if(world.z>1||world.z< -1){label.style.display='none';return;}label.style.display='flex';label.style.left=clamp((world.x+1)*w/2+18,10,Math.max(10,w-label.offsetWidth-10))+'px';label.style.top=clamp((-world.y+1)*h/2-15,70,h-130)+'px';}
function animate(now){requestAnimationFrame(animate);const dt=previousFrame?Math.min((now-previousFrame)/1000,.05):.016;previousFrame=now;if(demoActive)demoTick(now);else if(playing&&progress<total&&now-lastStep>=1900){setProgress(progress+1);lastStep=now;}
const t=now*.001,motion=reduceMotion ? .25 : 1;
membrane.scale.set(1.02*(1+.004*Math.sin(t*.7)*motion),.97*(1+.004*Math.sin(t*.65+1)*motion),.96*(1+.004*Math.sin(t*.6+2)*motion));
groups.forEach((g,i)=>{if(!g.visible)return;const intro=clamp((now-g.userData.revealedAt)/650,0,1),ease=1-Math.pow(1-intro,3),pulse=1+.012*Math.sin(t*.8+i)*motion;g.scale.setScalar((.55+.45*ease)*pulse);g.position.set(.035*Math.sin(t*.5+i)*motion,.04*Math.sin(t*.55+i*1.3)*motion,.025*Math.cos(t*.48+i)*motion);});
const a=1-Math.exp(-dt*5);current.theta+=(goal.theta-current.theta)*a;current.phi+=(goal.phi-current.phi)*a;current.distance+=(goal.distance-current.distance)*a;current.target.lerp(goal.target,a);
camera.position.set(current.target.x+current.distance*Math.sin(current.phi)*Math.sin(current.theta),current.target.y+current.distance*Math.cos(current.phi),current.target.z+current.distance*Math.sin(current.phi)*Math.cos(current.theta));camera.lookAt(current.target);camera.updateMatrixWorld();
if(cutOpen){const normal=camera.position.clone().normalize();cutPlane.normal.copy(normal).negate();cutPlane.constant=0;cutRim.quaternion.setFromUnitVectors(new THREE.Vector3(0,0,1),normal);}
renderer.render(scene,camera);placeLabel();
}
syncUI();requestAnimationFrame(animate);
})();
</script>
</body>
</html>
