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>Orange Slice Jelly</title>
<style>
:root { --ink:#3b2412; --panel:rgba(255,250,240,.94); --accent:#ff8a1f; }
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) { --ink:#fbe9d2; --panel:rgba(40,28,18,.94); --accent:#ff9c3a; }
}
:root[data-theme="dark"] { --ink:#fbe9d2; --panel:rgba(40,28,18,.94); --accent:#ff9c3a; }
html, body { margin:0; height:100%; overflow:hidden; background:#f4eee4; color:var(--ink);
font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif; }
#app { position:fixed; inset:0; }
#app canvas { display:block; width:100%; height:100%; touch-action:none; user-select:none; -webkit-user-select:none; }
#hint { position:fixed; top:16px; left:16px; right:16px; text-align:center; font-size:15px; font-weight:600;
pointer-events:none; opacity:.85; }
#ui { position:fixed; left:0; right:0; bottom:0; display:flex; justify-content:center;
padding:12px 16px calc(14px + env(safe-area-inset-bottom)); pointer-events:none; }
#panel { pointer-events:auto; width:min(440px,100%); box-sizing:border-box; background:var(--panel);
border-radius:20px; padding:14px; box-shadow:0 8px 28px rgba(60,30,0,.18); display:flex; flex-direction:column; gap:12px; }
.row { display:flex; gap:12px; }
button { flex:1; min-height:60px; border:0; border-radius:16px; font-size:19px; font-weight:700; background:var(--accent);
color:#2a1505; cursor:pointer; touch-action:manipulation; -webkit-tap-highlight-color:transparent; font-family:inherit; }
button:active { transform:scale(.97); }
button.alt { background:transparent; color:var(--ink); box-shadow:inset 0 0 0 2px var(--accent); }
.slider-head { display:flex; justify-content:space-between; font-size:17px; font-weight:700; }
input[type=range] { width:100%; height:44px; margin:0; accent-color:var(--accent); }
#juice { font-size:15px; opacity:.85; text-align:center; }
</style>
</head>
<body>
<div id="app"></div>
<div id="hint">Poke it, or drag to stretch it</div>
<div id="ui">
<div id="panel">
<div class="row">
<button id="squeeze">Squeeze</button>
<button id="reset" class="alt">Reset</button>
</div>
<div class="slider-head"><span>Firmness</span><span id="firmVal">50</span></div>
<input id="firm" type="range" min="0" max="100" value="50">
<div id="juice"></div>
</div>
</div>
<script type="module">
import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js';
const $ = id => document.getElementById(id);
const hintEl = $('hint'), juiceEl = $('juice'), firmEl = $('firm'), firmVal = $('firmVal');
const squeezeBtn = $('squeeze'), resetBtn = $('reset');
const DEFAULT_HINT = 'Poke it, or drag to stretch it';
// ---------- layout and physics constants ----------
const S = 10, N = 40, R = 5, SEG = N / S; // juice segments, angular steps, rings
const NP = 2 + 2 * R * N; // particles: 2 centres + rings on top and bottom
const HALF_H = 0.16, BODY_R = 1.0;
const PLATE_TOP = 0, FLOOR = PLATE_TOP + 0.02;
const Y0 = PLATE_TOP + HALF_H + 0.05; // resting height of the slice centre
const LIFT = 0.35; // drop height on load and reset
const GRAV = -9.8, SUB = 1 / 240, ITER = 8, DAMP = 0.992, MAXV = 0.03;
const PRESS = 34, OUTR = 9, PEEL_OFF = 0.045;
const JUICE_MAX = 7, SQ_DUR = 1.6, PICK_R = 0.2;
let STIFF = 0.7; // set by the Firmness slider
let pressS = 0; // squeeze strength 0..1
let grab = -1, TX = 0, TY = 0, TZ = 0;
let juiceLeft = JUICE_MAX;
// seeded random so every load looks the same
let seed = 20251122;
function rnd() {
seed = (seed + 0x6D2B79F5) | 0;
let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
}
// ---------- particle indexing: 0 and 1 are top/bottom centres, then rings ----------
function ringIdx(L, k, i) {
i = ((i % N) + N) % N;
return 2 + L * R * N + (k - 1) * N + i;
}
function pidx(L, k, i) { return k === 0 ? L : ringIdx(L, k, i); }
// ---------- rest shape (relative to slice centre): a slightly domed disc ----------
const rest = new Float64Array(NP * 3);
for (let L = 0; L < 2; L++) {
const sg = L === 0 ? 1 : -1, bulge = L === 0 ? 0.07 : 0.03;
rest[3 * L] = 0; rest[3 * L + 1] = sg * (HALF_H + bulge); rest[3 * L + 2] = 0;
for (let k = 1; k <= R; k++) {
const r = BODY_R * k / R;
for (let i = 0; i < N; i++) {
const a = i * Math.PI * 2 / N, p = ringIdx(L, k, i);
rest[3 * p] = r * Math.cos(a);
rest[3 * p + 1] = sg * (HALF_H + bulge * (1 - r * r));
rest[3 * p + 2] = r * Math.sin(a);
}
}
}
// ---------- springs (distance constraints with rest lengths) ----------
const SA = [], SB = [], SL = [], SK = [];
function link(a, b, k) {
if (a === b) return;
const dx = rest[3 * a] - rest[3 * b], dy = rest[3 * a + 1] - rest[3 * b + 1], dz = rest[3 * a + 2] - rest[3 * b + 2];
SA.push(a); SB.push(b); SL.push(Math.hypot(dx, dy, dz)); SK.push(k);
}
for (let L = 0; L < 2; L++) {
for (let i = 0; i < N; i++) link(L, ringIdx(L, 1, i), 1);
for (let k = 1; k <= R; k++) for (let i = 0; i < N; i++) {
const a = ringIdx(L, k, i);
link(a, ringIdx(L, k, i + 1), 1);
link(a, ringIdx(L, k, i + 2), 0.35); // bending
if (k < R) {
link(a, ringIdx(L, k + 1, i), 1);
link(a, ringIdx(L, k + 1, i + 1), 0.8); // shear
link(a, ringIdx(L, k + 1, i - 1), 0.8);
}
}
}
link(0, 1, 1);
for (let k = 1; k <= R; k++) for (let i = 0; i < N; i++) {
const t = ringIdx(0, k, i), b = ringIdx(1, k, i);
link(t, b, 1); link(t, ringIdx(1, k, i + 1), 0.6); link(ringIdx(0, k, i + 1), b, 0.6);
}
const NS = SA.length;
const sA = Int32Array.from(SA), sB = Int32Array.from(SB), sL = Float64Array.from(SL), sK = Float64Array.from(SK);
// ---------- simulation state ----------
const X = new Float64Array(NP), Y = new Float64Array(NP), Z = new Float64Array(NP);
const PX = new Float64Array(NP), PY = new Float64Array(NP), PZ = new Float64Array(NP);
const IM = new Float64Array(NP); // inverse mass, 0 = held by the hand
const OX = new Float64Array(NP), OZ = new Float64Array(NP), FY = new Float64Array(NP);
FY[0] = 1; FY[1] = 0;
for (let L = 0; L < 2; L++) for (let k = 1; k <= R; k++) for (let i = 0; i < N; i++) {
const p = ringIdx(L, k, i);
if (L === 0) FY[p] = 1 - 0.4 * k / R; // squeeze pushes the top face down
if (k === R) { const a = i * Math.PI * 2 / N; OX[p] = Math.cos(a); OZ[p] = Math.sin(a); } // rim bulges out
}
function resetBody(lift) {
for (let p = 0; p < NP; p++) {
X[p] = rest[3 * p]; Y[p] = rest[3 * p + 1] + Y0 + lift; Z[p] = rest[3 * p + 2];
PX[p] = X[p]; PY[p] = Y[p]; PZ[p] = Z[p]; IM[p] = 1;
}
}
function simStep(dt) {
const g = GRAV * dt * dt, ps = pressS * dt * dt;
for (let p = 0; p < NP; p++) {
if (IM[p] === 0) continue;
let vx = (X[p] - PX[p]) * DAMP, vy = (Y[p] - PY[p]) * DAMP, vz = (Z[p] - PZ[p]) * DAMP;
const sp = Math.sqrt(vx * vx + vy * vy + vz * vz);
if (sp > MAXV) { const f = MAXV / sp; vx *= f; vy *= f; vz *= f; }
PX[p] = X[p]; PY[p] = Y[p]; PZ[p] = Z[p];
X[p] += vx + ps * OUTR * OX[p];
Y[p] += vy + g - ps * PRESS * FY[p];
Z[p] += vz + ps * OUTR * OZ[p];
}
if (grab >= 0) { // hand pulls this particle
const p = grab;
PX[p] = X[p]; PY[p] = Y[p]; PZ[p] = Z[p];
X[p] += (TX - X[p]) * 0.5; Y[p] += (TY - Y[p]) * 0.5; Z[p] += (TZ - Z[p]) * 0.5;
}
for (let it = 0; it < ITER; it++) {
for (let s = 0; s < NS; s++) {
const a = sA[s], b = sB[s];
const wa = IM[a], wb = IM[b], w = wa + wb;
if (w === 0) continue;
const dx = X[b] - X[a], dy = Y[b] - Y[a], dz = Z[b] - Z[a];
const d = Math.sqrt(dx * dx + dy * dy + dz * dz) || 1e-9;
const c = (d - sL[s]) / d * sK[s] * STIFF / w;
X[a] += dx * c * wa; Y[a] += dy * c * wa; Z[a] += dz * c * wa;
X[b] -= dx * c * wb; Y[b] -= dy * c * wb; Z[b] -= dz * c * wb;
}
for (let p = 0; p < NP; p++) if (Y[p] < FLOOR) Y[p] = FLOOR; // plate never lets it sink
}
for (let p = 0; p < NP; p++) { // friction and no bounce on the plate
if (IM[p] === 0 || Y[p] > FLOOR + 0.005) continue;
const vx = X[p] - PX[p], vz = Z[p] - PZ[p], vy = Y[p] - PY[p];
PX[p] = X[p] - vx * 0.45; PZ[p] = Z[p] - vz * 0.45;
if (vy < 0) PY[p] = Y[p];
}
}
// ---------- grab, poke, squeeze helpers ----------
function startGrab(p) {
grab = p; IM[p] = 0; TX = X[p]; TY = Y[p]; TZ = Z[p];
dragPlane.setFromNormalAndCoplanarPoint(camera.getWorldDirection(new THREE.Vector3()),
new THREE.Vector3(X[p], Y[p], Z[p]));
}
function endGrab() { if (grab >= 0) { IM[grab] = 1; grab = -1; } }
function setGrabTarget(x, y, z) {
const p = grab; if (p < 0) return;
const rx = rest[3 * p], ry = rest[3 * p + 1] + Y0, rz = rest[3 * p + 2];
let dx = x - rx, dy = y - ry, dz = z - rz;
const L = Math.hypot(dx, dy, dz), MAXL = 1.5;
if (L > MAXL) { const f = MAXL / L; dx *= f; dy *= f; dz *= f; }
TX = rx + dx; TY = Math.max(FLOOR + 0.03, ry + dy); TZ = rz + dz;
}
function poke(p) {
const cx = X[p], cy = Y[p], cz = Z[p], imp = 2.2 * SUB, RAD = 0.45;
for (let q = 0; q < NP; q++) {
const dx = X[q] - cx, dy = Y[q] - cy, dz = Z[q] - cz;
const d = Math.sqrt(dx * dx + dy * dy + dz * dz);
if (d > RAD) continue;
PY[q] += imp * (1 - d / RAD); // adds downward velocity
}
}
let squeezeT = -1, nextDrop = 0;
function squeeze() { if (squeezeT >= 0) return; squeezeT = 0; nextDrop = 0; }
function updateSqueeze(dt) {
if (squeezeT < 0) { pressS = 0; return; }
squeezeT += dt;
pressS = squeezeT < SQ_DUR ? Math.sin(Math.PI * squeezeT / SQ_DUR) : 0;
if (nextDrop < 4 && squeezeT >= 0.4 + 0.3 * nextDrop) { nextDrop++; spawnDrop(); }
if (squeezeT >= SQ_DUR + 0.5) { squeezeT = -1; pressS = 0; }
}
// ---------- juice drops ----------
const drops = [];
const dropGeo = new THREE.SphereGeometry(0.03, 16, 12);
const dropMat = new THREE.MeshPhysicalMaterial({ color: 0xffb22e, roughness: 0.08, clearcoat: 1, transparent: true, opacity: 0.85 });
function spawnDrop() {
if (juiceLeft <= 0) return;
juiceLeft--; updateJuice();
const i = Math.floor(rnd() * N), p = ringIdx(0, R, i);
const cx = (X[0] + X[1]) * 0.5, cz = (Z[0] + Z[1]) * 0.5;
let ox = X[p] - cx, oz = Z[p] - cz; const l = Math.hypot(ox, oz) || 1; ox /= l; oz /= l;
const m = new THREE.Mesh(dropGeo, dropMat);
m.position.set(X[p] + ox * 0.05, Y[p] + 0.02, Z[p] + oz * 0.05);
scene.add(m);
drops.push({ m, vx: ox * 0.5, vy: 0.5, vz: oz * 0.5, landed: false });
while (drops.length > 16) scene.remove(drops.shift().m);
}
function updateDrops(dt) {
for (const d of drops) {
if (d.landed) continue;
d.vy += GRAV * 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 <= PLATE_TOP + 0.03) {
d.m.position.y = PLATE_TOP + 0.01; d.landed = true;
d.m.scale.set(1.8, 0.35, 1.8); d.vx = d.vy = d.vz = 0;
}
}
}
function clearDrops() { for (const d of drops) scene.remove(d.m); drops.length = 0; }
// ---------- three.js scene ----------
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
const canvas = renderer.domElement;
$('app').appendChild(canvas);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf4eee4);
const camera = new THREE.PerspectiveCamera(36, 1, 0.1, 100);
scene.add(new THREE.HemisphereLight(0xffffff, 0xd9c7b0, 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);
Object.assign(sun.shadow.camera, { left: -2.5, right: 2.5, top: 2.5, bottom: -2.5, near: 1, far: 20 });
sun.shadow.bias = -0.0008;
scene.add(sun);
const glow = new THREE.PointLight(0xffb865, 2.2, 8, 2); // warm light behind the slice, shows through the thin parts
glow.position.set(-0.6, 0.5, -1.6);
scene.add(glow);
const plate = new THREE.Mesh(new THREE.CylinderGeometry(1.45, 1.3, 0.08, 96),
new THREE.MeshStandardMaterial({ color: 0xfbfbf8, roughness: 0.35 }));
plate.position.y = -0.04; plate.receiveShadow = true; scene.add(plate);
const table = new THREE.Mesh(new THREE.CircleGeometry(12, 64),
new THREE.MeshStandardMaterial({ color: 0xe6ddcf, roughness: 0.9 }));
table.rotation.x = -Math.PI / 2; table.position.y = -0.085; table.receiveShadow = true; scene.add(table);
// orient a triangle so its normal points away from the body centre (origin in rest space)
function orientPush(arr, a, b, c, out) {
const ux = arr[3 * b] - arr[3 * a], uy = arr[3 * b + 1] - arr[3 * a + 1], uz = arr[3 * b + 2] - arr[3 * a + 2];
const vx = arr[3 * c] - arr[3 * a], vy = arr[3 * c + 1] - arr[3 * a + 1], vz = arr[3 * c + 2] - arr[3 * a + 2];
const nx = uy * vz - uz * vy, ny = uz * vx - ux * vz, nz = ux * vy - uy * vx;
const mx = arr[3 * a] + arr[3 * b] + arr[3 * c];
const my = arr[3 * a + 1] + arr[3 * b + 1] + arr[3 * c + 1];
const mz = arr[3 * a + 2] + arr[3 * b + 2] + arr[3 * c + 2];
if (nx * mx + ny * my + nz * mz < 0) out.push(a, c, b); else out.push(a, b, c);
}
// jelly body: top and bottom faces plus the rim (the rim is the white pith)
const jIdx = [];
function quadPush(a, b, c, d) { orientPush(rest, a, b, c, jIdx); orientPush(rest, a, c, d, jIdx); }
for (let L = 0; L < 2; L++) {
for (let i = 0; i < N; i++) orientPush(rest, L, ringIdx(L, 1, i), ringIdx(L, 1, i + 1), jIdx);
for (let k = 1; k < R; k++) for (let i = 0; i < N; i++)
quadPush(ringIdx(L, k, i), ringIdx(L, k + 1, i), ringIdx(L, k + 1, i + 1), ringIdx(L, k, i + 1));
}
for (let i = 0; i < N; i++) quadPush(ringIdx(0, R, i), ringIdx(1, R, i), ringIdx(1, R, i + 1), ringIdx(0, R, i + 1));
const jPos = new Float32Array(NP * 3), jCol = new Float32Array(NP * 3);
const cJuiceCore = new THREE.Color(0xffc266), cJuiceEdge = new THREE.Color(0xff8a24), cPith = new THREE.Color(0xfff6e6);
const tmpC = new THREE.Color();
const segTint = [];
for (let s = 0; s < S; s++) segTint.push(0.93 + 0.1 * rnd());
function paint(p, c) { jCol[3 * p] = c.r; jCol[3 * p + 1] = c.g; jCol[3 * p + 2] = c.b; }
for (let L = 0; L < 2; L++) {
paint(L, cJuiceCore);
for (let k = 1; k <= R; k++) for (let i = 0; i < N; i++) {
const p = ringIdx(L, k, i);
if (k === R) paint(p, cPith);
else {
tmpC.copy(cJuiceCore).lerp(cJuiceEdge, k / R).multiplyScalar(segTint[Math.floor(i / SEG)]);
paint(p, tmpC);
}
}
}
const jGeo = new THREE.BufferGeometry();
jGeo.setAttribute('position', new THREE.BufferAttribute(jPos, 3));
jGeo.setAttribute('color', new THREE.BufferAttribute(jCol, 3));
jGeo.setIndex(jIdx);
const jMat = new THREE.MeshPhysicalMaterial({
vertexColors: true, transparent: true, opacity: 0.72, roughness: 0.12,
clearcoat: 1, clearcoatRoughness: 0.08, side: THREE.DoubleSide, depthWrite: false,
emissive: 0xff6a1a, emissiveIntensity: 0.12
});
const jelly = new THREE.Mesh(jGeo, jMat);
jelly.castShadow = true; jelly.renderOrder = 1; jelly.frustumCulled = false;
scene.add(jelly);
// white pith membranes between the segments (radial walls)
const memCount = S * 2 * (R + 1);
const memMap = new Int32Array(memCount);
const memIdx = [];
for (let b = 0; b < S; b++) {
const iB = b * SEG, base = b * 2 * (R + 1);
for (let L = 0; L < 2; L++) for (let k = 0; k <= R; k++) memMap[base + L * (R + 1) + k] = pidx(L, k, iB);
for (let k = 0; k < R; k++) {
const t0 = base + k, t1 = base + k + 1, b1 = base + (R + 1) + k + 1, b0 = base + (R + 1) + k;
memIdx.push(t0, t1, b1, t0, b1, b0);
}
}
const memPos = new Float32Array(memCount * 3);
const memGeo = new THREE.BufferGeometry();
memGeo.setAttribute('position', new THREE.BufferAttribute(memPos, 3));
memGeo.setIndex(memIdx);
const memMesh = new THREE.Mesh(memGeo, new THREE.MeshStandardMaterial({
color: 0xfff7ea, roughness: 0.7, side: THREE.DoubleSide, transparent: true, opacity: 0.85, depthWrite: false
}));
memMesh.renderOrder = 2; memMesh.frustumCulled = false;
scene.add(memMesh);
// bright peel: a band just outside the rim
const peelRest = new Float64Array(2 * N * 3);
for (let i = 0; i < N; i++) {
const a = i * Math.PI * 2 / N, c = Math.cos(a) * (BODY_R + PEEL_OFF), s = Math.sin(a) * (BODY_R + PEEL_OFF);
peelRest[3 * i] = c; peelRest[3 * i + 1] = HALF_H + 0.012; peelRest[3 * i + 2] = s;
peelRest[3 * (N + i)] = c; peelRest[3 * (N + i) + 1] = -(HALF_H + 0.012); peelRest[3 * (N + i) + 2] = s;
}
const peelIdx = [];
for (let i = 0; i < N; i++) {
const j = (i + 1) % N;
orientPush(peelRest, i, j, N + j, peelIdx);
orientPush(peelRest, i, N + j, N + i, peelIdx);
}
const peelGeo = new THREE.BufferGeometry();
peelGeo.setAttribute('position', new THREE.BufferAttribute(new Float32Array(peelRest), 3));
peelGeo.setIndex(peelIdx);
const peelMesh = new THREE.Mesh(peelGeo, new THREE.MeshPhysicalMaterial({
color: 0xff7b12, roughness: 0.32, clearcoat: 0.9, clearcoatRoughness: 0.15, side: THREE.DoubleSide
}));
peelMesh.castShadow = true; peelMesh.frustumCulled = false;
scene.add(peelMesh);
// tiny juice cells (vesicles), each placed inside a segment and tied to the mesh
const VPS = 60, VN = S * VPS;
const vesList = [];
for (let s = 0; s < S; s++) for (let q = 0; q < VPS; q++) vesList.push({
k: 1 + Math.floor(rnd() * (R - 1)), i: s * SEG + Math.floor(rnd() * SEG),
u: 0.15 + 0.7 * rnd(), v: 0.15 + 0.7 * rnd(), t: 0.2 + 0.6 * rnd(), sz: 0.018 + 0.016 * rnd()
});
const vesMesh = new THREE.InstancedMesh(new THREE.SphereGeometry(1, 8, 6), new THREE.MeshPhysicalMaterial({
color: 0xffc24a, emissive: 0xff7a00, emissiveIntensity: 0.25, roughness: 0.15,
transparent: true, opacity: 0.8, depthWrite: false
}), VN);
vesMesh.renderOrder = 3; vesMesh.frustumCulled = false;
scene.add(vesMesh);
const dummy = new THREE.Object3D();
const tA = new Float64Array(3), tB = new Float64Array(3);
function gridLerp(L, k, i, u, v, out) {
const a = pidx(L, k, i), b = pidx(L, k, i + 1), c = pidx(L, k + 1, i), d = pidx(L, k + 1, i + 1);
const x0 = X[a] + (X[b] - X[a]) * u, y0 = Y[a] + (Y[b] - Y[a]) * u, z0 = Z[a] + (Z[b] - Z[a]) * u;
const x1 = X[c] + (X[d] - X[c]) * u, y1 = Y[c] + (Y[d] - Y[c]) * u, z1 = Z[c] + (Z[d] - Z[c]) * u;
out[0] = x0 + (x1 - x0) * v; out[1] = y0 + (y1 - y0) * v; out[2] = z0 + (z1 - z0) * v;
}
function updateGeometry() {
const jp = jGeo.attributes.position.array;
for (let p = 0; p < NP; p++) { jp[3 * p] = X[p]; jp[3 * p + 1] = Y[p]; jp[3 * p + 2] = Z[p]; }
jGeo.attributes.position.needsUpdate = true;
jGeo.computeVertexNormals();
for (let m = 0; m < memCount; m++) {
const p = memMap[m];
memPos[3 * m] = X[p]; memPos[3 * m + 1] = Y[p]; memPos[3 * m + 2] = Z[p];
}
memGeo.attributes.position.needsUpdate = true;
memGeo.computeVertexNormals();
const cx = (X[0] + X[1]) * 0.5, cz = (Z[0] + Z[1]) * 0.5;
const pp = peelGeo.attributes.position.array;
for (let i = 0; i < N; i++) {
const t = ringIdx(0, R, i), b = ringIdx(1, R, i);
let ox = X[t] - cx, oz = Z[t] - cz; const l = Math.hypot(ox, oz) || 1; ox /= l; oz /= l;
pp[3 * i] = X[t] + ox * PEEL_OFF; pp[3 * i + 1] = Y[t] + 0.012; pp[3 * i + 2] = Z[t] + oz * PEEL_OFF;
pp[3 * (N + i)] = X[b] + ox * PEEL_OFF; pp[3 * (N + i) + 1] = Y[b] - 0.012; pp[3 * (N + i) + 2] = Z[b] + oz * PEEL_OFF;
}
peelGeo.attributes.position.needsUpdate = true;
peelGeo.computeVertexNormals();
for (let v = 0; v < VN; v++) {
const q = vesList[v];
gridLerp(0, q.k, q.i, q.u, q.v, tA);
gridLerp(1, q.k, q.i, q.u, q.v, tB);
dummy.position.set(
tA[0] + (tB[0] - tA[0]) * q.t,
tA[1] + (tB[1] - tA[1]) * q.t,
tA[2] + (tB[2] - tA[2]) * q.t);
dummy.scale.setScalar(q.sz);
dummy.updateMatrix();
vesMesh.setMatrixAt(v, dummy.matrix);
}
vesMesh.instanceMatrix.needsUpdate = true;
}
// ---------- camera and resize ----------
function resize() {
const w = window.innerWidth, h = window.innerHeight;
renderer.setSize(w, h, false);
camera.aspect = w / h;
const s = Math.max(1, 1.1 / camera.aspect); // zoom out on phones so the plate fits
camera.position.set(0, 2.6 * s, 4.6 * s);
camera.lookAt(0, 0.1, 0);
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
}
window.addEventListener('resize', resize);
resize();
// ---------- UI: firmness, squeeze, reset, juice text ----------
function updateJuice() {
juiceEl.textContent = juiceLeft > 0 ? 'Juice drops left: ' + juiceLeft : 'Out of juice - press Reset';
}
function setFirm(v) {
STIFF = 0.45 + 0.5 * (v / 100);
firmVal.textContent = Math.round(v);
}
function setFirmUI(v) { firmEl.value = v; setFirm(v); }
firmEl.addEventListener('input', () => setFirm(+firmEl.value));
function resetAll() {
endGrab();
resetBody(LIFT);
squeezeT = -1; pressS = 0;
clearDrops();
juiceLeft = JUICE_MAX; updateJuice();
setFirmUI(50);
}
// ---------- hands-free demo (#demo): 20 s loop, any touch ends it ----------
let demoOn = location.hash === '#demo';
let demoT = 0, demoGrab = -1;
const fired = {};
const DEMO = [
[1.5, () => poke(ringIdx(0, R, 10))],
[3.0, () => { demoGrab = ringIdx(0, R, 10); startGrab(demoGrab); }],
[5.2, () => { endGrab(); }],
[6.6, () => squeeze()],
[16.0, () => resetAll()]
];
function demoTick(dt) {
demoT += dt;
if (demoT >= 20) { demoT -= 20; for (const k in fired) delete fired[k]; }
for (const [t, fn] of DEMO) if (demoT >= t && !fired[t]) { fired[t] = 1; fn(); }
if (grab >= 0 && grab === demoGrab && demoT >= 3 && demoT < 5.2) {
const p = Math.min(1, Math.max(0, (demoT - 3) / 1.4));
const e = p * p * (3 - 2 * p);
setGrabTarget(rest[3 * grab] + 0.45 * e, rest[3 * grab + 1] + Y0 + 0.3 * e, rest[3 * grab + 2] + 0.55 * e);
}
if (demoT >= 9 && demoT < 11.5) setFirmUI(50 + (10 - 50) * (demoT - 9) / 2.5);
else if (demoT >= 11.5 && demoT < 14) setFirmUI(10 + (95 - 10) * (demoT - 11.5) / 2.5);
}
function endDemo() {
if (!demoOn) return;
demoOn = false; endGrab(); demoGrab = -1;
hintEl.textContent = DEFAULT_HINT;
}
if (demoOn) hintEl.textContent = 'Demo - touch the jelly to play';
window.addEventListener('pointerdown', endDemo, true);
squeezeBtn.addEventListener('click', () => { endDemo(); squeeze(); });
resetBtn.addEventListener('click', () => { endDemo(); resetAll(); });
// ---------- touch and mouse: grab and drag, or poke with a tap ----------
const ray = new THREE.Raycaster();
const ndc = new THREE.Vector2();
const dragPlane = new THREE.Plane();
const tmpV = new THREE.Vector3();
let ptrId = -1, downX = 0, downY = 0, downT = 0, moved = 0, pokeIdx = -1;
function setRay(e) {
const r = canvas.getBoundingClientRect();
ndc.set(((e.clientX - r.left) / r.width) * 2 - 1, -((e.clientY - r.top) / r.height) * 2 + 1);
ray.setFromCamera(ndc, camera);
}
function pickParticle() {
const o = ray.ray.origin, d = ray.ray.direction;
let best = -1, bestS = Infinity;
for (let p = 0; p < NP; p++) {
const vx = X[p] - o.x, vy = Y[p] - o.y, vz = Z[p] - o.z;
const t = vx * d.x + vy * d.y + vz * d.z;
if (t < 0) continue;
const qx = vx - d.x * t, qy = vy - d.y * t, qz = vz - d.z * t;
const d2 = qx * qx + qy * qy + qz * qz;
if (d2 > PICK_R * PICK_R) continue;
const sc = d2 + 0.003 * t; // prefer the front surface
if (sc < bestS) { bestS = sc; best = p; }
}
return best;
}
canvas.addEventListener('pointerdown', e => {
if (ptrId !== -1) return;
ptrId = e.pointerId;
try { canvas.setPointerCapture(e.pointerId); } catch (err) {}
downX = e.clientX; downY = e.clientY; downT = performance.now(); moved = 0;
setRay(e);
const p = pickParticle();
pokeIdx = p;
if (p >= 0) startGrab(p);
});
canvas.addEventListener('pointermove', e => {
if (e.pointerId !== ptrId) return;
moved = Math.max(moved, Math.hypot(e.clientX - downX, e.clientY - downY));
if (grab < 0) return;
setRay(e);
if (ray.ray.intersectPlane(dragPlane, tmpV)) setGrabTarget(tmpV.x, tmpV.y, tmpV.z);
});
function endPointer(e) {
if (e.pointerId !== ptrId) return;
endGrab();
if (pokeIdx >= 0 && moved < 12 && performance.now() - downT < 450) poke(pokeIdx);
ptrId = -1; pokeIdx = -1;
}
canvas.addEventListener('pointerup', endPointer);
canvas.addEventListener('pointercancel', endPointer);
// ---------- main loop ----------
updateJuice();
resetBody(LIFT);
setFirm(50);
let last = performance.now(), acc = 0;
function frame(now) {
const dt = Math.min(0.05, (now - last) / 1000);
last = now;
if (demoOn) demoTick(dt);
updateSqueeze(dt);
acc += dt;
let n = 0;
while (acc >= SUB && n < 12) { simStep(SUB); acc -= SUB; n++; }
if (n >= 12) acc = 0;
updateGeometry();
updateDrops(dt);
renderer.render(scene, camera);
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
window.addEventListener('error', e => { hintEl.textContent = 'Could not start: ' + (e.message || 'error'); });
</script>
</body>
</html>
