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
partly 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,maximum-scale=1,user-scalable=no">
<title>Jelly Orange Slice</title>
<style>
html,body{margin:0;height:100%;overflow:hidden;background:#fdf8f0;font-family:system-ui,sans-serif;touch-action:none;user-select:none;-webkit-user-select:none}
#c{position:fixed;inset:0;width:100%;height:100%;display:block}
.ui{position:fixed;left:0;right:0;bottom:0;padding:14px;display:flex;flex-wrap:wrap;gap:12px;align-items:center;justify-content:center;background:linear-gradient(transparent,rgba(253,248,240,.95) 40%)}
button{font-size:18px;padding:14px 22px;border:0;border-radius:14px;background:#ff8a1f;color:#fff;font-weight:700;box-shadow:0 4px 0 #c75f0c;cursor:pointer;min-width:120px}
button:active{transform:translateY(3px);box-shadow:0 1px 0 #c75f0c}
label{font-size:16px;color:#5a3a1a;display:flex;align-items:center;gap:10px}
input[type=range]{width:160px;height:28px}
#hint{position:fixed;top:12px;left:0;right:0;text-align:center;color:#7a5530;font-size:15px;pointer-events:none}
</style>
</head>
<body>
<canvas id="c"></canvas>
<div id="hint">Poke it or drag to stretch it</div>
<div class="ui">
<button id="squeeze">Squeeze</button>
<label>Firmness <input id="firm" type="range" min="0" max="100" value="50"></label>
<button id="reset">Reset</button>
</div>
<script type="importmap">
{"imports":{"three":"https://unpkg.com/[email protected]/build/three.module.js"}}
</script>
<script type="module">
import * as THREE from 'three';
const canvas = document.getElementById('c');
const renderer = new THREE.WebGLRenderer({canvas, antialias:true, alpha:false});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setClearColor(0xfdf8f0);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(40, 1, 0.1, 100);
camera.position.set(0, 3.2, 6.2);
camera.lookAt(0, 0.4, 0);
scene.add(new THREE.HemisphereLight(0xffffff, 0xe8d8c0, 1.0));
const sun = new THREE.DirectionalLight(0xffffff, 1.6);
sun.position.set(3, 6, 4);
sun.castShadow = true;
sun.shadow.mapSize.set(1024, 1024);
sun.shadow.camera.left = -4; sun.shadow.camera.right = 4;
sun.shadow.camera.top = 4; sun.shadow.camera.bottom = -4;
scene.add(sun);
const back = new THREE.DirectionalLight(0xffe2b0, 0.9);
back.position.set(-4, 3, -3);
scene.add(back);
// Plate
const plate = new THREE.Mesh(
new THREE.CylinderGeometry(2.6, 2.2, 0.12, 64),
new THREE.MeshStandardMaterial({color:0xffffff, roughness:0.25, metalness:0})
);
plate.position.y = -0.06;
plate.receiveShadow = true;
scene.add(plate);
// ---------- Soft-body slice ----------
// Slice = a disc of N radial sectors. Each vertex is a mass point with
// a spring network (structural + shear-ish + radial ring) integrated with
// Verlet and damping. Segments are rendered as separate meshes per sector
// with a jelly material and an internal juice-cell texture.
const RINGS = 5; // radial rings
const SEG = 10; // segments (wedges)
const R0 = 1.8; // radius
const THICK = 0.55; // half thickness (top/bottom offsets)
// Build point grid: for each segment wedge s, ring r, and layer (top/bottom)
const pts = []; // {x,y,z, px,py,pz, fixed:false}
const idx = (s, r, l) => ((s % SEG) * RINGS + r) * 2 + l;
for (let s = 0; s < SEG; s++) {
for (let r = 0; r < RINGS; r++) {
for (let l = 0; l < 2; l++) {
const a = (s / SEG) * Math.PI * 2;
const rad = (r + 1) / RINGS * R0;
const y = (l === 0 ? THICK : -THICK);
const x = Math.cos(a) * rad, z = Math.sin(a) * rad;
pts.push({x, y: y + 0.4, z, px: x, py: y + 0.4, pz: z, vx:0, vy:0, vz:0});
}
}
}
const center = [];
for (let l = 0; l < 2; l++) {
const y = (l === 0 ? THICK : -THICK) + 0.4;
center.push({x:0,y,z:0,px:0,py:y,pz:0,vx:0,vy:0,vz:0});
}
pts.push(center[0], center[1]);
const CT = pts.length - 2, CB = pts.length - 1;
const P = i => pts[i];
// Springs
const springs = [];
function addSpring(a, b, k) {
const p = P(a), q = P(b);
const dx = p.x - q.x, dy = p.y - q.y, dz = p.z - q.z;
springs.push({a, b, k, rest: Math.hypot(dx, dy, dz)});
}
for (let s = 0; s < SEG; s++) {
for (let r = 0; r < RINGS; r++) {
for (let l = 0; l < 2; l++) {
const i = idx(s, r, l);
const inext = idx(s + 1, r, l);
addSpring(i, inext, 1.0); // ring
if (r < RINGS - 1) {
addSpring(i, idx(s, r + 1, l), 1.0); // radial
addSpring(i, idx(s + 1, r + 1, l), 0.6); // shear
addSpring(idx(s + 1, r, l), idx(s, r + 1, l), 0.6);
}
}
// vertical thickness springs
addSpring(idx(s, r, 0), idx(s, r, 1), 1.2);
if (r < RINGS - 1) {
addSpring(idx(s, r, 0), idx(s, r + 1, 1), 0.4);
addSpring(idx(s, r, 1), idx(s, r + 1, 0), 0.4);
}
}
// center to first ring
addSpring(CT, idx(s, 0, 0), 1.2);
addSpring(CB, idx(s, 0, 1), 1.2);
addSpring(CT, CB, 1.0);
}
// Vertex-level Verlet: a point stays above plate (y >= 0.3 rest) and inside plate radius
const GRAV = -9.8;
let firmness = 0.5;
let stiffnessScale = 1, dampScale = 1;
let squeezeT = 0; // >0 while squeezing
let squeezeSeq = []; // drop events
let juice = []; // falling droplets
let dragPt = null, dragTarget = null;
function applyFirmness() {
stiffnessScale = 0.35 + firmness * 1.6;
dampScale = 1.2 - firmness * 0.4;
}
// Raycast helpers
const ray = new THREE.Raycaster();
const ndc = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0,1,0), -1.0);
const hit = new THREE.Vector3();
// Mesh rendering: one mesh per wedge with shared geometry updated each frame
const jellyMat = new THREE.MeshPhysicalMaterial({
color: 0xff8a1f, transparent: true, opacity: 0.82,
roughness: 0.12, metalness: 0, transmission: 0.35, thickness: 0.8,
clearcoat: 1, clearcoatRoughness: 0.08, ior: 1.35,
emissive: 0xff5a00, emissiveIntensity: 0.12, side: THREE.DoubleSide
});
const pithMat = new THREE.MeshStandardMaterial({color:0xfff6e8, roughness:0.6, transparent:true, opacity:0.75, side:THREE.DoubleSide});
const peelMat = new THREE.MeshStandardMaterial({color:0xf26b00, roughness:0.35, metalness:0.05, side:THREE.DoubleSide});
// Juice-cell texture
function makeCellTexture() {
const c = document.createElement('canvas');
c.width = c.height = 256;
const g = c.getContext('2d');
g.fillStyle = '#ffb347'; g.fillRect(0,0,256,256);
for (let i = 0; i < 260; i++) {
const x = Math.random() * 256, y = Math.random() * 256, r = 2 + Math.random() * 5;
const grd = g.createRadialGradient(x, y, 0, x, y, r);
grd.addColorStop(0, 'rgba(255,240,180,0.9)');
grd.addColorStop(1, 'rgba(255,140,30,0)');
g.fillStyle = grd;
g.beginPath(); g.arc(x, y, r, 0, Math.PI * 2); g.fill();
}
const t = new THREE.CanvasTexture(c);
t.wrapS = t.wrapT = THREE.RepeatWrapping;
return t;
}
const cellTex = makeCellTexture();
jellyMat.map = cellTex;
jellyMat.transmission = 0.35;
// Geometry per wedge: we build a triangle soup from points each frame
const wedges = [];
const peelGroup = new THREE.Group();
scene.add(peelGroup);
for (let s = 0; s < SEG; s++) {
const geo = new THREE.BufferGeometry();
const verts = new Float32Array(3 * 3 * 4 * (RINGS) * 2 + 3 * 3 * 2 * SEG);
geo.setAttribute('position', new THREE.BufferAttribute(verts, 3));
geo.setAttribute('uv', new THREE.BufferAttribute(new Float32Array(verts.length / 3 * 2), 2));
const mesh = new THREE.Mesh(geo, jellyMat);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
wedges.push(mesh);
}
// Peel rim: a ring around the outer edge following the outer ring points
const peelGeo = new THREE.BufferGeometry();
const peelMesh = new THREE.Mesh(peelGeo, peelMat);
peelMesh.castShadow = true;
scene.add(peelMesh);
// Pith: inner white band, between radius rings 3 and 4
const pithGeo = new THREE.BufferGeometry();
const pithMesh = new THREE.Mesh(pithGeo, pithMat);
scene.add(pithMesh);
// Stub a per-frame update of the geometry from point positions.
function buildMeshes() {
for (let s = 0; s < SEG; s++) {
const mesh = wedges[s];
const pos = mesh.geometry.attributes.position.array;
let k = 0;
const put = (i) => {
const p = P(i);
pos[k++] = p.x; pos[k++] = p.y; pos[k++] = p.z;
};
// Each wedge quad strip between rings 0..RINGS-1 with top/bottom
const s1 = (s + 1) % SEG;
for (let r = 0; r < RINGS - 1; r++) {
// top face quad
const a = idx(s, r, 0), b = idx(s1, r, 0), c = idx(s, r + 1, 0), d = idx(s1, r + 1, 0);
put(a); put(b); put(c);
put(b); put(d); put(c);
// bottom face quad
const e = idx(s, r, 1), f = idx(s1, r, 1), g = idx(s, r + 1, 1), h = idx(s1, r + 1, 1);
put(e); put(g); put(f);
put(f); put(g); put(h);
}
// inner fan triangles at center (top and bottom)
put(CT); put(idx(s1, 0, 0)); put(idx(s, 0, 0));
put(CB); put(idx(s, 0, 1)); put(idx(s1, 0, 1));
// side walls on outer ring
const o = RINGS - 1;
const ta = idx(s, o, 0), tb = idx(s1, o, 0), ba = idx(s, o, 1), bb = idx(s1, o, 1);
put(ta); put(ba); put(tb);
put(tb); put(ba); put(bb);
// fill remaining buffer with degenerate triangles
while (k < pos.length) pos[k++] = 0;
mesh.geometry.attributes.position.needsUpdate = true;
mesh.geometry.computeVertexNormals();
}
// Peel: outer ring side band, slightly beyond
const peelPos = [];
for (let s = 0; s < SEG; s++) {
const s1 = (s + 1) % SEG;
const o = RINGS - 1;
const ta = P(idx(s, o, 0)), tb = P(idx(s1, o, 0));
const ba = P(idx(s, o, 1)), bb = P(idx(s1, o, 1));
const v = [ta, ba, tb, tb, ba, bb];
for (const q of v) peelPos.push(q.x * 1.02, q.y, q.z * 1.02);
}
peelGeo.setAttribute('position', new THREE.Float32BufferAttribute(peelPos, 3));
peelGeo.computeVertexNormals();
// Pith: inner ring band (ring 3 -> 4)
const pithPos = [];
const pr = RINGS - 2;
for (let s = 0; s < SEG; s++) {
const s1 = (s + 1) % SEG;
const a = P(idx(s, pr, 0)), b = P(idx(s1, pr, 0));
const c = P(idx(s, pr + 1, 0)), d = P(idx(s1, pr + 1, 0));
pithPos.push(a.x,a.y,a.z, b.x,b.y,b.z, c.x,c.y,c.z, b.x,b.y,b.z, d.x,d.y,d.z, c.x,c.y,c.z);
}
pithGeo.setAttribute('position', new THREE.Float32BufferAttribute(pithPos, 3));
pithGeo.computeVertexNormals();
}
// ---------- Physics step ----------
function step(dt) {
const sub = 4;
const h = dt / sub;
for (let it = 0; it < sub; it++) {
for (const p of pts) {
if (p.fixed) continue;
const vx = (p.x - p.px) * dampScale;
const vy = (p.y - p.py) * dampScale;
const vz = (p.z - p.pz) * dampScale;
p.px = p.x; p.py = p.y; p.pz = p.z;
p.x += vx + 0 * h;
p.y += vy + GRAV * h * h;
p.z += vz;
}
// springs
for (const s of springs) {
const a = P(s.a), b = P(s.b);
let dx = b.x - a.x, dy = b.y - a.y, dz = b.z - a.z;
const d = Math.hypot(dx, dy, dz) || 1e-6;
const diff = (d - s.rest) / d * 0.5 * s.k * stiffnessScale * 0.9;
dx *= diff; dy *= diff; dz *= diff;
if (!a.fixed) { a.x += dx; a.y += dy; a.z += dz; }
if (!b.fixed) { b.x -= dx; b.y -= dy; b.z -= dz; }
}
// dragged point target
if (dragPt) {
const p = dragPt;
p.x += (dragTarget.x - p.x) * 0.5;
p.y += (dragTarget.y - p.y) * 0.5;
p.z += (dragTarget.z - p.z) * 0.5;
}
// squeeze: pull ring toward center in y compression
if (squeezeT > 0) {
const f = Math.min(1, squeezeT) * 0.05;
for (const p of pts) {
p.x *= (1 - f * 0.1);
p.z *= (1 - f * 0.1);
p.y -= f * 0.02;
}
}
// plate collision: keep above plate, and inside radius
for (const p of pts) {
if (p.y < 0.12) {
p.y = 0.12;
const fx = (p.x - p.px) * 0.6, fz = (p.z - p.pz) * 0.6;
p.px = p.x - fx; p.pz = p.z - fz; p.py = p.y;
}
const r = Math.hypot(p.x, p.z);
if (r > 2.4) {
const s2 = 2.4 / r; p.x *= s2; p.z *= s2;
}
}
}
}
// Squeeze: press down and emit drops
let dropsLeft = 0;
function doSqueeze() {
squeezeT = 1.0;
dropsLeft = 4 + Math.floor(Math.random() * 3);
}
function emitDrop() {
const g = new THREE.SphereGeometry(0.05, 10, 10);
const m = new THREE.Mesh(g, new THREE.MeshPhysicalMaterial({color:0xffb347, transparent:true, opacity:0.9, roughness:0.1, transmission:0.4}));
const a = Math.random() * Math.PI * 2;
m.position.set(Math.cos(a) * 1.2, 0.9, Math.sin(a) * 1.2);
m.userData.vy = -0.5 - Math.random();
scene.add(m);
juice.push(m);
}
// Pointer interaction
let pointerDown = false;
function screenToPoint(e) {
const rect = canvas.getBoundingClientRect();
ndc.x = ((e.clientX - rect.left) / rect.width) * 2 - 1;
ndc.y = -((e.clientY - rect.top) / rect.height) * 2 + 1;
return ndc;
}
function nearestPoint(e) {
screenToPoint(e);
ray.setFromCamera(ndc, camera);
let best = null, bestD = 1e9;
for (const p of pts) {
const v = new THREE.Vector3(p.x, p.y, p.z);
const d = ray.ray.distanceToPoint(v);
if (d < bestD) { bestD = d; best = p; }
}
return bestD < 0.5 ? best : null;
}
function worldAt(e) {
screenToPoint(e);
ray.setFromCamera(ndc, camera);
const t = new THREE.Vector3();
ray.ray.intersectPlane(dragPlane, t);
return t;
}
canvas.addEventListener('pointerdown', (e) => {
pointerDown = true;
document.getElementById('hint').style.opacity = 0;
demoActive && stopDemo();
dragPt = nearestPoint(e);
if (dragPt) {
dragTarget = worldAt(e);
canvas.setPointerCapture(e.pointerId);
// Poke: also push the nearest point
dragPt.vy = -0.3;
dragPt.py = dragPt.y + 0.3;
} else {
// poke: push all points near the touch
const t = worldAt(e);
for (const p of pts) {
const d = Math.hypot(p.x - t.x, p.z - t.z);
if (d < 1.2) { p.py = p.y + 0.25; }
}
}
});
canvas.addEventListener('pointermove', (e) => {
if (!pointerDown || !dragPt) return;
dragTarget = worldAt(e);
dragTarget.y = Math.max(0.3, Math.min(dragTarget.y, 3));
});
function endDrag() {
pointerDown = false;
dragPt = null;
}
canvas.addEventListener('pointerup', endDrag);
canvas.addEventListener('pointercancel', endDrag);
// Buttons
document.getElementById('squeeze').addEventListener('click', doSqueeze);
document.getElementById('firm').addEventListener('input', (e) => {
firmness = e.target.value / 100;
applyFirmness();
});
document.getElementById('reset').addEventListener('click', () => resetSlice());
function resetSlice() {
for (let i = 0; i < pts.length; i++) {
const p = pts[i];
const isCenter = i >= CT;
if (isCenter) {
const y = (i === CT ? THICK : -THICK) + 0.4;
p.x = 0; p.z = 0; p.y = y; p.px = 0; p.py = y; p.pz = 0;
} else {
const k = Math.floor(i / 2);
const l = i % 2;
const r = k % RINGS;
const s = Math.floor(k / RINGS);
const a = (s / SEG) * Math.PI * 2;
const rad = (r + 1) / RINGS * R0;
const y = (l === 0 ? THICK : -THICK) + 0.4;
p.x = Math.cos(a) * rad; p.z = Math.sin(a) * rad; p.y = y;
p.px = p.x; p.py = y; p.pz = p.z;
}
}
juice.forEach(m => scene.remove(m));
juice = [];
squeezeT = 0;
}
// Demo mode (#demo)
let demoActive = location.hash === '#demo';
let demoT = 0;
function stopDemo() { demoActive = false; }
function runDemo(dt) {
demoT += dt;
if (demoT > 20) { demoActive = false; return; }
const t = demoT;
if (t > 3 && t < 3.1) { const p = pts[Math.floor(Math.random() * pts.length)]; p.py = p.y + 0.4; }
if (t > 6 && t < 6.1) doSqueeze();
if (t > 12 && t < 12.1) { firmness = 0.2; document.getElementById('firm').value = 20; applyFirmness(); }
if (t > 15 && t < 15.1) { firmness = 0.8; document.getElementById('firm').value = 80; applyFirmness(); }
if (t > 18 && t < 18.1) resetSlice();
}
// Resize
function resize() {
const w = innerWidth, h = innerHeight;
renderer.setSize(w, h, false);
camera.aspect = w / h;
camera.fov = w < h ? 55 : 40;
camera.updateProjectionMatrix();
}
addEventListener('resize', resize);
resize();
// Init
applyFirmness();
buildMeshes();
// Loop
let last = performance.now();
function frame(now) {
const dt = Math.min(0.033, (now - last) / 1000);
last = now;
if (demoActive) runDemo(dt);
if (squeezeT > 0) {
squeezeT -= dt * 1.2;
if (dropsLeft > 0 && Math.random() < 0.08) { emitDrop(); dropsLeft--; }
}
step(dt);
buildMeshes();
for (let i = juice.length - 1; i >= 0; i--) {
const m = juice[i];
m.userData.vy -= 9.8 * dt;
m.position.y += m.userData.vy * dt;
if (m.position.y < 0.1) { scene.remove(m); juice.splice(i, 1); }
}
renderer.render(scene, camera);
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
</script>
</body>
</html>
