Back to the app
The code
Build a Cell, Part by Part by GPT-6.1 Sol
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.0, viewport-fit=cover">
<meta name="theme-color" content="#101c25">
<title>Cell by cell — Little Lab</title>
<style>
:root{--bg:#101c25;--panel:#172731;--line:#2a3b43;--text:#f4f5ec;--muted:#9baeb4;--mint:#b8eccd;--accent:#b8eccd;--color:#b8eccd}*{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--text);font-family:Inter,ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;-webkit-font-smoothing:antialiased}button,input{font:inherit}button{color:inherit;cursor:pointer}button:focus-visible,input:focus-visible{outline:3px solid var(--mint);outline-offset:4px}button{touch-action:manipulation}svg{display:block}header{height:88px;margin:0 4.2%;display:flex;align-items:center;justify-content:space-between;border-bottom:1px solid var(--line)}.brand{display:flex;gap:11px;align-items:center;font-size:21px;font-weight:720;letter-spacing:-.8px}.brand-icon{width:36px;height:36px;color:var(--mint)}.header-right{display:flex;align-items:center;gap:24px}.eyebrow{font-size:10px;letter-spacing:2px;font-weight:650;text-transform:uppercase}.header-note{color:var(--muted);font-size:12px}.lesson-tag{border:1px solid #3a4b50;padding:9px 13px;border-radius:30px;color:#d4dfd9;font-size:11px;display:flex;align-items:center;gap:8px}.lesson-tag i{width:6px;height:6px;border-radius:50%;background:var(--mint)}main{max-width:1600px;margin:auto;padding:34px 4.2% 20px}.intro{display:flex;justify-content:space-between;align-items:flex-end;margin-bottom:27px}.intro .eyebrow{color:var(--mint);margin-bottom:10px}h1{font-size:clamp(36px,3.4vw,52px);font-weight:520;line-height:1.1;letter-spacing:-2.2px;margin:0}h1 em{font-family:Georgia,serif;font-weight:400;color:var(--mint)}.intro p{color:var(--muted);font-size:13px;line-height:1.7;margin:0 0 3px;max-width:300px}.workspace{display:grid;grid-template-columns:minmax(0,1fr) 320px;gap:22px}.left{min-width:0}.stage{height:clamp(430px,49vh,620px);position:relative;border:1px solid var(--line);border-radius:18px;overflow:hidden;background:radial-gradient(ellipse at 48% 53%,#25444a7a 0,#182c3570 38%,#14222b 74%)}.stage:before{content:"";position:absolute;inset:0;background-image:radial-gradient(#52676c55 .7px,transparent .7px);background-size:26px 26px;pointer-events:none;mask-image:linear-gradient(to bottom,transparent,#000 30%,#000 70%,transparent)}#canvas{width:100%;height:100%;display:block;position:relative;touch-action:none;cursor:grab}#canvas:active{cursor:grabbing}.stage-top{position:absolute;left:23px;right:23px;top:21px;display:flex;align-items:center;justify-content:space-between;pointer-events:none}.live-label{font-size:10px;letter-spacing:1.7px;color:#c4d3d2;display:flex;align-items:center;gap:8px}.live-label span{width:5px;height:5px;background:var(--mint);border-radius:50%;box-shadow:0 0 10px #b8eccd65}.view-tag{color:#8eaaad;font-size:10px;font-family:ui-monospace,monospace;border:1px solid #3a5155;border-radius:5px;padding:5px 7px}.stage-tools{position:absolute;right:17px;bottom:17px;display:flex;gap:7px}.icon-button{width:40px;height:40px;display:grid;place-items:center;background:#182c35d9;border:1px solid #3b5055;border-radius:11px;transition:background .2s}.icon-button:hover{background:#29434a}.icon-button svg{width:17px;height:17px}.orientation{position:absolute;bottom:22px;left:23px;color:#6f9399;font-size:10px;font-family:ui-monospace,monospace;display:flex;align-items:center;gap:9px}.orientation svg{width:25px;height:25px}.float-label{position:absolute;left:52%;top:42%;pointer-events:none;display:flex;align-items:center;gap:9px;transition:opacity .3s;filter:drop-shadow(0 3px 14px #07181c);white-space:nowrap}.float-label:before{content:"";width:34px;height:1px;background:var(--color);opacity:.65}.float-label span{display:flex;gap:8px;align-items:center;font-size:11px;padding:10px 12px;border:1px solid #526366;background:#14252de8;border-radius:9px}.float-label b{height:6px;width:6px;background:var(--color);border-radius:100%}.scene-note{position:absolute;left:50%;bottom:26px;transform:translateX(-50%);color:#b1c5c6;font-size:11px;pointer-events:none;white-space:nowrap;display:flex;align-items:center;gap:8px}.scene-note svg{width:15px;height:15px}.demo-badge{display:none;position:absolute;left:23px;top:55px;background:var(--mint);color:#17352a;border-radius:30px;padding:6px 10px;font-size:10px;font-weight:650}.demo-badge.visible{display:block}.controls{border:1px solid var(--line);border-radius:16px;padding:19px 22px;margin-top:14px;background:#14242d}.controls-top{display:flex;align-items:center;justify-content:space-between;gap:15px;margin-bottom:16px}.timeline-title{font-size:12px;font-weight:600}.timeline-title small{color:var(--muted);font-size:10px;font-weight:400;margin-left:9px}.play-button{border:0;border-radius:8px;min-width:80px;height:34px;background:#b8eccd;color:#17372b;display:flex;align-items:center;justify-content:center;gap:7px;font-size:11px;font-weight:650}.play-button svg{width:12px;height:12px}.range-wrap{position:relative;height:22px;display:flex;align-items:center}input[type=range]{appearance:none;-webkit-appearance:none;width:100%;height:5px;border-radius:5px;background:linear-gradient(to right,var(--mint) 0%,var(--mint) var(--progress,0%),#34494e var(--progress,0%),#34494e 100%);cursor:pointer;position:relative;z-index:2;margin:0}input[type=range]::-webkit-slider-thumb{appearance:none;width:17px;height:17px;background:#d3f2d6;border:3px solid #152c2d;border-radius:50%;box-shadow:0 0 0 1px #b8eccd}input[type=range]::-moz-range-thumb{width:12px;height:12px;background:#d3f2d6;border:3px solid #152c2d;border-radius:50%}.range-labels{display:flex;justify-content:space-between;color:var(--muted);font-size:9px;margin-top:7px}.bottom-actions{display:flex;justify-content:space-between;align-items:center;margin-top:18px;padding-top:15px;border-top:1px solid var(--line)}.switch-label{display:flex;align-items:center;gap:10px;font-size:12px;cursor:pointer;min-height:36px}.switch{width:37px;height:22px;border-radius:20px;border:1px solid #53656a;background:#2a3c43;padding:2px;display:flex;align-items:center;transition:.25s}.switch i{width:16px;height:16px;border-radius:50%;background:#bac9c8;transition:.25s}.switch[aria-checked=true]{background:var(--mint);border-color:var(--mint)}.switch[aria-checked=true] i{transform:translateX(14px);background:#1b4032}.text-button{background:transparent;border:0;display:flex;align-items:center;gap:8px;font-size:11px;padding:10px 0 10px 15px;color:#cfddda;min-height:38px}.text-button svg{width:14px;height:14px}.sidebar{display:flex;flex-direction:column;gap:15px}.info-card{padding:23px;background:#dce9dd;color:#233d34;border-radius:16px;position:relative;overflow:hidden;min-height:239px;transition:background .4s}.info-card:after{content:"";position:absolute;right:-24px;top:-35px;width:120px;height:120px;border-radius:50%;border:1px solid #244f3520;box-shadow:0 0 0 15px #244f3509,0 0 0 30px #244f3506;pointer-events:none}.info-top{display:flex;justify-content:space-between;align-items:center;margin-bottom:22px}.info-top .eyebrow{font-size:9px;letter-spacing:1.5px}.step-count{font-family:ui-monospace,monospace;font-size:11px;color:#476555}.info-card h2{font-size:25px;line-height:1.15;font-weight:560;letter-spacing:-.8px;margin:0 0 12px;position:relative}.info-card p{font-size:13px;line-height:1.7;margin:0;position:relative;max-width:270px}.role-tag{display:inline-flex;align-items:center;gap:6px;margin-top:19px;font-size:9px;text-transform:uppercase;letter-spacing:1.1px;border:1px solid #7e9d8860;border-radius:30px;padding:6px 9px}.role-tag:before{content:"";height:5px;width:5px;border-radius:50%;background:currentColor}.parts-panel{border:1px solid var(--line);border-radius:16px;padding:20px 15px 13px;background:#14232c;flex:1}.parts-head{display:flex;justify-content:space-between;align-items:center;padding:0 6px 12px}.parts-head h3{font-size:11px;font-weight:500;margin:0;color:#dbe4de}.parts-head span{font-family:ui-monospace,monospace;font-size:10px;color:#789799}.parts-list{display:grid;gap:3px}.part-row{width:100%;display:flex;align-items:center;text-align:left;gap:10px;min-height:34px;padding:7px 9px;border:1px solid transparent;border-radius:7px;background:transparent;font-size:11px;color:#7f969e;transition:background .2s,color .2s}.part-row .dot{width:7px;height:7px;border-radius:50%;background:var(--part);flex-shrink:0;opacity:.35}.part-row .num{margin-left:auto;color:#5b737c;font-size:9px;font-family:ui-monospace,monospace}.part-row.built{color:#d4dfdf}.part-row.built .dot{opacity:1;box-shadow:0 0 8px color-mix(in srgb,var(--part),transparent 75%)}.part-row.active{background:#263d43;border-color:#435c5b;color:#f2f6ed}.part-row.active .num{color:var(--mint)}.part-row:hover{background:#21353e}.panel-tip{font-size:10px;color:#859ca3;padding:12px 6px 0;border-top:1px solid var(--line);margin-top:12px;line-height:1.6}.footer{display:flex;justify-content:space-between;gap:15px;margin-top:21px;color:#708891;font-size:10px;line-height:1.6}.footer span:first-child{display:flex;gap:8px;align-items:center}.footer i{display:inline-block;width:5px;height:5px;background:var(--mint);border-radius:50%}.error{position:absolute;inset:0;display:none;align-items:center;justify-content:center;padding:45px;text-align:center;z-index:10;background:#14242df5;line-height:1.8;color:#d5e6dd}.sr-only{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border:0}@media(min-width:1500px){main{padding-top:40px}.stage{height:570px}.workspace{grid-template-columns:minmax(0,1fr) 350px}.part-row{min-height:38px}}@media(max-width:1000px){.workspace{grid-template-columns:minmax(0,1fr) 285px;gap:15px}.header-note{display:none}.intro p{max-width:240px;font-size:12px}.stage{height:480px}.info-card{padding:20px}.parts-panel{padding:17px 11px}.part-row{font-size:10px}}@media(max-width:730px){header{height:70px;margin:0 6%}.brand{font-size:20px}.lesson-tag{font-size:10px;padding:8px 10px}.lesson-tag .optional{display:none}main{padding:27px 5% 22px}.intro{display:block;margin-bottom:21px}h1{font-size:41px}.intro p{max-width:100%;margin-top:12px;font-size:12px}.intro p br{display:none}.intro .eyebrow{font-size:9px}.workspace{grid-template-columns:1fr;gap:17px}.stage{height:410px;border-radius:15px}.stage-top{left:17px;right:17px;top:17px}.scene-note{bottom:25px;font-size:10px}.orientation{bottom:78px;left:17px}.stage-tools{right:12px;bottom:12px;flex-direction:column}.icon-button{width:36px;height:36px;border-radius:9px}.controls{padding:16px 18px}.info-card{min-height:210px;padding:22px}.info-card p{max-width:100%;font-size:14px}.info-card h2{font-size:27px}.info-top{margin-bottom:17px}.role-tag{margin-top:15px}.sidebar{gap:15px}.parts-list{grid-template-columns:1fr 1fr;gap:5px}.part-row{font-size:11px;min-height:43px;padding:8px}.part-row .num{display:none}.parts-panel{padding:19px 12px 15px}.footer{display:block;font-size:9px}.footer>span{display:block;margin-bottom:5px}.timeline-title small{font-size:9px}.float-label span{font-size:10px;padding:8px 9px}.float-label:before{width:20px}}@media(prefers-reduced-motion:reduce){*{transition:none!important}}
</style>
</head>
<body>
<svg aria-hidden="true" style="position:absolute;width:0;height:0;overflow:hidden"><defs><symbol id="reset-icon" viewBox="0 0 24 24"><path d="M4 10a8 8 0 1 1 1.6 7M4 4v6h6" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"/></symbol><symbol id="home-icon" viewBox="0 0 24 24"><path d="m4 9 8-5 8 5v9l-8 4-8-4zM4 9l8 4 8-4M12 13v9" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round"/></symbol><symbol id="plus-icon" viewBox="0 0 24 24"><path d="M5 12h14M12 5v14" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round"/></symbol><symbol id="minus-icon" viewBox="0 0 24 24"><path d="M5 12h14" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round"/></symbol></defs></svg>
<header><div class="brand"><svg class="brand-icon" viewBox="0 0 40 40" fill="none" aria-hidden="true"><path d="M34 21c0 8-6 14-14 13C11 34 5 29 6 20 5 12 12 6 20 6s15 6 14 15Z" stroke="currentColor" stroke-width="1.5"/><ellipse cx="20" cy="20" rx="7" ry="8" stroke="currentColor" stroke-width="1.5" transform="rotate(-25 20 20)"/><circle cx="21" cy="19" r="2.5" fill="currentColor"/><path d="m11 14 2 2m13 11 3-1M12 27l2-2m12-14 2 2" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/></svg>little lab<span style="color:var(--mint)">.</span></div><div class="header-right"><span class="header-note">Big discoveries. Tiny worlds.</span><span class="lesson-tag"><i></i> BIOLOGY <span class="optional"> / INTERACTIVE 01</span></span></div></header>
<main>
<section class="intro"><div><div class="eyebrow">Explore the building blocks of life</div><h1>Cell by <em>cell.</em></h1></div><p>A tiny world with a lot going on.<br>Build an animal cell and get to know its parts.</p></section>
<div class="workspace">
<section class="left" aria-label="Interactive animal cell">
<div class="stage" id="stage"><canvas id="canvas" aria-label="3D animal cell. Drag to rotate, pinch or scroll to zoom, and tap a part to explore."></canvas><div class="stage-top"><div class="live-label"><span></span> ANIMAL CELL</div><div class="view-tag" id="viewTag">3D EXPLORER</div></div><div id="demoBadge" class="demo-badge">Demo playing · touch to explore</div><div class="float-label" id="floatingLabel" style="--color:#b8eccd"><span><b></b><span id="floatingText" style="padding:0;border:0;background:none">Cell membrane</span></span></div><div class="orientation"><svg viewBox="0 0 30 30" fill="none"><path d="M14 22V6m0 16 11-6M14 22 4 16" stroke="currentColor" stroke-width="1.2"/><circle cx="14" cy="22" r="2" fill="currentColor"/></svg> X / Y / Z</div><div class="scene-note" id="sceneNote"><svg viewBox="0 0 20 20" fill="none"><path d="M5 12V9a1 1 0 0 1 2 0V5a1 1 0 0 1 2 0v5-2a1 1 0 0 1 2 0v2-1a1 1 0 0 1 2 0v2a1 1 0 0 1 2 0v3l-3 3H8l-3-5Z" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"/></svg>Drag to orbit · pinch or scroll to zoom</div><div class="stage-tools"><button class="icon-button" id="zoomIn" title="Zoom in" aria-label="Zoom in"><svg><use href="#plus-icon"/></svg></button><button class="icon-button" id="zoomOut" title="Zoom out" aria-label="Zoom out"><svg><use href="#minus-icon"/></svg></button><button class="icon-button" id="home" title="Reset view" aria-label="Reset view"><svg><use href="#home-icon"/></svg></button></div><div class="error" id="error" role="status"></div></div>
<div class="controls"><div class="controls-top"><div class="timeline-title">Build your cell <small id="buildStatus">Starting with the membrane</small></div><button id="play" class="play-button" aria-label="Pause the build"><svg id="playIcon" viewBox="0 0 12 12" fill="currentColor"><path d="M3 2h2v8H3zm4 0h2v8H7z"/></svg><span id="playText">Pause</span></button></div><div class="range-wrap"><input type="range" id="buildRange" min="0" max="11" step="1" value="0" aria-label="Cell build progress" aria-valuetext="Empty membrane, 0 of 11 parts"></div><div class="range-labels"><span>EMPTY MEMBRANE</span><span id="progressCount">0 / 11</span><span>COMPLETE CELL</span></div><div class="bottom-actions"><label class="switch-label" for="cut"><button class="switch" id="cut" role="switch" aria-checked="false" aria-label="Cut open the cell"><i></i></button>Cut open</label><button class="text-button" id="rebuild"><svg><use href="#reset-icon"/></svg>Rebuild</button></div></div>
</section>
<aside class="sidebar"><section class="info-card" id="infoCard" aria-live="polite"><div class="info-top"><span class="eyebrow" id="infoEyebrow">First, the boundary</span><span class="step-count" id="stepCount">00 / 11</span></div><h2 id="partTitle">Cell membrane</h2><p id="partDescription">The membrane is a flexible boundary that controls what enters and leaves the cell.</p><span class="role-tag" id="roleTag">The gatekeeper</span></section><section class="parts-panel"><div class="parts-head"><h3>A little team. A living cell.</h3><span id="partsCount">00 / 11</span></div><div class="parts-list" id="partsList"></div><div class="panel-tip" id="panelTip">Watch it grow, or move the slider.<br>Tap a part to get a closer look.</div></section></aside>
</div><footer class="footer"><span><i></i>Made for curious minds.</span><span>Illustrative model · colors identify parts · sizes and numbers simplified</span></footer>
</main>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/OrbitControls.js"></script>
<script>
(()=>{'use strict';
const $=id=>document.getElementById(id);
const data=[
{name:'Nucleus',color:'#ad9bea',tint:'#e1dcf1',role:'The instruction keeper',sentence:'The nucleus holds the instructions that tell the cell what to do.',position:[-.65,.3,-.35],size:1.1},
{name:'Nucleolus',color:'#e4a2d9',tint:'#f0dbe9',role:'The ribosome workshop',sentence:'The nucleolus makes the pieces that come together to form ribosomes.',position:[-.75,.36,-.04],size:.4},
{name:'Rough ER',full:'Rough endoplasmic reticulum',color:'#88adeb',tint:'#dce6f3',role:'The protein workshop',sentence:'The rough ER helps fold and move proteins made by the ribosomes on its surface.',position:[-.65,.1,-.36],size:1.5},
{name:'Smooth ER',full:'Smooth endoplasmic reticulum',color:'#71cbb1',tint:'#d9ece1',role:'The chemical workshop',sentence:'The smooth ER makes fats and helps remove harmful chemicals.',position:[-1.5,-.78,.05],size:.85},
{name:'Ribosomes',color:'#efd39c',tint:'#f0e8d6',role:'The protein builders',sentence:'Ribosomes read instructions and link tiny building blocks together to make proteins.',position:[.1,.86,.4],size:.6},
{name:'Golgi apparatus',color:'#e8a5b9',tint:'#f0dfe3',role:'The packing station',sentence:'The Golgi apparatus sorts and packages proteins to send them where they are needed.',position:[1.25,-.38,-.25],size:1},
{name:'Mitochondria',color:'#f3aa7c',tint:'#f1e2d6',role:'The energy converters',sentence:'Mitochondria turn energy from food into a form the cell can use.',position:[1.45,1,.05],size:.85},
{name:'Lysosomes',color:'#d7d27b',tint:'#ecebd8',role:'The recycling team',sentence:'Lysosomes break down waste and worn-out cell parts so their materials can be reused.',position:[.5,-1.43,.35],size:.55},
{name:'Vesicles',color:'#83cedc',tint:'#dbecef',role:'The tiny delivery sacs',sentence:'Vesicles are tiny sacs that store materials and carry them around or out of the cell.',position:[1.98,-.52,.23],size:.5},
{name:'Centrioles',color:'#e5bd79',tint:'#eee5d4',role:'The division organizers',sentence:'Centrioles help organize the fibers that pull chromosomes apart when an animal cell divides.',position:[.45,.08,.65],size:.65},
{name:'Cytoskeleton',color:'#8ec8bd',tint:'#dcebe4',role:'The inner framework',sentence:'The cytoskeleton is a network of fibers that supports the cell and helps its parts move.',position:[0,0,0],size:2.5}
];
let count=0,selected=-1,playing=true,cut=false,demo=location.hash==='#demo',lastBuild=0,demoStart=0,demoCycle=-1,demoEvent=-1,flight=null;
let renderer,scene,camera,orbit,cell,membrane,cutEdge,parts=[],pickables=[],materials=new Set();
const reduced=matchMedia('(prefers-reduced-motion: reduce)').matches;
data.forEach((d,i)=>{const b=document.createElement('button');b.className='part-row';b.style.setProperty('--part',d.color);b.innerHTML='<span class="dot"></span><span>'+d.name+'</span><span class="num">'+String(i+1).padStart(2,'0')+'</span>';b.setAttribute('aria-label','Explore '+(d.full||d.name));b.addEventListener('click',()=>{pause();if(count<i+1)setBuild(i+1);focusPart(i)});$('partsList').appendChild(b);d.button=b});
const membraneInfo={name:'Cell membrane',sentence:'The membrane is a flexible boundary that controls what enters and leaves the cell.',role:'The gatekeeper',tint:'#dce9dd',color:'#b8eccd'};
function info(i){selected=i;const d=i<0?membraneInfo:data[i];$('partTitle').textContent=d.full||d.name;$('partTitle').style.fontSize=i===2||i===3?'22px':'';$('partDescription').textContent=d.sentence;$('roleTag').textContent=d.role;$('infoCard').style.background=d.tint;$('infoEyebrow').textContent=i<0?'First, the boundary':playing?'Meet the next part':'A closer look';$('stepCount').textContent=String(i+1).padStart(2,'0')+' / 11';$('floatingText').textContent=d.name;$('floatingLabel').style.setProperty('--color',d.color);data.forEach((p,j)=>p.button.classList.toggle('active',i===j));}
function playUI(){$('playText').textContent=playing?'Pause':count===11?'Replay':'Play';$('playIcon').innerHTML=playing?'<path d="M3 2h2v8H3zm4 0h2v8H7z"/>':'<path d="m3 1 8 5-8 5z"/>';$('play').setAttribute('aria-label',playing?'Pause the build':count===11?'Replay the build':'Continue the build');}
function pause(){playing=false;playUI();}
function setBuild(n,show=true){count=Math.max(0,Math.min(11,n));$('buildRange').value=count;$('buildRange').style.setProperty('--progress',(count/11*100)+'%');$('buildRange').setAttribute('aria-valuetext',(count?data[count-1].name:'Empty membrane')+', '+count+' of 11 parts');$('progressCount').textContent=count+' / 11';$('partsCount').textContent=String(count).padStart(2,'0')+' / 11';$('buildStatus').textContent=count===0?'Starting with the membrane':count===11?'All parts, working together':count+' of 11 parts added';$('panelTip').innerHTML=count===11?'Your cell is complete. Tap any part to explore.<br>Try cutting it open for a new perspective.':'Watch it grow, or move the slider.<br>Tap a part to get a closer look.';data.forEach((d,i)=>{d.button.classList.toggle('built',i<count);d.button.setAttribute('aria-label',(i<count?'Explore ':'Build through ')+(d.full||d.name));if(parts[i]){let p=parts[i];if(i<count&&!p.visible)p.userData.appear=performance.now();p.visible=i<count;}});if(show)info(count-1);if(count===11)playing=false;playUI();}
function setCut(value){cut=value;$('cut').setAttribute('aria-checked',String(cut));$('viewTag').textContent=cut?'CUTAWAY VIEW':'3D EXPLORER';if(renderer){const plane=new THREE.Plane(new THREE.Vector3(0,0,-1),.05);materials.forEach(m=>{m.clippingPlanes=cut?[plane]:[];m.needsUpdate=true});cutEdge.visible=cut;}}
function resetView(){if(!camera)return;flight={start:performance.now(),from:camera.position.clone(),to:new THREE.Vector3(.45,.85,8.8),targetFrom:orbit.target.clone(),targetTo:new THREE.Vector3(0,0,0),duration:1100};}
function focusPart(i){if(i>=count||!parts[i])return;info(i);const target=parts[i].position.clone();if(i===10)target.set(0,0,0);let direction=camera.position.clone().sub(orbit.target).normalize();if(direction.length()<.1)direction.set(0,.1,1).normalize();const distance=i===10?7.7:i===1?2.5:i===2?4.6:Math.max(2.9,data[i].size*3.15);flight={start:performance.now(),from:camera.position.clone(),to:target.clone().addScaledVector(direction,distance),targetFrom:orbit.target.clone(),targetTo:target,duration:1200};}
$('play').addEventListener('click',()=>{if(playing){pause();return}if(count===11){setBuild(0);resetView()}playing=true;lastBuild=performance.now();playUI()});
$('buildRange').addEventListener('input',e=>{pause();flight=null;setBuild(+e.target.value)});
$('cut').addEventListener('click',()=>setCut(!cut));
$('rebuild').addEventListener('click',()=>{setCut(false);setBuild(0);resetView();playing=true;lastBuild=performance.now();playUI()});
$('home').addEventListener('click',resetView);
function zoom(factor){if(!camera)return;flight=null;const off=camera.position.clone().sub(orbit.target);off.multiplyScalar(factor);off.clampLength(orbit.minDistance,orbit.maxDistance);camera.position.copy(orbit.target).add(off);orbit.update()}
$('zoomIn').addEventListener('click',()=>zoom(.8));$('zoomOut').addEventListener('click',()=>zoom(1.25));
function stopDemo(){if(!demo)return;demo=false;$('demoBadge').classList.remove('visible');pause();flight=null;}
document.addEventListener('pointerdown',stopDemo,{capture:true,passive:true});document.addEventListener('wheel',stopDemo,{capture:true,passive:true});document.addEventListener('keydown',stopDemo,{capture:true});
function showError(message){$('error').style.display='flex';$('error').textContent=message;pause();$('floatingLabel').style.display='none';}
if(typeof THREE==='undefined'||!THREE.OrbitControls){showError('The 3D library could not load. Connect to the internet and reload to explore your cell.');return;}
try{
renderer=new THREE.WebGLRenderer({canvas:$('canvas'),antialias:true,alpha:true});renderer.setPixelRatio(Math.min(devicePixelRatio||1,2));renderer.setClearColor(0x000000,0);renderer.outputEncoding=THREE.sRGBEncoding;renderer.localClippingEnabled=true;
scene=new THREE.Scene();camera=new THREE.PerspectiveCamera(39,1,.05,80);camera.position.set(.45,.85,8.8);
orbit=new THREE.OrbitControls(camera,$('canvas'));orbit.enableDamping=true;orbit.dampingFactor=.065;orbit.enablePan=false;orbit.minDistance=2;orbit.maxDistance=14;orbit.rotateSpeed=.65;orbit.zoomSpeed=.7;orbit.addEventListener('start',()=>{flight=null});
scene.add(new THREE.HemisphereLight(0xc8edff,0x35545b,1.1));const key=new THREE.DirectionalLight(0xfff3df,1.25);key.position.set(-3,5,6);scene.add(key);const rim=new THREE.DirectionalLight(0x72d5dc,.9);rim.position.set(4,0,-5);scene.add(rim);const fill=new THREE.PointLight(0xf4d0ff,.5);fill.position.set(-3,-2,4);scene.add(fill);
cell=new THREE.Group();scene.add(cell);
function mat(color,opts={}){const m=new THREE.MeshStandardMaterial(Object.assign({color,roughness:.48,metalness:.05},opts));materials.add(m);return m;}
function sphere(parent,position,scale,material,segments=32){const m=new THREE.Mesh(new THREE.SphereGeometry(1,segments,20),material);m.position.set(...position);m.scale.set(...scale);parent.add(m);return m;}
function tube(parent,pts,radius,material,segments=64){const c=new THREE.CatmullRomCurve3(pts.map(p=>new THREE.Vector3(...p)));const m=new THREE.Mesh(new THREE.TubeGeometry(c,segments,radius,7,false),material);parent.add(m);return m;}
function group(i){const g=new THREE.Group();g.position.set(...data[i].position);g.userData.base=g.position.clone();g.userData.index=i;g.userData.appear=0;g.visible=false;cell.add(g);parts[i]=g;return g;}
function finish(g){g.traverse(o=>{if(o.isMesh){o.userData.part=g.userData.index;pickables.push(o)}})}
membrane=new THREE.Group();cell.add(membrane);
const shell=mat('#a1dbd0',{transparent:true,opacity:.10,roughness:.2,metalness:.12,side:THREE.BackSide,depthWrite:false});sphere(membrane,[0,0,0],[2.92,2.27,2.08],shell,64);
const outer=mat('#a8e9d9',{transparent:true,opacity:.055,roughness:.3,side:THREE.FrontSide,depthWrite:false});sphere(membrane,[0,0,0],[2.95,2.3,2.11],outer,64);
// Fine contours make the transparent boundary legible without hiding its contents.
const contourMat=new THREE.LineBasicMaterial({color:0x89c8be,transparent:true,opacity:.24,depthWrite:false});materials.add(contourMat);
for(let k=0;k<3;k++){const pts=[];for(let i=0;i<=160;i++){let a=i/160*Math.PI*2;pts.push(new THREE.Vector3(2.95*Math.cos(a),2.3*Math.sin(a)*Math.cos(k*.28),2.11*Math.sin(a)*Math.sin(k*.28)));}membrane.add(new THREE.Line(new THREE.BufferGeometry().setFromPoints(pts),contourMat));}
const seed=(()=>{let s=813;return()=>{s=(s*1664525+1013904223)>>>0;return s/4294967296}})();
const poreMat=mat('#c1eddf',{transparent:true,opacity:.32,depthWrite:false});for(let i=0;i<80;i++){let y=1-2*(i+.5)/80,a=i*2.399963,r=Math.sqrt(1-y*y);sphere(membrane,[2.95*r*Math.cos(a),2.3*y,2.11*r*Math.sin(a)],[.018,.018,.018],poreMat,8)}
cutEdge=new THREE.Group();cutEdge.visible=false;cell.add(cutEdge);const ringPts=[];for(let i=0;i<=180;i++){let a=i/180*Math.PI*2;ringPts.push(new THREE.Vector3(2.95*Math.cos(a),2.3*Math.sin(a),.05));}cutEdge.add(new THREE.Line(new THREE.BufferGeometry().setFromPoints(ringPts),new THREE.LineBasicMaterial({color:0xb9ebd7,transparent:true,opacity:.75})));const cutSurface=new THREE.Mesh(new THREE.CircleGeometry(1,100),new THREE.MeshBasicMaterial({color:0x9bd7c0,transparent:true,opacity:.035,side:THREE.DoubleSide,depthWrite:false}));cutSurface.scale.set(2.95,2.3,1);cutSurface.position.z=.045;cutEdge.add(cutSurface);
let g=group(0);sphere(g,[0,0,0],[.81,.85,.76],mat('#9c89d6',{transparent:true,opacity:.65,roughness:.35,depthWrite:false}),48);sphere(g,[0,0,-.08],[.74,.77,.68],mat('#8c78c0',{transparent:true,opacity:.3,side:THREE.BackSide,depthWrite:false}),40);const poreN=mat('#b9a6ed');for(let i=0;i<27;i++){const y=1-2*(i+.5)/27,a=i*2.39996,r=Math.sqrt(1-y*y);sphere(g,[.81*r*Math.cos(a),.85*y,.76*r*Math.sin(a)],[.04,.04,.025],poreN,10)}finish(g);
g=group(1);sphere(g,[0,0,0],[.28,.26,.27],mat('#db98cf',{roughness:.4}),36);const grain=mat('#f1c7df');for(let i=0;i<14;i++){let a=seed()*6.28,y=seed()*2-1,r=Math.sqrt(1-y*y);sphere(g,[r*Math.cos(a)*.27,y*.25,r*Math.sin(a)*.26],[.032,.032,.032],grain,8)}finish(g);
// Layered, rippling ribbons represent the folded membranes of the rough ER.
g=group(2);const roughMat=mat('#658ecc',{side:THREE.DoubleSide,roughness:.5});const roughEdge=mat('#a1bff1',{roughness:.4});const roughDots=mat('#e9d3a4');for(let layer=0;layer<5;layer++){const verts=[],indices=[],edge=[];for(let j=0;j<=72;j++){let a=.39*Math.PI+j/72*1.38*Math.PI;let r=1.04+layer*.085+.045*Math.sin(a*7+layer);let x=Math.cos(a)*r,y=Math.sin(a)*r*.78,z=(layer-2)*.18-.07;for(let s=-1;s<=1;s+=2){verts.push(x,y,z+s*.14+.035*Math.sin(a*8+layer))}edge.push([x,y,z+.14+.035*Math.sin(a*8+layer)]);if(j<72){let k=j*2;indices.push(k,k+1,k+2,k+1,k+3,k+2)}if(j%5===0)sphere(g,[x,y,z+.17],[.029,.029,.029],roughDots,8);}const geo=new THREE.BufferGeometry();geo.setAttribute('position',new THREE.Float32BufferAttribute(verts,3));geo.setIndex(indices);geo.computeVertexNormals();g.add(new THREE.Mesh(geo,roughMat));tube(g,edge,.018,roughEdge,80)}finish(g);
g=group(3);const smoothMat=mat('#66bba6',{roughness:.42});for(let j=0;j<5;j++){let pts=[];for(let i=0;i<14;i++){let t=i/13;pts.push([-.55+t*1.15,.20*Math.sin(t*Math.PI*3+j*.6)+(j-2)*.16,.17*Math.cos(t*Math.PI*3+j*.9)])}tube(g,pts,.072,smoothMat,55)}tube(g,[[-.52,-.35,0],[-.65,-.1,.2],[-.55,.25,.13],[-.35,.42,-.05]],.065,smoothMat);finish(g);
g=group(4);const riboMat=mat('#eed19b',{roughness:.6});for(let i=0;i<75;i++){let p;do{p=[(seed()-.5)*4.5,(seed()-.5)*3.5,(seed()-.5)*2.6]}while((p[0]*p[0]/6+p[1]*p[1]/3.4+p[2]*p[2]/2)>1||Math.hypot(p[0]+.65,p[1]-.3,p[2]+.35)<1);sphere(g,[p[0]-.1,p[1]-.86,p[2]-.4],[.037,.031,.035],riboMat,8)}finish(g);
g=group(5);const golgiMat=mat('#d58ba5',{roughness:.43});const golgiEdge=mat('#f5bdc9');for(let j=0;j<6;j++){let pts=[];for(let i=0;i<=18;i++){let t=i/18,wide=.58-Math.abs(j-2)*.055;pts.push([(t*2-1)*wide,(j-2.5)*.16+.14*Math.pow(t*2-1,2),Math.sin(t*Math.PI)*.26+.025*Math.cos(t*12+j)])}const m=tube(g,pts,.082,golgiMat,40);m.scale.z=.7;tube(g,pts.map(p=>[p[0],p[1]+.04,p[2]+.045]),.018,golgiEdge,40)}for(let i=0;i<6;i++)sphere(g,[.6+seed()*.2,(seed()-.5)*.9,(seed()-.5)*.4],[.07,.07,.07],golgiMat,16);finish(g);
g=group(6);const mitoMat=mat('#e59b73',{roughness:.48});const foldMat=mat('#af5d45',{roughness:.6});const mitoPositions=[[0,0,0],[-2.97,-2.12,.35],[-.25,-2.22,-.65]];for(let n=0;n<3;n++){const m=new THREE.Group();m.position.set(...mitoPositions[n]);m.rotation.z=[-.45,.5,-.5][n];g.add(m);sphere(m,[0,0,0],[.56,.25,.23],mitoMat,36);const pts=[];for(let k=0;k<=45;k++){let t=k/45;pts.push([-.42+t*.84,.13*Math.sin(t*Math.PI*9),.196+.013*Math.cos(t*Math.PI*9)])}tube(m,pts,.027,foldMat,90);tube(m,[[-.46,0,.13],[-.3,.18,.14],[0,.20,.13],[.3,.18,.14],[.46,0,.13]],.019,mat('#ffd3aa'),40)}finish(g);
g=group(7);const lyso=mat('#c6c571',{roughness:.5});for(const p of [[0,0,0],[-2.55,1.7,-.68],[1.65,1.63,-.8]]){sphere(g,p,[.19,.19,.19],lyso,24);for(let i=0;i<7;i++){let a=i/7*6.28;sphere(g,[p[0]+Math.cos(a)*.105,p[1]+Math.sin(a)*.1,p[2]+.125],[.025,.025,.025],mat('#ece6a9'),8)}}finish(g);
g=group(8);const vesicleMat=mat('#8dc9d9',{transparent:true,opacity:.5,roughness:.2,depthWrite:false});for(const p of [[0,0,0],[-.1,.42,-.3],[-.45,-.5,.2],[-3.7,1.25,-.8],[.15,1.2,-.65]]){let s=.11+seed()*.065;sphere(g,p,[s,s,s],vesicleMat,22);sphere(g,[p[0]-.02,p[1]+.02,p[2]],[s*.44,s*.44,s*.44],mat('#b5e8ed'),16)}finish(g);
g=group(9);const centMat=mat('#dfb76f',{roughness:.5});const endMat=mat('#f4d794');for(let n=0;n<2;n++){const bundle=new THREE.Group();bundle.position.set(n*.24-.12,n*.18-.08,n?-.15:0);bundle.rotation.z=n?1.05:-.3;bundle.rotation.x=n?.3:0;g.add(bundle);for(let j=0;j<9;j++){let a=j/9*Math.PI*2;const rod=new THREE.Mesh(new THREE.CylinderGeometry(.034,.034,.48,9),centMat);rod.position.set(Math.cos(a)*.12,0,Math.sin(a)*.12);bundle.add(rod);sphere(bundle,[Math.cos(a)*.12,.25,Math.sin(a)*.12],[.035,.025,.035],endMat,8)}}finish(g);
g=group(10);const fiber=mat('#79b6ab',{transparent:true,opacity:.34,roughness:.5,depthWrite:false});for(let j=0;j<15;j++){let a=j/15*Math.PI*2;let pts=[];for(let k=0;k<=14;k++){let t=k/14,ang=a+t*.65;pts.push([Math.cos(ang)*2.65*Math.sin(Math.PI*t),-1.98+t*3.96,Math.sin(ang)*1.7*Math.sin(Math.PI*t)])}tube(g,pts,.014,fiber,45)}for(let j=0;j<5;j++){let pts=[];for(let k=0;k<=20;k++){let a=k/20*Math.PI*2;let y=(j-2)*.68,r=Math.sqrt(1-y*y/(2.27*2.27));pts.push([2.75*r*Math.cos(a),y+.08*Math.sin(a*3),1.88*r*Math.sin(a)])}tube(g,pts,.012,fiber,65)}finish(g);
function resize(){const w=$('stage').clientWidth,h=$('stage').clientHeight;renderer.setSize(w,h,false);camera.aspect=w/h;camera.fov=w<480?49:39;camera.updateProjectionMatrix()}
new ResizeObserver(resize).observe($('stage'));resize();
const ray=new THREE.Raycaster(),mouse=new THREE.Vector2();let pointerStart=null,dragged=false,pointerCount=0;
$('canvas').addEventListener('pointerdown',e=>{pointerCount++;if(pointerCount>1)dragged=true;else{pointerStart={x:e.clientX,y:e.clientY,time:performance.now()};dragged=false}});
$('canvas').addEventListener('pointermove',e=>{if(pointerStart&&Math.hypot(e.clientX-pointerStart.x,e.clientY-pointerStart.y)>7)dragged=true});
$('canvas').addEventListener('pointercancel',()=>{pointerStart=null;pointerCount=0;dragged=true});
$('canvas').addEventListener('pointerup',e=>{pointerCount=Math.max(0,pointerCount-1);if(!pointerStart||dragged||performance.now()-pointerStart.time>600){if(!pointerCount)pointerStart=null;return}const rect=$('canvas').getBoundingClientRect();mouse.set((e.clientX-rect.left)/rect.width*2-1,-(e.clientY-rect.top)/rect.height*2+1);ray.setFromCamera(mouse,camera);const hits=ray.intersectObjects(pickables,false).filter(hit=>{const idx=hit.object.userData.part;return idx<count&&parts[idx].visible&&(!cut||hit.point.z<=.055)});if(hits.length){pause();focusPart(hits[0].object.userData.part)}pointerStart=null;});
function runDemo(ms){if(!demoStart)demoStart=ms;const time=(ms-demoStart)/1000,cycle=Math.floor(time/20),t=time%20;let event=t<8.8?Math.floor(t/.8):t<10?11:t<12?12:t<15?13:t<17?14:t<18.3?15:16;if(cycle!==demoCycle){demoCycle=cycle;demoEvent=-1;setCut(false);setBuild(0);resetView()}if(event!==demoEvent){demoEvent=event;if(event<=11)setBuild(event);else if(event===12)focusPart(6);else if(event===13){setCut(true);focusPart(5)}else if(event===14)focusPart(0);else if(event===15){setBuild(5);resetView()}else{setBuild(11);setCut(false);resetView()}}if(t>8.8&&t<10&&!flight){const r=camera.position.clone().sub(orbit.target);r.applyAxisAngle(new THREE.Vector3(0,1,0),.006);camera.position.copy(orbit.target).add(r)}}
let lastFrame=0;function animate(ms){requestAnimationFrame(animate);const dt=Math.min((ms-lastFrame)/1000,.05);lastFrame=ms;if(demo)runDemo(ms);else if(playing&&ms-lastBuild>2350){lastBuild=ms;setBuild(count+1)}const t=ms/1000;parts.forEach((p,i)=>{if(!p.visible)return;const base=p.userData.base,amount=reduced?0:1;p.position.copy(base);if(i!==0&&i!==1&&i!==2&&i!==10){p.position.y+=Math.sin(t*.5+i*1.7)*.035*amount;p.position.x+=Math.cos(t*.4+i)*.025*amount;p.rotation.z=Math.sin(t*.3+i)*.025*amount}let age=(ms-p.userData.appear)/650;let growth=Math.min(1,Math.max(.001,age));growth=1-Math.pow(1-growth,3);let pulse=1+Math.sin(t*.9+i)*.012*amount;p.scale.setScalar(growth*pulse)});membrane.scale.setScalar(1+Math.sin(t*.65)*.003*(reduced?0:1));if(flight){let x=Math.min(1,(ms-flight.start)/flight.duration),e=x*x*(3-2*x);camera.position.lerpVectors(flight.from,flight.to,e);orbit.target.lerpVectors(flight.targetFrom,flight.targetTo,e);if(x===1)flight=null}orbit.update();scene.updateMatrixWorld();const label=$('floatingLabel');let pos;if(selected<0){pos=new THREE.Vector3(2.08,.8,.6)}else if(parts[selected]){pos=parts[selected].position.clone();if(selected===4)pos.set(.45,1.2,.4);if(selected===10)pos.set(1.8,-1.3,.2);pos.x+=selected===0?.5:selected===2?-.7:.12;}else pos=new THREE.Vector3();pos.project(camera);let w=$('stage').clientWidth,h=$('stage').clientHeight,lx=(pos.x*.5+.5)*w+10,ly=(-pos.y*.5+.5)*h;label.style.left=Math.max(15,Math.min(w-(w<500?190:220),lx))+'px';label.style.top=Math.max(65,Math.min(h-100,ly))+'px';label.style.opacity=pos.z>1||pos.z< -1?'0':'1';renderer.render(scene,camera)}
lastBuild=performance.now();if(demo){$('demoBadge').classList.add('visible');pause()}setBuild(0);requestAnimationFrame(animate);
$('canvas').addEventListener('webglcontextlost',e=>{e.preventDefault();showError('The 3D view paused because the graphics connection was interrupted. Reload this page to rebuild your cell.');});
}catch(e){showError('This device could not start the 3D view. Try a browser with WebGL enabled, then reload to build your cell.');}
})();
</script>
</body>
</html>
