Back to the app
The code
Jelly Orange Slice by GPT-6.2 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">
<title>Citrus Study — Jelly Lab</title>
<style>
:root{color-scheme:dark;--ink:#fff5e8;--muted:#c5aaa0;--accent:#ff9a57;--line:#ffffff24}
*{box-sizing:border-box}
body{margin:0;background:#201515;color:var(--ink);font:14px system-ui,sans-serif}
main{width:min(1280px,100%);min-height:100svh;margin:auto;padding:24px 34px 18px;display:flex;flex-direction:column}
header{display:flex;justify-content:space-between;align-items:center;gap:12px;margin-bottom:18px}
.brand{display:flex;align-items:center;gap:10px;font-weight:750;letter-spacing:.05em}
.brand svg{width:26px;height:26px;color:var(--accent)}
.tag{font:600 10px system-ui;letter-spacing:.15em;text-transform:uppercase;color:var(--muted);display:flex;align-items:center;gap:8px}
.tag::before{content:"";width:6px;height:6px;border-radius:100%;background:#ffc28e;box-shadow:0 0 11px #ff995c}
.stage{position:relative;isolation:isolate;overflow:hidden;border:1px solid #745048;border-radius:24px;min-height:500px;height:calc(100svh - 186px);max-height:790px;background:radial-gradient(ellipse at 58% 49%,#97614b 0%,#643e35 45%,#382424 95%)}
canvas.scene{width:100%;height:100%;display:block;touch-action:none;cursor:grab;outline:none}
canvas.scene:active{cursor:grabbing}
.intro{position:absolute;z-index:1;left:clamp(20px,4vw,56px);top:clamp(23px,5vw,55px);pointer-events:none}
h1{font:normal clamp(39px,6vw,88px)/.91 Georgia,serif;letter-spacing:-.07em;margin:0;text-shadow:0 2px 20px #43272244}
h1 em{display:block;font-weight:400;color:#ffc599}
.intro p{color:#f2d6c7;font-size:13px;margin:16px 0 0}
.hint{position:absolute;right:17px;bottom:16px;pointer-events:none;border:1px solid #ffe8d23d;border-radius:30px;padding:8px 12px;background:#2d17184d;color:#fff0e1;font-size:11px;letter-spacing:.035em}
.controls{display:flex;align-items:center;gap:10px;margin-top:12px;padding:10px;background:#302122;border:1px solid var(--line);border-radius:17px}
button{min-height:48px;border-radius:10px;border:1px solid var(--line);padding:0 18px;font:700 13px system-ui;color:var(--ink);background:#3c2a2a;cursor:pointer}
button:active{transform:translateY(1px)}
button:focus-visible,input:focus-visible{outline:2px solid #ffe0a8;outline-offset:3px}
#squeeze{background:#ffad73;color:#321e1b;border-color:#ffad73}
.firm{display:grid;grid-template-columns:auto minmax(65px,1fr) 36px;align-items:center;gap:13px;margin-left:auto;padding:0 8px;width:min(390px,48%);font-size:12px;color:#f8e6d8}
output{color:#ffc396;text-align:right;font-variant-numeric:tabular-nums}
input{width:100%;accent-color:#ffad73;margin:0;min-height:36px;cursor:pointer}
#error{position:absolute;inset:auto 16px 65px;text-align:center}
@media(max-width:650px){main{padding:14px 12px}header{margin-bottom:12px}.stage{height:calc(100svh - 164px);min-height:340px;border-radius:17px}.intro p{margin-top:10px}.controls{gap:7px;padding:7px}.firm{width:auto;flex:1;grid-template-columns:1fr auto;gap:0 5px;padding:0 3px}.firm input{grid-row:2;grid-column:1/-1;min-height:25px}.firm output{grid-row:1;grid-column:2}button{padding:0 12px}.hint{right:10px;bottom:10px}}
</style>
</head>
<body>
<main>
<header><div class="brand"><svg viewBox="0 0 32 32" fill="none" aria-hidden="true"><circle cx="16" cy="16" r="13" stroke="currentColor" stroke-width="1.7"/><path d="M16 6v20M6 16h20M9 9l14 14M23 9 9 23" stroke="currentColor" stroke-width="1.3"/><circle cx="16" cy="16" r="2.7" fill="currentColor"/></svg> Jelly Lab</div><div class="tag" id="mode">Interactive study</div></header>
<section class="stage" id="stage" aria-label="Interactive 3D orange jelly">
<div class="intro"><h1>Citrus <em>Jelly</em></h1><p>Tap to poke. Drag to stretch.</p></div>
<div class="hint" id="hint" aria-live="polite">Ready to wobble</div>
<div id="error" hidden>3D rendering is unavailable in this browser.</div>
</section>
<div class="controls" aria-label="Jelly controls">
<button id="squeeze" type="button">Squeeze</button>
<button id="reset" type="button">Reset</button>
<label class="firm" for="firmness"><span>Firmness</span><input id="firmness" type="range" min="0" max="100" value="55"><output id="value" for="firmness">55%</output></label>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.min.js"></script>
<script>
(() => {
'use strict';
const stage=document.getElementById('stage'), hint=document.getElementById('hint'), control=document.getElementById('firmness');
const readout=document.getElementById('value'), mode=document.getElementById('mode');
function boot(){
const T=THREE, tau=2*Math.PI;
const renderer=new T.WebGLRenderer({alpha:true,antialias:true});
renderer.setPixelRatio(Math.min(devicePixelRatio,1.8));
renderer.toneMapping=T.ACESFilmicToneMapping;
renderer.toneMappingExposure=1.18;
renderer.domElement.className='scene';
stage.prepend(renderer.domElement);
const scene=new T.Scene();
const camera=new T.PerspectiveCamera(34,1,.1,50);
function canvasTexture(w,h,paint){
const c=document.createElement('canvas'); c.width=w;c.height=h;
paint(c.getContext('2d'),w,h);
const tex=new T.CanvasTexture(c);tex.colorSpace=T.SRGBColorSpace;return tex;
}
let seed=9127;
function rand(){seed=(Math.imul(seed,1664525)+1013904223)>>>0;return seed/4294967296;}
const studio=canvasTexture(512,256,(ctx,w,h)=>{
ctx.fillStyle='#62545d';ctx.fillRect(0,0,w,h);
let g=ctx.createLinearGradient(0,15,0,h);
g.addColorStop(0,'#e9c8ad');g.addColorStop(.48,'#8c6664');g.addColorStop(1,'#30282f');
ctx.fillStyle=g;ctx.fillRect(0,0,w,h);
for(const [x,y,a,b] of [[92,53,89,38],[348,95,117,27],[226,37,31,17]]){
ctx.fillStyle='#fff5e9';ctx.fillRect(x,y,a,b);
}
});
studio.mapping=T.EquirectangularReflectionMapping;
const pmrem=new T.PMREMGenerator(renderer);
scene.environment=pmrem.fromEquirectangular(studio).texture;
studio.dispose();pmrem.dispose();
scene.add(new T.HemisphereLight(0xffeee5,0x683538,2.15));
function light(color,intensity,x,y,z){
const lamp=new T.DirectionalLight(color,intensity);lamp.position.set(x,y,z);scene.add(lamp);
}
light(0xffead8,2.55,-3,6,3);
light(0xffac69,2.7,2,3,-4);
light(0xfff1e9,.8,5,5,1);
const plate=new T.Mesh(new T.LatheGeometry([
[0,.04],[1.22,.04],[1.3,.02],[1.37,.025],[1.43,.075],[1.76,.09],[1.96,.135],[2.17,.21],[2.35,.33],[2.40,.38],[2.39,.415],[2.35,.432],[2.28,.421],[2.12,.344],[1.93,.27],[1.73,.222],[1.51,.203],[0,.203]
].map(p=>new T.Vector2(...p)),128),new T.MeshPhysicalMaterial({color:0xfff9ef,roughness:.22,clearcoat:.65,side:T.DoubleSide}));
scene.add(plate);
function shadow(radius,opacity,y,scaleZ){
const texture=canvasTexture(128,128,(ctx,w,h)=>{
const g=ctx.createRadialGradient(w/2,h/2,0,w/2,h/2,w/2);
g.addColorStop(0,'rgba(74,24,14,0.85)');g.addColorStop(.65,'rgba(74,24,14,0.45)');g.addColorStop(1,'rgba(74,24,14,0)');
ctx.fillStyle=g;ctx.fillRect(0,0,w,h);
});
const mesh=new T.Mesh(new T.PlaneGeometry(radius*2,radius*2),new T.MeshBasicMaterial({map:texture,transparent:true,opacity,depthWrite:false,polygonOffset:true,polygonOffsetFactor:-2}));
mesh.rotation.x=-Math.PI/2;mesh.position.y=y;mesh.scale.y=scaleZ;scene.add(mesh);return mesh;
}
shadow(3.05,.74,.013,.83).position.z=.24;
shadow(1.85,.53,.211,.95);
const cellTexture=canvasTexture(512,512,(ctx,w,h)=>{
const count=53, points=[];
for(let y=0;y<count;y++)for(let x=0;x<count;x++)points.push([x+(rand()-.5)*.82,y+(rand()-.5)*.82,rand()]);
const im=ctx.createImageData(w,h);
for(let y=0;y<h;y++)for(let x=0;x<w;x++){
const px=x/w*(count-4)+1.5,py=y/h*(count-4)+1.5;
let first=9,second=9,cx=0,cy=0,tint=0;
for(let j=-1;j<=1;j++)for(let i=-1;i<=1;i++){
const [a,b,t]=points[(Math.floor(py)+j)*count+Math.floor(px)+i];
const dx=px-a,dy=(py-b)*1.19,d=dx*dx+dy*dy;
if(d<first){second=first;first=d;cx=dx;cy=dy;tint=t;}else if(d<second){second=d;}
}
const edge=Math.max(0,1-(second-first)*7);
const glint=Math.exp(-33*((cx+.14)**2+(cy+.16)**2));
const offset=(y*w+x)*4;
im.data[offset]=255;
im.data[offset+1]=127+56*tint-44*edge+53*glint;
im.data[offset+2]=12+27*tint-7*edge+89*glint;
im.data[offset+3]=255;
}
ctx.putImageData(im,0,0);
});
cellTexture.anisotropy=Math.min(8,renderer.capabilities.getMaxAnisotropy());
const peelTexture=canvasTexture(512,128,(ctx,w,h)=>{
ctx.fillStyle='#ff8a22';ctx.fillRect(0,0,w,h);
for(let i=0;i<4900;i++){
const x=rand()*w,y=rand()*h,r=.25+rand()*1.05;
ctx.fillStyle=i%3===0?'#ffca5c':'#d65a1c';
ctx.globalAlpha=.12+rand()*.35;ctx.beginPath();ctx.arc(x,y,r,0,tau);ctx.fill();
}
});
peelTexture.wrapS=T.RepeatWrapping;
const peel=new T.MeshPhysicalMaterial({map:peelTexture,roughness:.33,clearcoat:.65,bumpMap:peelTexture,bumpScale:.027,side:T.DoubleSide});
const pith=new T.MeshPhysicalMaterial({color:0xffe6bc,roughness:.56,side:T.DoubleSide});
const flesh=new T.MeshPhysicalMaterial({color:0xffad50,map:cellTexture,roughness:.28,metalness:0,clearcoat:1,clearcoatRoughness:.13,transmission:.22,thickness:.32,ior:1.36,attenuationColor:new T.Color(0xff8924),attenuationDistance:.75,emissive:0xf76509,emissiveIntensity:.11,side:T.DoubleSide,bumpMap:cellTexture,bumpScale:.014});
const skin=new T.MeshPhysicalMaterial({color:0xffab47,map:cellTexture,roughness:.25,transmission:.35,thickness:.2,clearcoat:1,ior:1.35,emissive:0xe85b04,emissiveIntensity:.13,side:T.DoubleSide});
const floor=.223, R=1.61, H=.44, N=7, L=3, span=3.34;
const originX=-span/2, originZ=-span/2, step=span/(N-1);
const nodes=[], springs=[], deformables=[];
const index=(x,y,z)=>(y*N+z)*N+x;
for(let y=0;y<L;y++)for(let z=0;z<N;z++)for(let x=0;x<N;x++){
const rest=new T.Vector3(originX+x*step,floor+y*H/(L-1),originZ+z*step);
nodes.push({rest,p:rest.clone(),v:new T.Vector3(),f:new T.Vector3()});
}
for(let y=0;y<L;y++)for(let z=0;z<N;z++)for(let x=0;x<N;x++){
const a=nodes[index(x,y,z)];
for(let dy=0;dy<=1;dy++)for(let dz=-1;dz<=1;dz++)for(let dx=-1;dx<=1;dx++){
if(dy===0&&(dz<0||(dz===0&&dx<=0)))continue;
if(x+dx<0||x+dx>=N||z+dz<0||z+dz>=N||y+dy>=L)continue;
const b=nodes[index(x+dx,y+dy,z+dz)];
springs.push({a,b,length:a.rest.distanceTo(b.rest),weight:1/(Math.abs(dx)+dy+Math.abs(dz))**.65});
}
}
function bind(geometry,mat){
const attr=geometry.attributes.position, weights=[];
for(let i=0;i<attr.count;i++){
const x=Math.min(N-1,Math.max(0,(attr.getX(i)-originX)/step));
const z=Math.min(N-1,Math.max(0,(attr.getZ(i)-originZ)/step));
const y=Math.min(L-1,Math.max(0,(attr.getY(i)-floor)*(L-1)/H));
const ix=Math.min(N-2,Math.floor(x)), iz=Math.min(N-2,Math.floor(z)), iy=Math.min(L-2,Math.floor(y));
const w=[];
for(let j=0;j<2;j++)for(let k=0;k<2;k++)for(let l=0;l<2;l++){
w.push([nodes[index(ix+l,iy+j,iz+k)].p,(l?x-ix:1-x+ix)*(j?y-iy:1-y+iy)*(k?z-iz:1-z+iz)]);
}
weights.push(w);
}
const mesh=new T.Mesh(geometry,mat);
mesh.frustumCulled=false;scene.add(mesh);deformables.push({geometry,attr,weights});return mesh;
}
function patch(fn,nu,nv,mat,flip=false,unwrap=false){
const pos=[],uv=[],indices=[];
for(let j=0;j<=nv;j++)for(let i=0;i<=nu;i++){
const u=i/nu,v=j/nv,[x,y,z]=fn(u,v);
pos.push(x,y,z);uv.push(...(unwrap?[u,v]:[.5+x/3.22,.5+z/3.22]));
if(i<nu&&j<nv){
const a=j*(nu+1)+i,b=a+1,c=a+nu+1,d=c+1;
if(flip)indices.push(a,c,b,b,c,d);else indices.push(a,b,c,b,d,c);
}
}
const geo=new T.BufferGeometry();
geo.setAttribute('position',new T.Float32BufferAttribute(pos,3));
geo.setAttribute('uv',new T.Float32BufferAttribute(uv,2));
geo.setIndex(indices);geo.computeVertexNormals();
return bind(geo,mat);
}
const polar=(r,a,h)=>[r*Math.cos(a),floor+h,r*Math.sin(a)];
// A thin albedo layer below the beveled fruit sections connects their membranes.
patch((u,v)=>polar(1.595*v,tau*u,H-.045),112,12,pith);
const profile=[[1.49,0],[1.55,.012],[1.594,.044],[1.611,.095],[1.614,.31],[1.603,.378],[1.575,.421],[1.52,.438],[1.475,.44]];
function rind(u,v){
const s=v*(profile.length-1),i=Math.min(profile.length-2,Math.floor(s)),f=s-i;
const [a,b]=profile[i],[c,d]=profile[i+1];
return polar(a+(c-a)*f,tau*u+.005*Math.sin(13*tau*u),b+(d-b)*f);
}
patch((u,v)=>rind(u,v*7/8),128,28,peel,true,true);
patch((u,v)=>rind(u,(7+v)/8),128,4,pith,true);
patch((u,v)=>polar(1.50*v,tau*u,.006),96,12,skin,true);
const wedgeCount=10;
for(let i=0;i<wedgeCount;i++){
const start=(i+.025)*tau/wedgeCount-.11, end=(i+.975)*tau/wedgeCount-.11;
const outer=1.482+.018*Math.sin(i*6.14+1);
const section=(u,v,level)=>{
const inner=.075+.023*Math.sin(i*2.9);
const round=Math.sin(Math.PI*u)**.42;
const r=inner+(outer-inner)*v-.065*(1-round)*v**6;
const center=(start+end)/2, arc=(end-start)/2;
const a=center+arc*(2*u-1)*(1-.15*(1-v)**4);
const edge=Math.min(1,Math.min(u,1-u)*15,v*18,(1-v)*17);
const crown=.399+.037*Math.sin(edge*Math.PI/2)+.004*Math.sin(a*11+v*7);
return polar(r,a,level===1?crown:.009);
};
patch((u,v)=>section(u,v,1),15,16,flesh);
// Segment walls supply the transparent sides and the small meniscus at each seam.
for(const [edge,flip] of [[(u)=>[u,1],true],[(u)=>[0,u],true],[(u)=>[1,u],false],[(u)=>[u,0],false]]){
patch((u,v)=>{
const [a,b]=edge(u),[x,y,z]=section(a,b,1);
return [x,floor+.009+(y-floor-.009)*v,z];
},15,5,skin,flip);
}
}
const juiceMat=new T.MeshPhysicalMaterial({color:0xff9f32,metalness:0,roughness:.12,transmission:.42,thickness:.1,clearcoat:1,ior:1.34,emissive:0xda6208,emissiveIntensity:.14});
const drops=[];
const dropGeo=new T.SphereGeometry(1,16,12);
function releaseDrop(n){
const a=.48+n*.5+(rand()-.5)*.24, size=.042+rand()*.029;
const mesh=new T.Mesh(dropGeo,juiceMat);scene.add(mesh);
mesh.position.set(Math.cos(a)*1.52,.41,Math.sin(a)*1.52);
drops.push({mesh,size,v:new T.Vector3(Math.cos(a)*(.62+rand()*.3),.33+rand()*.35,Math.sin(a)*.73),landed:false,age:0});
}
let grab=null,squeezeTime=-1,dropCount=0,settle=0;
const dt=1/120;
function status(message){if(hint.textContent!==message)hint.textContent=message;}
function squeeze(){
if(squeezeTime>=0)return;
squeezeTime=0;dropCount=0;settle=2.2;status('Squeezing');
document.getElementById('squeeze').setAttribute('aria-pressed','true');
while(drops.length>24)scene.remove(drops.shift().mesh);
}
function reset(){
for(const n of nodes){n.p.copy(n.rest);n.v.set(0,0,0);}
for(const d of drops)scene.remove(d.mesh);
drops.length=0;grab=null;squeezeTime=-1;settle=0;
document.getElementById('squeeze').setAttribute('aria-pressed','false');
status('Ready to wobble');
}
function physics(){
const f=Number(control.value)/100;
let press=0;
if(squeezeTime>=0){
squeezeTime+=dt;
press=Math.min(1,squeezeTime/.5,Math.max(0,(1.65-squeezeTime)/.48));
press=press*press*(3-2*press);
if(dropCount<6&&squeezeTime>.46+.11*dropCount)releaseDrop(dropCount++);
if(squeezeTime>=1.65){
squeezeTime=-1;status('Settling');
document.getElementById('squeeze').setAttribute('aria-pressed','false');
}
}
const stiffness=105+f*245, anchor=19+f*24;
for(const n of nodes){
const height=(n.rest.y-floor)/H;
const spread=1+press*(.035+.047*height);
n.f.set(
(n.rest.x*spread-n.p.x)*anchor,
(n.rest.y-.21*press*height-n.p.y)*(anchor+press*95),
(n.rest.z*spread-n.p.z)*anchor
);
}
for(const s of springs){
const {a,b,length,weight}=s;
const x=b.p.x-a.p.x,y=b.p.y-a.p.y,z=b.p.z-a.p.z,d=Math.hypot(x,y,z);
if(d<.0001)continue;
const speed=((b.v.x-a.v.x)*x+(b.v.y-a.v.y)*y+(b.v.z-a.v.z)*z)/d;
const k=((d-length)*stiffness+speed*1.2)*weight/d;
a.f.x+=x*k;a.f.y+=y*k;a.f.z+=z*k;b.f.x-=x*k;b.f.y-=y*k;b.f.z-=z*k;
}
if(grab){
for(const {n,w} of grab.nodes){
n.f.x+=(grab.x-(n.p.x-n.rest.x))*155*w;
n.f.y+=(grab.y-(n.p.y-n.rest.y))*155*w;
n.f.z+=(grab.z-(n.p.z-n.rest.z))*155*w;
}
}
const drag=Math.exp(-(3.1+f*.8)*dt);
for(const n of nodes){
n.v.addScaledVector(n.f,dt).multiplyScalar(drag).clampLength(0,6);
n.p.addScaledVector(n.v,dt);
if(n.p.y<floor){n.p.y=floor;n.v.y=Math.max(0,n.v.y);n.v.x*=.98;n.v.z*=.98;}
}
if(!grab&&squeezeTime<0&&settle>0){
settle-=dt;if(settle<=0)status('Ready to wobble');
}
for(const d of drops){
const p=d.mesh.position;
d.age+=dt;
if(!d.landed)d.v.y-=4.9*dt;
p.addScaledVector(d.v,dt);
const radius=Math.hypot(p.x,p.z);
const plateY=.215+Math.max(0,radius-1.55)*.28;
if(p.y<=plateY+d.size*.42){
p.y=plateY+d.size*.42;d.v.y=0;d.landed=true;d.v.multiplyScalar(Math.exp(-dt*(5+radius)));
}
const factor=d.landed?1.3:1;
d.mesh.scale.set(d.size*factor,d.size*(d.landed?.45:1.18),d.size*factor);
}
}
function updateMeshes(){
for(const {geometry,attr,weights} of deformables){
for(let i=0;i<weights.length;i++){
let x=0,y=0,z=0;
for(const [p,w] of weights[i]){x+=p.x*w;y+=p.y*w;z+=p.z*w;}
attr.setXYZ(i,x,y,z);
}
attr.needsUpdate=true;geometry.computeVertexNormals();
}
}
const ray=new T.Raycaster(), ndc=new T.Vector2(), hit=new T.Vector3(), dragPoint=new T.Vector3();
const topPlane=new T.Plane(new T.Vector3(0,1,0),-(floor+H));
function point(event,out){
const rect=renderer.domElement.getBoundingClientRect();
ndc.set(2*(event.clientX-rect.left)/rect.width-1,1-2*(event.clientY-rect.top)/rect.height);
ray.setFromCamera(ndc,camera);return ray.ray.intersectPlane(topPlane,out);
}
function poke(x,z){
grab={x:0,y:-.31,z:0,nodes:[]};
for(const n of nodes){
const radial=Math.hypot(n.rest.x-x,n.rest.z-z);
if(radial>.94)continue;
const height=(n.rest.y-floor)/H;
const w=Math.exp(-radial*radial*4)*(.18+.82*height);
grab.nodes.push({n,w});n.v.y-=1.22*w;
}
status('Poking');
}
function letGo(){
grab=null;settle=1.8;status(squeezeTime>=0?'Squeezing':'Settling');
}
let demo=location.hash.toLowerCase()==='#demo', demoTime=0, demoOrigin=null;
if(demo)mode.textContent='Demo loop';
function endDemo(){
if(!demo)return;
demo=false;mode.textContent='Interactive study';
if(grab)letGo();
}
document.addEventListener('pointerdown',endDemo,{capture:true});
const view=renderer.domElement;
let pointer=null,startX=0,startY=0,downAt=0,quickRelease=0;
view.addEventListener('pointerdown',event=>{
if(pointer!==null||!point(event,hit)||Math.hypot(hit.x,hit.z)>R+.11)return;
event.preventDefault();
pointer=event.pointerId;quickRelease=0;downAt=performance.now();
startX=event.clientX;startY=event.clientY;demoOrigin=hit.clone();
view.setPointerCapture(pointer);poke(hit.x,hit.z);
});
view.addEventListener('pointermove',event=>{
if(pointer!==event.pointerId||!grab||!point(event,dragPoint))return;
if(Math.hypot(event.clientX-startX,event.clientY-startY)<6)return;
dragPoint.sub(demoOrigin).clampLength(0,.84);
grab.x=dragPoint.x;grab.z=dragPoint.z;grab.y=.1+dragPoint.length()*.33;
status('Stretching');
});
function finish(event){
if(pointer!==event.pointerId)return;
pointer=null;
if(grab&&event.type==='pointerup'&&performance.now()-downAt<100)quickRelease=performance.now()+85;
else if(grab)letGo();
}
view.addEventListener('pointerup',finish);
view.addEventListener('pointercancel',finish);
view.addEventListener('lostpointercapture',finish);
function firmness(v){control.value=v;readout.value=v+'%';}
control.addEventListener('input',()=>{endDemo();firmness(control.value);});
document.getElementById('squeeze').addEventListener('click',()=>{endDemo();squeeze();});
document.getElementById('reset').addEventListener('click',()=>{endDemo();firmness(55);reset();});
function choreograph(delta){
const before=demoTime;demoTime+=delta;
const reached=t=>before<t&&demoTime>=t;
if(reached(.9))poke(.72,.52);
if(reached(1.24))letGo();
if(reached(3.5))poke(-.63,.67);
if(grab&&demoTime>=3.5&&demoTime<5.7){
const p=Math.min(1,(demoTime-3.5)/1.7);
grab.x=-.7*p;grab.z=.4*p;grab.y=.37*p;status('Stretching');
}
if(reached(5.7))letGo();
if(reached(8.1))squeeze();
if(reached(11))firmness(82);
if(reached(12))poke(.45,.63);
if(reached(12.3))letGo();
if(reached(14.2))firmness(23);
if(reached(15.1))poke(.45,.63);
if(reached(15.43))letGo();
if(reached(19)){firmness(55);reset();}
if(demoTime>=20)demoTime=0;
}
function resize(){
const w=stage.clientWidth,h=stage.clientHeight;
if(!w||!h)return;
renderer.setSize(w,h,false);
camera.aspect=w/h;
const compact=w<650;
const distance=Math.max(compact?9.5:8.8,8.15/camera.aspect);
camera.position.set(distance*.38,distance*.62,distance*.68);
camera.lookAt(0,.24,0);
const shiftX=compact?0:Math.min(w*.09,103);
const shiftY=compact?Math.min(h*.11,52):Math.min(h*.07,38);
camera.setViewOffset(w,h,-shiftX,-shiftY,w,h);
}
new ResizeObserver(resize).observe(stage);resize();
let last=performance.now(), accumulator=0;
function frame(now){
const elapsed=Math.min(.045,(now-last)/1000);last=now;
if(demo)choreograph(elapsed);
if(quickRelease&&now>=quickRelease){quickRelease=0;if(grab)letGo();}
accumulator+=elapsed;
while(accumulator>=dt){physics();accumulator-=dt;}
updateMeshes();renderer.render(scene,camera);requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
}
try{boot();}catch(error){document.getElementById('error').hidden=false;console.error(error);}
})();
</script>
</body>
</html>
