Skip to content
VibeCodeBench
Back to the app

The code

Jelly Orange Slice by Claude Haiku 5.5

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.)16.5 KB · 405 lines · loads unpkg.com/three@0.160.0/build/three.module.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>Jelly Orange Slice</title>
<style>
:root{--bg:#f3ede2;--ink:#3a2a1a;--accent:#ff9a1f;--panel:rgba(255,255,255,.7);}
@media (prefers-color-scheme: dark){:root:not([data-theme="light"]){--bg:#1d1812;--ink:#f5e8d6;--accent:#ff9a1f;--panel:rgba(40,30,22,.75);}}
:root[data-theme="dark"]{--bg:#1d1812;--ink:#f5e8d6;--accent:#ff9a1f;--panel:rgba(40,30,22,.75);}
html,body{margin:0;height:100%;overflow:hidden;background:var(--bg);color:var(--ink);font-family:system-ui,-apple-system,"Segoe UI",Roboto,sans-serif;}
#c{position:fixed;inset:0;width:100%;height:100%;display:block;touch-action:none;}
#hint{position:fixed;top:12px;left:0;right:0;text-align:center;font-size:15px;font-weight:600;opacity:.85;pointer-events:none;padding:0 16px;}
#ui{position:fixed;left:0;right:0;bottom:0;padding:12px 14px calc(14px + env(safe-area-inset-bottom));display:flex;flex-wrap:wrap;gap:10px;align-items:center;justify-content:center;background:linear-gradient(to top,var(--bg) 60%,transparent);}
button{min-height:54px;min-width:120px;padding:0 22px;border:0;border-radius:16px;background:var(--accent);color:#2b1600;font:700 18px system-ui,sans-serif;cursor:pointer;box-shadow:0 4px 0 #b55f0c;}
button:active{transform:translateY(2px);box-shadow:0 2px 0 #b55f0c;}
#fw{display:flex;align-items:center;gap:10px;min-height:54px;padding:0 16px;border-radius:16px;background:var(--panel);font-weight:600;}
#fz{width:160px;height:36px;accent-color:var(--accent);}
</style>
</head>
<body>
<canvas id="c"></canvas>
<div id="hint">Drag to stretch, tap to poke, Squeeze for juice</div>
<div id="ui">
  <button id="squeeze" type="button">Squeeze</button>
  <label id="fw">Firmness <input id="fz" type="range" min="0" max="100" value="55"></label>
  <button id="reset" type="button">Reset</button>
</div>
<script type="module">
import * as THREE from 'https://unpkg.com/three@0.160.0/build/three.module.js';

// ---------- Soft body layout: H vertical layers, R rings, S segments around ----------
const H = 3, R = 6, S = 30;
const NC = H + 1;                       // one centre particle per layer
const NV = NC + (H + 1) * R * S;        // total particles = vertices
const TH = 0.36;                        // slice thickness
const yOf = l => 0.03 + l * (TH / H);
const vid = (l, r, j) => {
  if (r === 0) return l;
  j = ((j % S) + S) % S;
  return NC + (l * R + (r - 1)) * S + j;
};

const P = new Float32Array(NV * 3);     // current positions (also the render attribute)
const Q = new Float32Array(NV * 3);     // previous positions (Verlet)
const GS = new Float32Array(NV * 3);    // positions when a grab started
const W = new Float32Array(NV);         // interaction weights (poke / grab)
const REST = new Float32Array(NV * 3);  // rest shape

for (let l = 0; l <= H; l++) {
  REST.set([0, yOf(l), 0], vid(l, 0, 0) * 3);
  for (let r = 1; r <= R; r++) for (let j = 0; j < S; j++) {
    const a = j / S * Math.PI * 2, rad = r / R;
    REST.set([Math.cos(a) * rad, yOf(l), Math.sin(a) * rad], vid(l, r, j) * 3);
  }
}

// ---------- Springs (rest lengths from the rest shape) ----------
const CA = [], CB = [], CR = [];
const addC = (a, b) => {
  if (a === b) return;
  const a3 = a * 3, b3 = b * 3;
  CA.push(a); CB.push(b);
  CR.push(Math.hypot(REST[b3] - REST[a3], REST[b3 + 1] - REST[a3 + 1], REST[b3 + 2] - REST[a3 + 2]));
};
for (let l = 0; l <= H; l++) {
  if (l < H) addC(vid(l, 0, 0), vid(l + 1, 0, 0));
  for (let j = 0; j < S; j++) addC(vid(l, 0, 0), vid(l, 1, j));
  for (let r = 1; r <= R; r++) for (let j = 0; j < S; j++) {
    const a = vid(l, r, j);
    addC(a, vid(l, r, j + 1));                       // ring
    addC(a, vid(l, r, j + 2));                       // ring bending
    if (r < R) { addC(a, vid(l, r + 1, j)); addC(a, vid(l, r + 1, j + 1)); } // radial + shear
    if (r + 2 <= R) addC(a, vid(l, r + 2, j));       // radial bending
    if (l < H) { addC(a, vid(l + 1, r, j)); addC(a, vid(l + 1, r, j + 1)); } // vertical + shear
  }
}
const NCON = CA.length;
const CAi = Int32Array.from(CA), CBi = Int32Array.from(CB), CRf = Float32Array.from(CR);

// ---------- Triangles (outer skin: top cap, bottom cap, side wall) ----------
const IDX = [];
for (let j = 0; j < S; j++) {
  // top cap (faces up)
  IDX.push(vid(H, 0, 0), vid(H, 1, j + 1), vid(H, 1, j));
  // bottom cap (faces down)
  IDX.push(vid(0, 0, 0), vid(0, 1, j), vid(0, 1, j + 1));
}
for (let r = 1; r < R; r++) for (let j = 0; j < S; j++) {
  const a = vid(H, r, j), b = vid(H, r, j + 1), c = vid(H, r + 1, j + 1), d = vid(H, r + 1, j);
  IDX.push(a, b, c, a, c, d);
  const a0 = vid(0, r, j), b0 = vid(0, r, j + 1), c0 = vid(0, r + 1, j + 1), d0 = vid(0, r + 1, j);
  IDX.push(a0, c0, b0, a0, d0, c0);
}
for (let l = 0; l < H; l++) for (let j = 0; j < S; j++) {
  const a = vid(l, R, j), b = vid(l, R, j + 1), c = vid(l + 1, R, j + 1), d = vid(l + 1, R, j);
  IDX.push(a, d, c, a, c, b);
}

// ---------- Vertex colours: pulp, membranes (white), pith, peel ----------
const COL = new Float32Array(NV * 3);
const hash = (l, r, j) => { const x = Math.sin(l * 12.9898 + r * 78.233 + j * 37.719) * 43758.5453; return x - Math.floor(x); };
for (let l = 0; l <= H; l++) for (let r = 0; r <= R; r++) {
  const jn = r === 0 ? 1 : S;
  for (let j = 0; j < jn; j++) {
    const i = vid(l, r, j) * 3, n = hash(l, r, j);
    let cr, cg, cb;
    if (r === 0) { cr = 1; cg = 0.78; cb = 0.4; }
    else if (r === R) { cr = 0.86; cg = 0.36; cb = 0.06; }                 // bright peel
    else if (r === R - 1) { cr = 1; cg = 0.96; cb = 0.88; }                // thin pith
    else if (j % 3 === 0) { cr = 1; cg = 0.92; cb = 0.72; }                // membranes between 10 segments
    else { const k = 0.85 + 0.25 * n; cr = k; cg = (0.56 + 0.14 * n) * k; cb = (0.1 + 0.1 * n) * k; } // juice cells
    COL.set([cr, cg, cb], i);
  }
}

// ---------- three.js scene ----------
const canvas = document.getElementById('c');
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
const dark = window.matchMedia && matchMedia('(prefers-color-scheme: dark)').matches;
const scene = new THREE.Scene();
scene.background = new THREE.Color(dark ? 0x1d1812 : 0xf3ede2);
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 100);

scene.add(new THREE.AmbientLight(0xffffff, 0.55));
const sun = new THREE.DirectionalLight(0xffffff, 1.1); sun.position.set(2, 4, 3); scene.add(sun);
scene.add(new THREE.HemisphereLight(0xfff5e6, 0x886644, 0.4));
const glow = new THREE.PointLight(0xffa040, 0.9, 3); glow.position.set(0, 0.18, 0); scene.add(glow);

// White plate
const plate = new THREE.Mesh(
  new THREE.CylinderGeometry(1.75, 1.8, 0.08, 72),
  new THREE.MeshStandardMaterial({ color: 0xfafafa, roughness: 0.25 })
);
plate.position.y = -0.04; plate.receiveShadow = true; scene.add(plate);

// Soft contact shadow under the jelly
const shCanvas = document.createElement('canvas'); shCanvas.width = shCanvas.height = 128;
const shCtx = shCanvas.getContext('2d');
const shGrad = shCtx.createRadialGradient(64, 64, 0, 64, 64, 64);
shGrad.addColorStop(0, 'rgba(90,50,20,0.45)'); shGrad.addColorStop(0.6, 'rgba(90,50,20,0.22)'); shGrad.addColorStop(1, 'rgba(90,50,20,0)');
shCtx.fillStyle = shGrad; shCtx.fillRect(0, 0, 128, 128);
const shadow = new THREE.Mesh(new THREE.PlaneGeometry(3, 3),
  new THREE.MeshBasicMaterial({ map: new THREE.CanvasTexture(shCanvas), transparent: true, depthWrite: false }));
shadow.rotation.x = -Math.PI / 2; shadow.position.y = 0.003; scene.add(shadow);

// Jelly mesh (its vertices are the particles)
const geo = new THREE.BufferGeometry();
geo.setAttribute('position', new THREE.BufferAttribute(P, 3));
geo.setAttribute('color', new THREE.BufferAttribute(COL, 3));
geo.setIndex(IDX);
const jellyMat = new THREE.MeshPhysicalMaterial({
  vertexColors: true, transmission: 0.5, thickness: 0.5, roughness: 0.2,
  clearcoat: 1, clearcoatRoughness: 0.1, ior: 1.35,
  emissive: new THREE.Color(0x331500), emissiveIntensity: 0.25,
  side: THREE.DoubleSide
});
const jelly = new THREE.Mesh(geo, jellyMat);
scene.add(jelly);

// Juice drops
const dropGeo = new THREE.SphereGeometry(0.035, 12, 8);
const dropMat = new THREE.MeshPhysicalMaterial({ color: 0xffa030, transmission: 0.4, roughness: 0.05, thickness: 0.2 });
const drops = [];
function spawnDrop(j) {
  const i = vid(H, R, j) * 3;
  const x = P[i], y = P[i + 1], z = P[i + 2];
  const r = Math.hypot(x, z) || 1;
  const m = new THREE.Mesh(dropGeo, dropMat);
  m.position.set(x + x / r * 0.03, y, z + z / r * 0.03);
  scene.add(m);
  drops.push({ m, vx: x / r * 0.5, vy: 0.3, vz: z / r * 0.5, life: 0, landed: false });
}
function updateDrops(dt) {
  for (let k = drops.length - 1; k >= 0; k--) {
    const d = drops[k];
    if (!d.landed) {
      d.vy -= 9.8 * dt;
      d.m.position.x += d.vx * dt; d.m.position.y += d.vy * dt; d.m.position.z += d.vz * dt;
      if (d.m.position.y <= 0.03) { d.m.position.y = 0.03; d.landed = true; d.vx = d.vz = 0; }
    } else {
      d.life += dt;
      d.m.scale.set(1 + d.life * 2, 0.3, 1 + d.life * 2);
      if (d.life > 0.8) { scene.remove(d.m); drops.splice(k, 1); }
    }
  }
}

// ---------- Physics (Verlet + position-based distance constraints) ----------
let K = 0.7, ITER = 6;                  // stiffness and solver iterations (set by Firmness)
const DAMP = 0.996, GRAV = -9.8;
let grabOn = false;
const grabStart = new THREE.Vector3(), grabNow = new THREE.Vector3();
let squeezing = false, sqT = 0;
const sqFired = [];
const SQ_MARKS = [0.2, 0.4, 0.6, 0.8];

function weigh(pt, rad) {
  for (let i = 0; i < NV; i++) {
    const i3 = i * 3;
    const dx = P[i3] - pt.x, dy = P[i3 + 1] - pt.y, dz = P[i3 + 2] - pt.z;
    const d = Math.sqrt(dx * dx + dy * dy + dz * dz) / rad;
    W[i] = d < 1 ? (1 - d) * (1 - d) : 0;
  }
}
function applyGrab() {
  const dx = grabNow.x - grabStart.x, dz = grabNow.z - grabStart.z;
  for (let i = 0; i < NV; i++) {
    const w = W[i];
    if (w <= 0) continue;
    const i3 = i * 3;
    P[i3] += (GS[i3] + dx - P[i3]) * 0.3 * w;
    P[i3 + 2] += (GS[i3 + 2] + dz - P[i3 + 2]) * 0.3 * w;
  }
}
function applySqueeze(h) {
  sqT += h;
  const s = Math.min(sqT / 2.4, 1);
  const p = Math.sin(Math.PI * s);
  const kx = 1 - 0.10 * p * h, ky = 1 - 0.12 * p * h;
  for (let i = 0; i < NV; i++) {
    const i3 = i * 3;
    P[i3] *= kx; P[i3 + 2] *= kx; P[i3 + 1] *= ky;
  }
  for (let k = 0; k < SQ_MARKS.length; k++) {
    if (!sqFired[k] && s >= SQ_MARKS[k]) { sqFired[k] = true; spawnDrop(Math.floor(Math.random() * S)); }
  }
  if (sqT >= 2.6) squeezing = false;
}
function collide() {
  for (let i = 0; i < NV; i++) {
    const i3 = i * 3;
    if (P[i3 + 1] < 0) {
      const vy = P[i3 + 1] - Q[i3 + 1];
      P[i3 + 1] = 0;
      const vx = P[i3] - Q[i3], vz = P[i3 + 2] - Q[i3 + 2];
      Q[i3] = P[i3] - vx * 0.5;                 // ground friction
      Q[i3 + 2] = P[i3 + 2] - vz * 0.5;
      if (vy < 0) Q[i3 + 1] = P[i3 + 1] + vy * 0.25; // small bounce
    }
    const rr = Math.hypot(P[i3], P[i3 + 2]);
    if (rr > 1.65) { P[i3] *= 1.65 / rr; P[i3 + 2] *= 1.65 / rr; } // stay on the plate
  }
}
function substep(h) {
  const h2 = h * h;
  for (let i = 0; i < NV; i++) {
    const i3 = i * 3;
    const vx = (P[i3] - Q[i3]) * DAMP, vy = (P[i3 + 1] - Q[i3 + 1]) * DAMP, vz = (P[i3 + 2] - Q[i3 + 2]) * DAMP;
    Q[i3] = P[i3]; Q[i3 + 1] = P[i3 + 1]; Q[i3 + 2] = P[i3 + 2];
    P[i3] += vx; P[i3 + 1] += vy + GRAV * h2; P[i3 + 2] += vz;
  }
  if (grabOn) applyGrab();
  if (squeezing) applySqueeze(h);
  for (let it = 0; it < ITER; it++) {
    for (let c = 0; c < NCON; c++) {
      const a3 = CAi[c] * 3, b3 = CBi[c] * 3;
      const dx = P[b3] - P[a3], dy = P[b3 + 1] - P[a3 + 1], dz = P[b3 + 2] - P[a3 + 2];
      const len = Math.sqrt(dx * dx + dy * dy + dz * dz) || 1e-9;
      const f = (len - CRf[c]) / len * 0.5 * K;
      P[a3] += dx * f; P[a3 + 1] += dy * f; P[a3 + 2] += dz * f;
      P[b3] -= dx * f; P[b3 + 1] -= dy * f; P[b3 + 2] -= dz * f;
    }
  }
  collide();
}

function resetJelly() {
  P.set(REST); Q.set(REST);
  grabOn = false; squeezing = false;
  for (const d of drops) scene.remove(d.m);
  drops.length = 0;
}
function squeeze() {
  if (squeezing) return;
  squeezing = true; sqT = 0; sqFired.length = 0;
}
const slider = document.getElementById('fz');
function applyFirm(v) {
  const f = v / 100;
  K = 0.3 + 0.65 * f;
  ITER = 3 + Math.round(6 * f);
}
function setFirm(v) { slider.value = v; applyFirm(v); }

// ---------- Input: drag to stretch, tap to poke ----------
const ray = new THREE.Raycaster();
const ndc = new THREE.Vector2();
const dragPlane = new THREE.Plane();
let pd = null; // current pointer state
function toNDC(e) {
  const r = canvas.getBoundingClientRect();
  ndc.x = ((e.clientX - r.left) / r.width) * 2 - 1;
  ndc.y = -((e.clientY - r.top) / r.height) * 2 + 1;
  ray.setFromCamera(ndc, camera);
}
function poke(pt) {
  weigh(pt, 0.45);
  for (let i = 0; i < NV; i++) if (W[i] > 0) P[i * 3 + 1] -= 0.12 * W[i];
}
canvas.addEventListener('pointerdown', e => {
  toNDC(e);
  const hits = ray.intersectObject(jelly, false);
  if (!hits.length) return;
  const hp = hits[0].point.clone();
  try { canvas.setPointerCapture(e.pointerId); } catch (_) {}
  pd = { id: e.pointerId, x: e.clientX, y: e.clientY, t: performance.now(), moved: false, hp };
  weigh(hp, 0.45);
  grabStart.copy(hp); grabNow.copy(hp);
  GS.set(P);
  grabOn = true;
});
canvas.addEventListener('pointermove', e => {
  if (!pd || e.pointerId !== pd.id) return;
  if (Math.hypot(e.clientX - pd.x, e.clientY - pd.y) > 10) pd.moved = true;
  toNDC(e);
  dragPlane.set(new THREE.Vector3(0, 1, 0), -pd.hp.y);
  const out = new THREE.Vector3();
  if (ray.ray.intersectPlane(dragPlane, out)) grabNow.set(out.x, pd.hp.y, out.z);
});
function endPointer(e) {
  if (!pd || e.pointerId !== pd.id) return;
  const quick = performance.now() - pd.t < 350;
  if (!pd.moved && quick) poke(pd.hp);
  grabOn = false;
  pd = null;
}
canvas.addEventListener('pointerup', endPointer);
canvas.addEventListener('pointercancel', endPointer);

// ---------- Buttons ----------
document.getElementById('squeeze').addEventListener('click', squeeze);
document.getElementById('reset').addEventListener('click', () => { resetJelly(); setFirm(55); });
slider.addEventListener('input', () => applyFirm(+slider.value));

// ---------- Demo mode (#demo): hands-free ~20 s loop, any touch ends it ----------
let demoOn = location.hash === '#demo';
const hintEl = document.getElementById('hint');
if (demoOn) hintEl.textContent = 'Demo running: touch the jelly to play';
function stopDemo() {
  if (!demoOn) return;
  demoOn = false; grabOn = false;
  hintEl.textContent = 'Drag to stretch, tap to poke, Squeeze for juice';
}
['pointerdown', 'touchstart', 'keydown', 'wheel'].forEach(ev =>
  window.addEventListener(ev, stopDemo, { capture: true, passive: true }));

let demoT = 0;
const fired = {};
const fire = (t, k, fn) => { if (demoT >= t && !fired[k]) { fired[k] = true; fn(); } };
const pt = (x, z) => new THREE.Vector3(x, 0.37, z);
function demoStep(dt) {
  demoT += dt;
  fire(0.6, 'p1', () => poke(pt(0.45, 0.2)));
  fire(2.2, 'p2', () => poke(pt(-0.2, -0.5)));
  fire(3.2, 'g0', () => {
    grabStart.copy(pt(-0.45, 0)); grabNow.copy(grabStart);
    weigh(grabStart, 0.45); GS.set(P); grabOn = true;
  });
  if (grabOn && demoT >= 3.2 && demoT < 4.8) grabNow.x = grabStart.x + (demoT - 3.2) / 1.6 * 0.8;
  fire(4.8, 'g1', () => { grabOn = false; });
  fire(7.0, 'sq', squeeze);
  fire(10.5, 'f1', () => setFirm(88));
  fire(13.5, 'f2', () => setFirm(30));
  fire(15.5, 'p3', () => poke(pt(-0.35, 0.35)));
  fire(18.5, 'f3', () => setFirm(60));
  if (demoT >= 20) { resetJelly(); demoT = 0; for (const k in fired) delete fired[k]; }
}

// ---------- Layout and loop ----------
function resize() {
  const w = window.innerWidth, h = window.innerHeight, aspect = w / h;
  renderer.setSize(w, h, false);
  camera.aspect = aspect;
  camera.fov = aspect < 1 ? 62 : 38;
  const t = Math.tan(THREE.MathUtils.degToRad(camera.fov / 2));
  const dist = Math.max(4.2, 2.0 / (t * aspect));
  camera.position.set(0, dist * 0.6, dist * 0.8);
  camera.lookAt(0, -0.2, 0);
  camera.updateProjectionMatrix();
}
window.addEventListener('resize', resize);
resize();
resetJelly();
applyFirm(55);

let last = performance.now();
function loop(now) {
  requestAnimationFrame(loop);
  const dt = Math.min((now - last) / 1000, 1 / 30);
  last = now;
  if (demoOn) demoStep(dt);
  const h = dt / 4;
  if (h > 0) for (let s = 0; s < 4; s++) substep(h);
  geo.attributes.position.needsUpdate = true;
  geo.computeVertexNormals();
  geo.boundingSphere = null;
  updateDrops(dt);
  renderer.render(scene, camera);
}
requestAnimationFrame(loop);
</script>
</body>
</html>