Skip to content
VibeCodeBench
Back to the app

The code

Build a Cell, Part by Part by Claude Haiku 5.5

Exactly as the AI wrote it. Shown as text, so nothing runs on this page.

index.html

broken · auto-checked (Checked by an automated pass. Not hand-played yet.)17.8 KB · 462 lines · loads cdnjs.cloudflare.com/ajax/libs/three.js/r150/three.min.js
Download
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Animal Cell 3D</title>
<style>
:root{--bg:#eef3fb;--panel:rgba(255,255,255,.93);--text:#0f172a;--muted:#475569;--accent:#2563eb;--btn:#dbeafe;--btnText:#0f172a}
@media (prefers-color-scheme: dark){:root:not([data-theme="light"]){--bg:#0b1020;--panel:rgba(15,23,42,.9);--text:#e5e7eb;--muted:#94a3b8;--accent:#60a5fa;--btn:#1e293b;--btnText:#e5e7eb}}
:root[data-theme="dark"]{--bg:#0b1020;--panel:rgba(15,23,42,.9);--text:#e5e7eb;--muted:#94a3b8;--accent:#60a5fa;--btn:#1e293b;--btnText:#e5e7eb}
html,body{margin:0;height:100%;background:var(--bg);color:var(--text);font-family:system-ui,-apple-system,sans-serif;overflow:hidden}
#c{position:fixed;inset:0;width:100%;height:100%;display:block;touch-action:none;outline:none}
#labels{position:fixed;inset:0;pointer-events:none;overflow:hidden}
.label{position:absolute;left:0;top:0;font:600 12px/1.2 system-ui,sans-serif;background:rgba(15,23,42,.75);color:#fff;padding:4px 8px;border-radius:999px;white-space:nowrap;opacity:0;transition:opacity .3s}
#top{position:fixed;top:0;left:0;right:0;padding:12px 16px;pointer-events:none}
#top h1{margin:0;font-size:18px}
#hint{margin:2px 0 0;font-size:13px;color:var(--muted)}
#status{font-size:14px;color:var(--muted)}
#info{position:fixed;top:72px;left:12px;right:12px;max-width:460px;margin:0 auto;background:var(--panel);border-radius:16px;padding:14px 46px 14px 16px;box-shadow:0 8px 24px rgba(0,0,0,.25)}
#info[hidden]{display:none}
#info h2{margin:0 0 6px;font-size:19px;color:var(--accent)}
#info p{margin:0;font-size:16px;line-height:1.4}
#iclose{position:absolute;top:8px;right:8px;width:48px;height:48px;border:0;border-radius:50%;background:var(--btn);color:var(--btnText);font-size:24px;cursor:pointer}
#panel{position:fixed;left:0;right:0;bottom:0;padding:12px 16px calc(16px + env(safe-area-inset-bottom));background:var(--panel);border-radius:18px 18px 0 0;display:flex;flex-direction:column;gap:10px;box-shadow:0 -6px 20px rgba(0,0,0,.15)}
#now{margin:0;font-size:15px;line-height:1.35;min-height:2.7em}
.row{display:flex;gap:10px;align-items:center;justify-content:space-between}
.btn,.switch{min-height:52px;border-radius:14px;font-size:17px;font-weight:600}
.btn{border:0;background:var(--accent);color:#fff;padding:0 22px;cursor:pointer}
.switch{display:flex;align-items:center;gap:12px;background:var(--btn);color:var(--btnText);padding:0 16px;cursor:pointer;user-select:none}
.switch input{width:26px;height:26px;margin:0;accent-color:var(--accent)}
#scrub{width:100%;height:36px;accent-color:var(--accent)}
</style>
</head>
<body>
<canvas id="c" tabindex="-1"></canvas>
<div id="labels"></div>
<div id="top">
  <h1>Animal cell</h1>
  <p id="hint">Drag to orbit - pinch or scroll to zoom - tap a part</p>
  <span id="status"></span>
</div>
<div id="info" hidden>
  <button id="iclose" aria-label="Close">&times;</button>
  <h2 id="iname"></h2>
  <p id="itext"></p>
</div>
<div id="panel">
  <p id="now"></p>
  <div class="row">
    <label class="switch"><input type="checkbox" id="cut"><span>Cut open</span></label>
    <button id="rebuild" class="btn" type="button">Rebuild</button>
  </div>
  <input id="scrub" type="range" min="0" max="1000" value="0" aria-label="Build progress">
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r150/three.min.js"></script>
<script>
(function(){
'use strict';
var $ = function(s){ return document.querySelector(s); };
var clamp = function(v,a,b){ return Math.min(b, Math.max(a, v)); };
var easeInOut = function(x){ return x < 0.5 ? 2*x*x : 1 - Math.pow(-2*x+2, 2)/2; };
var easeOutBack = function(x){ var c1=1.4, c3=c1+1; return 1 + c3*Math.pow(x-1,3) + c1*Math.pow(x-1,2); };
var HINT = 'Drag to orbit - pinch or scroll to zoom - tap a part';

if (typeof THREE === 'undefined') { $('#status').textContent = '3D library could not load. Check your connection.'; return; }

var canvas = $('#c');
var renderer;
try {
  renderer = new THREE.WebGLRenderer({canvas: canvas, antialias: true, alpha: true});
} catch (e) {
  $('#status').textContent = 'WebGL is not available on this device.';
  return;
}
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.localClippingEnabled = true;

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(50, 1, 0.1,100);
function resize(){
  var w = window.innerWidth, h = window.innerHeight;
  renderer.setSize(w, h, false);
  camera.aspect = w / h;
  camera.updateProjectionMatrix();
}
window.addEventListener('resize', resize);
resize();

scene.add(new THREE.AmbientLight(0xffffff, 0.75));
var sun = new THREE.DirectionalLight(0xffffff, 0.9);
sun.position.set(3, 5, 4);
scene.add(sun);
var fill = new THREE.PointLight(0x9ec8ff, 0.8, 20);
fill.position.set(-4, -2, 3);
scene.add(fill);

// Deterministic random so every load looks the same
var seed = 7;
function rnd(){ seed = (seed * 16807) % 2147483647; return (seed - 1) / 2147483646; }
function rr(a, b){ return a + (b - a) * rnd(); }

var R = 2.8;
var NUC = new THREE.Vector3(0.2, 0.15, 0);
var clipMats = [];
var geoCache = {};
function sphG(r){ var k = 's' + r; return geoCache[k] || (geoCache[k] = new THREE.SphereGeometry(r, 12, 10)); }
function mat(color, o){
  var m = new THREE.MeshStandardMaterial(Object.assign({color: color, roughness: 0.5, metalness: 0.05}, o || {}));
  clipMats.push(m);
  return m;
}
function add(parent, geo, m, x, y, z){
  var o = new THREE.Mesh(geo, m);
  o.position.set(x || 0, y || 0, z || 0);
  parent.add(o);
  return o;
}
function ballPoint(r){
  for (;;) {
    var p = new THREE.Vector3(rr(-1,1), rr(-1,1), rr(-1,1));
    if (p.lengthSq() <= 1) return p.multiplyScalar(r);
  }
}
function freePoint(r){
  for (var k = 0; k < 500; k++) {
    var p = ballPoint(r);
    if (p.distanceTo(NUC) > 1.35) return p;
  }
  return new THREE.Vector3(-r, 0, 0);
}

// Membrane (starts alone, see-through)
var membraneMat = mat(0x7dd3fc, {transparent: true, opacity: 0.16, depthWrite: false, side: THREE.DoubleSide, roughness: 0.2});
var membrane = add(scene, new THREE.SphereGeometry(R, 64, 40), membraneMat);
var wireMat = new THREE.MeshBasicMaterial({color: 0x7dd3fc, wireframe: true, transparent: true, opacity: 0.12, depthWrite: false});
clipMats.push(wireMat);
var wire = add(scene, new THREE.SphereGeometry(R, 24, 16), wireMat);

// Parts in build order
var PARTS = [
  {name: 'Nucleus', text: 'The control center: it holds the DNA, the cell instruction book.', c: [0.2, 0.15, 0], build: function(g){
    add(g, new THREE.SphereGeometry(1, 40, 28), mat(0x8b5cf6, {transparent: true, opacity: 0.4, depthWrite: false}));
  }},
  {name: 'Nucleolus', text: 'Inside the nucleus, it makes the parts that become ribosomes.', c: [0.4, 0.3, 0.25], build: function(g){
    add(g, sphG(0.4), mat(0xbe185d));
  }},
  {name: 'Rough ER', text: 'Studded with ribosomes, it helps fold new proteins into shape.', c: [-1.2, -0.5, 0.9], build: function(g){
    var m = mat(0x2c7fb8), dm = mat(0xf4e285);
    for (var k = 0; k < 3; k++) {
      var t = add(g, new THREE.TorusGeometry(0.35, 0.07, 10, 30, Math.PI*1.4), m, 0, k*0.22 - 0.22, 0);
      t.rotation.set(rr(0,3), rr(0,3), rr(0,3));
      for (var j = 0; j < 12; j++) {
        var a = j/12 * Math.PI*1.4;
        add(t, sphG(0.035), dm, 0.35*Math.cos(a), 0.35*Math.sin(a), 0.09);
      }
    }
  }},
  {name: 'Smooth ER', text: 'A network of smooth tubes that makes fats and cleans up toxins.', c: [1.3, 0.9, -0.8], build: function(g){
    var m = mat(0x7ee08f);
    for (var k = 0; k < 3; k++) {
      var pts = [];
      for (var j = 0; j < 5; j++) pts.push(new THREE.Vector3(rr(-0.7,0.7), rr(-0.5,0.5), rr(-0.5,0.5)));
      add(g, new THREE.TubeGeometry(new THREE.CatmullRomCurve3(pts), 30, 0.06, 8), m);
    }
  }},
  {name: 'Ribosomes', text: 'Tiny factories that read the DNA instructions and build proteins.', c: [0, 0, 0], build: function(g){
    var m = mat(0xf4e285);
    for (var j = 0; j < 70; j++) { var p = freePoint(2.3); add(g, sphG(0.05), m, p.x, p.y, p.z); }
  }},
  {name: 'Golgi apparatus', text: 'It packs, labels and ships proteins to where they need to go.', c: [-0.8, 0.9, -0.8], build: function(g){
    var m = mat(0xff9f43);
    for (var k = 0; k < 5; k++) {
      var t = add(g, new THREE.TorusGeometry(0.28, 0.04, 8, 28, Math.PI*1.2), m, 0, k*0.09 - 0.18, 0);
      t.rotation.set(Math.PI/2, 0.25*k, 0);
    }
    var vm = mat(0xffd08a);
    for (var j = 0; j < 4; j++) add(g, sphG(0.05), vm, rr(-0.4,0.4), rr(0.1,0.3), rr(-0.4,0.4));
  }},
  {name: 'Mitochondria', text: 'They turn food into usable energy, like tiny power plants.', c: [0, 0, 0], build: function(g){
    var m = mat(0xff6b6b, {roughness: 0.4});
    for (var j = 0; j < 6; j++) {
      var p = freePoint(1.9);
      var cap = add(g, new THREE.CapsuleGeometry(0.1, 0.35, 6, 12), m, p.x, p.y, p.z);
      cap.rotation.set(rr(0,3), rr(0,3), rr(0,3));
    }
  }},
  {name: 'Lysosomes', text: 'The recycling center: they break down leftovers and worn-out parts.', c: [0, 0, 0], build: function(g){
    var m = mat(0x2ecc71);
    for (var j = 0; j < 6; j++) { var p = freePoint(2.2); add(g, sphG(0.12), m, p.x, p.y, p.z); }
  }},
  {name: 'Vesicles', text: 'Little bubbles that carry cargo from one place to another.', c: [0, 0, 0], build: function(g){
    var m = mat(0xa5f3fc, {transparent: true, opacity: 0.6});
    for (var j = 0; j < 10; j++) { var p = freePoint(2.2); add(g, sphG(0.075), m, p.x, p.y, p.z); }
  }},
  {name: 'Centrioles', text: 'A pair of rods that help pull chromosomes apart when a cell divides.', c: [1.1, -0.7, 0.6], build: function(g){
    var m = mat(0xfbbf24), geo = new THREE.CylinderGeometry(0.05, 0.05, 0.4, 10);
    for (var k = 0; k < 2; k++) {
      var pair = new THREE.Group();
      pair.rotation.set(k*Math.PI/2, 0, 0);
      add(pair, geo, m, -0.07, 0, 0);
      add(pair, geo, m, 0.07, 0, 0);
      g.add(pair);
    }
  }},
  {name: 'Cytoskeleton', text: 'A web of protein threads that gives the cell its shape and lets it move.', c: [0, 0, 0], build: function(g){
    var m = mat(0xe2e8f0, {transparent: true, opacity: 0.55});
    for (var k = 0; k < 16; k++) {
      var pts = [];
      for (var j = 0; j < 5; j++) pts.push(freePoint(2.4));
      add(g, new THREE.TubeGeometry(new THREE.CatmullRomCurve3(pts), 30, 0.02, 6, false), m);
    }
  }}
];

var N = PARTS.length;
var partsRoot = new THREE.Group();
scene.add(partsRoot);
var parts = PARTS.map(function(d, i){
  var g = new THREE.Group();
  g.position.set(d.c[0], d.c[1], d.c[2]);
  g.userData.idx = i;
  g.scale.setScalar(0.0001);
  d.build(g);
  partsRoot.add(g);
  return {
    i: i, name: d.name, text: d.text, group: g,
    c: new THREE.Vector3(d.c[0], d.c[1], d.c[2]),
    start: i / N, end: (i + 0.85) / N, phase: i * 1.7, grow: 0, label: null
  };
});

// Labels
var labelBox = $('#labels');
parts.forEach(function(p){
  var d = document.createElement('div');
  d.className = 'label';
  d.textContent = p.name;
  labelBox.appendChild(d);
  p.label = d;
});

// Cut open
var cut = false;
var cutPlane = new THREE.Plane(new THREE.Vector3(1, 0, 0), 0);
function setCut(v){
  cut = v;
  clipMats.forEach(function(m){
    m.clippingPlanes = v ? [cutPlane] : null;
    m.needsUpdate = true;
  });
  $('#cut').checked = v;
}

// Info card
var infoShown = -1;
function showInfo(p){
  infoShown = p.i;
  $('#iname').textContent = p.name;
  $('#itext').textContent = p.text;
  $('#info').hidden = false;
}
function hideInfo(){
  infoShown = -1;
  $('#info').hidden = true;
}
$('#iclose').addEventListener('click', hideInfo);

// Build progress
var progress = 0, tween = null;
function startBuild(ms){ tween = {t0: performance.now(), dur: ms}; progress = 0; }
$('#rebuild').addEventListener('click', function(){ startBuild(14000); });
$('#scrub').addEventListener('input', function(e){ tween = null; progress = e.target.value / 1000; });
$('#cut').addEventListener('change', function(e){ setCut(e.target.checked); });

// Demo mode (#demo at the end of the URL): hands-free 20 s loop
var demoOn = /#demo$/i.test(window.location.href);
var demoT0 = performance.now(), demoLast = -1;
var stages = [
  {t: 9,    fn: function(){ setCut(true); }},
  {t: 11.5, fn: function(){ setCut(false); flyTo(parts[0]); }},
  {t: 14,   fn: function(){ flyTo(parts[6]); }},
  {t: 16.5, fn: function(){ flyTo(parts[7]); }},
  {t: 18.5, fn: function(){ flyHome(); }}
];
if (demoOn) $('#hint').textContent = 'Demo running - touch the screen to take over';
function stopDemo(){
  if (!demoOn) return;
  demoOn = false;
  $('#hint').textContent = HINT;
}
window.addEventListener('pointerdown', stopDemo, true);

// Orbit / zoom / fly state
var theta = 0.6, phi = 1.25, radius = 9;
var target = new THREE.Vector3();
var tmpV = new THREE.Vector3();
var fly = null;
function flyTo(p){
  fly = {p: p, t0: performance.now(), dur: 1000, fromT: target.clone(), fromR: radius, toR: 4.4};
  showInfo(p);
}
function flyHome(){
  fly = {p: null, t0: performance.now(), dur: 1200, fromT: target.clone(), fromR: radius, toR: 9};
  hideInfo();
}

// Pointers: one finger orbits, two fingers pinch, a tap flies to a part
var ptrs = new Map();
var dragMoved = false, downAt = 0, downX = 0, downY = 0, pinchD = 0;
function pinchDist(){
  var v = Array.from(ptrs.values());
  return Math.hypot(v[0].x - v[1].x, v[0].y - v[1].y);
}
canvas.addEventListener('pointerdown', function(e){
  try { canvas.setPointerCapture(e.pointerId); } catch (err) {}
  ptrs.set(e.pointerId, {x: e.clientX, y: e.clientY});
  if (ptrs.size === 1) {
    dragMoved = false; downAt = performance.now(); downX = e.clientX; downY = e.clientY;
    fly = null;
  } else if (ptrs.size === 2) {
    pinchD = pinchDist(); dragMoved = true;
  }
});
canvas.addEventListener('pointermove', function(e){
  var p = ptrs.get(e.pointerId);
  if (!p) return;
  var dx = e.clientX - p.x, dy = e.clientY - p.y;
  p.x = e.clientX; p.y = e.clientY;
  if (ptrs.size === 1) {
    if (Math.hypot(e.clientX - downX, e.clientY - downY) > 8) dragMoved = true;
    if (dragMoved) {
      theta -= dx * 0.007;
      phi = clamp(phi - dy * 0.007, 0.2, Math.PI - 0.2);
    }
  } else if (ptrs.size === 2) {
    var d = pinchDist();
    if (pinchD > 0 && d > 0) radius = clamp(radius * pinchD / d, 3.5, 14);
    pinchD = d;
  }
});
var ray = new THREE.Raycaster(), ndc = new THREE.Vector2();
function tap(x, y){
  var r = canvas.getBoundingClientRect();
  ndc.set((x - r.left) / r.width * 2 - 1, -((y - r.top) / r.height) * 2 + 1);
  ray.setFromCamera(ndc, camera);
  var hits = ray.intersectObjects(partsRoot.children, true);
  for (var i = 0; i < hits.length; i++) {
    var o = hits[i].object;
    while (o && o.userData.idx === undefined) o = o.parent;
    if (o) {
      var p = parts[o.userData.idx];
      if (p.grow > 0.5) { flyTo(p); return; }
    }
  }
  hideInfo();
}
function release(e){
  var single = ptrs.size === 1;
  ptrs.delete(e.pointerId);
  if (e.type === 'pointerup' && single && !dragMoved && performance.now() - downAt < 600) tap(e.clientX, e.clientY);
  if (ptrs.size === 0) dragMoved = false;
}
canvas.addEventListener('pointerup', release);
canvas.addEventListener('pointercancel', release);
canvas.addEventListener('wheel', function(e){
  e.preventDefault();
  radius = clamp(radius * (1 + e.deltaY * 0.0015), 3.5, 14);
}, {passive: false});

// Demo driver
function demoUpdate(now){
  var tt = ((now - demoT0) / 1000) % 20;
  if (tt < demoLast) setCut(false);
  for (var i = 0; i < stages.length; i++) {
    if (demoLast < stages[i].t && tt >= stages[i].t) stages[i].fn();
  }
  demoLast = tt;
  progress = easeInOut(clamp(tt / 7, 0, 1));
}

var lastCaption = '';
function frame(now){
  var t = now / 1000;
  if (demoOn) {
    demoUpdate(now);
    theta += 0.006;
  } else {
    if (tween) {
      var x0 = clamp((now - tween.t0) / tween.dur, 0, 1);
      progress = easeInOut(x0);
      if (x0 >= 1) tween = null;
    }
    if (ptrs.size === 0 && !fly) theta += 0.0012;
  }

  if (fly) {
    var x = clamp((now - fly.t0) / fly.dur, 0, 1), e = easeInOut(x);
    if (fly.p) fly.p.group.getWorldPosition(tmpV); else tmpV.set(0, 0, 0);
    target.lerpVectors(fly.fromT, tmpV, e);
    radius = fly.fromR + (fly.toR - fly.fromR) * e;
    if (x >= 1) fly = null;
  }

  var sp = Math.sin(phi);
  camera.position.set(
    target.x + radius * sp * Math.sin(theta),
    target.y + radius * Math.cos(phi),
    target.z + radius * sp * Math.cos(theta)
  );
  camera.lookAt(target);

  var pulse = 1 + 0.012 * Math.sin(t * 0.9);
  membrane.scale.setScalar(pulse);
  wire.scale.setScalar(pulse);

  var building = null;
  parts.forEach(function(p){
    var x = clamp((progress - p.start) / (p.end - p.start), 0, 1);
    p.grow = x;
    if (x > 0 && x < 1 && !building) building = p;
    var s = Math.max(0.0001, easeOutBack(x)) * (1 + 0.03 * Math.sin(t * 1.6 + p.phase));
    p.group.scale.setScalar(s);
    p.group.position.set(
      p.c.x + 0.05 * Math.sin(t * 0.5 + p.phase),
      p.c.y + 0.05 * Math.cos(t * 0.45 + p.phase * 1.3),
      p.c.z + 0.05 * Math.sin(t * 0.4 + p.phase * 0.7)
    );
    p.group.rotation.y = 0.3 * Math.sin(t * 0.3 + p.phase);
    p.group.visible = x > 0;
  });

  var cap;
  if (building) cap = 'Building: ' + building.name + '. ' + building.text;
  else if (progress <= 0) cap = 'Starting with an empty membrane...';
  else cap = 'Complete cell. Tap any part to learn what it does.';
  if (cap !== lastCaption) { $('#now').textContent = cap; lastCaption = cap; }

  var w = window.innerWidth, h = window.innerHeight;
  parts.forEach(function(p){
    p.group.getWorldPosition(tmpV);
    tmpV.y += 0.35;
    tmpV.project(camera);
    var sx = (tmpV.x + 1) / 2 * w, sy = (1 - tmpV.y) / 2 * h;
    var show = tmpV.z < 1 && p.grow > 0.6;
    p.label.style.transform = 'translate(' + sx.toFixed(1) + 'px,' + sy.toFixed(1) + 'px) translate(-50%,-50%)';
    p.label.style.opacity = show ? '1' : '0';
  });

  var sv = Math.round(progress * 1000);
  if (Number($('#scrub').value) !== sv) $('#scrub').value = sv;

  renderer.render(scene, camera);
  requestAnimationFrame(frame);
}

setCut(false);
if (!demoOn) startBuild(24000);
requestAnimationFrame(frame);
})();
</script>
</body>
</html>