Back to the app
The code
Pelican on a Bike by Claude Opus 5.5
Exactly as the AI wrote it. Shown as text, so nothing runs on this page.
index.html
works<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>Pelican Promenade</title>
<style>
*{box-sizing:border-box}
html,body{margin:0;height:100%;overflow:hidden;background:#bfe8f8;font-family:system-ui,-apple-system,"Segoe UI",Roboto,sans-serif;-webkit-tap-highlight-color:transparent;overscroll-behavior:none}
#app{position:fixed;inset:0;display:flex;flex-direction:column}
#stage{position:relative;flex:1 1 auto;min-height:0;overflow:hidden;touch-action:none;cursor:pointer;user-select:none;-webkit-user-select:none}
#scene{position:absolute;inset:0;width:100%;height:100%;display:block;background:#bfe8f8}
#hint{position:absolute;left:50%;top:12px;transform:translateX(-50%);background:rgba(255,255,255,.9);color:#1f3a52;font:700 15px/1.25 system-ui,sans-serif;padding:9px 16px;border-radius:999px;box-shadow:0 2px 10px rgba(0,0,0,.15);pointer-events:none;transition:opacity .6s;white-space:nowrap;max-width:calc(100% - 24px);overflow:hidden;text-overflow:ellipsis}
#hint.hide{opacity:0}
#badge{position:absolute;left:12px;top:58px;display:none;background:#e4574b;color:#fff;font:800 12px/1 system-ui,sans-serif;letter-spacing:.12em;padding:7px 10px;border-radius:8px;pointer-events:none}
body.demo #badge{display:block}
#controls{flex:none;display:flex;flex-wrap:wrap;align-items:center;justify-content:center;gap:8px 16px;padding:10px max(14px,env(safe-area-inset-right)) calc(10px + env(safe-area-inset-bottom)) max(14px,env(safe-area-inset-left));background:linear-gradient(#24435c,#1a3346);color:#fff;border-top:3px solid #f2c14e;z-index:5}
.speed{display:flex;align-items:center;gap:10px;flex:1 1 260px;max-width:560px;min-width:0}
.speed .lbl{font-weight:800;font-size:15px;letter-spacing:.02em}
.speed output{min-width:68px;text-align:right;font-variant-numeric:tabular-nums;font-weight:800;font-size:15px;color:#ffd479}
input[type=range]{flex:1 1 auto;min-width:0;height:44px;-webkit-appearance:none;appearance:none;background:transparent;cursor:pointer;margin:0}
input[type=range]::-webkit-slider-runnable-track{height:10px;border-radius:6px;background:linear-gradient(90deg,#7fd3f0,#ffb238)}
input[type=range]::-webkit-slider-thumb{-webkit-appearance:none;width:30px;height:30px;border-radius:50%;background:#fff;border:4px solid #e4574b;margin-top:-10px;box-shadow:0 2px 5px rgba(0,0,0,.35)}
input[type=range]::-moz-range-track{height:10px;border-radius:6px;background:linear-gradient(90deg,#7fd3f0,#ffb238)}
input[type=range]::-moz-range-thumb{width:22px;height:22px;border-radius:50%;background:#fff;border:4px solid #e4574b;box-shadow:0 2px 5px rgba(0,0,0,.35)}
input[type=range]:focus-visible{outline:2px solid #ffd479;outline-offset:2px;border-radius:8px}
.btns{display:flex;gap:8px}
.btns button{min-width:48px;height:48px;padding:0 14px;border:0;border-radius:24px;background:#33617f;color:#fff;font:800 15px/1 system-ui,sans-serif;display:flex;align-items:center;justify-content:center;gap:7px;cursor:pointer;touch-action:manipulation;transition:transform .1s,background .2s;box-shadow:0 2px 0 rgba(0,0,0,.25)}
.btns button svg{width:22px;height:22px;flex:none}
.btns button:active,.btns button.pressed{transform:scale(.92);background:#4a86ab}
.btns button:focus-visible{outline:3px solid #ffd479;outline-offset:2px}
#bellBtn{background:#e4574b}
#bellBtn:active,#bellBtn.pressed{background:#f07a6f}
@media (max-width:360px){.btns button span{display:none}}
#ptr{position:fixed;left:0;top:0;width:0;height:0;z-index:40;pointer-events:none;display:none}
#ptr svg{position:absolute;left:-4px;top:-3px;width:30px;height:30px;filter:drop-shadow(0 2px 3px rgba(0,0,0,.4))}
#ptr .ring{position:absolute;left:-20px;top:-20px;width:40px;height:40px;border-radius:50%;border:3px solid #ffb238;background:rgba(255,178,56,.25);opacity:0;transform:scale(.4);transition:opacity .15s,transform .15s}
#ptr.down .ring{opacity:1;transform:scale(.8)}
#ptr.tap .ring{animation:tapring .5s ease-out}
@keyframes tapring{0%{opacity:1;transform:scale(.4)}100%{opacity:0;transform:scale(1.5)}}
.spark{animation:tw 2.6s ease-in-out infinite}
@keyframes tw{0%,100%{opacity:0}50%{opacity:.95}}
</style>
</head>
<body>
<div id="app">
<div id="stage" role="img" aria-label="A pelican riding a bicycle along a seaside promenade. Tap to make it flap its wings.">
<svg id="scene" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice"></svg>
<div id="hint">Tap anywhere to flap!</div>
<div id="badge">DEMO</div>
</div>
<div id="controls">
<div class="speed">
<label for="speed" class="lbl">Speed</label>
<input id="speed" type="range" min="0" max="100" step="1" value="40">
<output id="speedOut" for="speed">10 km/h</output>
</div>
<div class="btns">
<button id="bellBtn" type="button" aria-label="Ring the bell" title="Ring the bell (B)"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3a6 6 0 0 0-6 6v4.2L4 16.5h16l-2-3.3V9a6 6 0 0 0-6-6z" fill="currentColor"/><circle cx="12" cy="19.2" r="2.2" fill="currentColor"/></svg><span>Ring</span></button>
<button id="muteBtn" type="button" aria-label="Mute sound" aria-pressed="false" title="Sound on/off (M)"></button>
<button id="restartBtn" type="button" aria-label="Restart" title="Restart (R)"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M5.2 12a6.8 6.8 0 1 0 2-4.8" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round"/><path d="M6 3.5v4.6h4.6" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round"/></svg><span>Restart</span></button>
</div>
</div>
</div>
<div id="ptr" aria-hidden="true"><div class="ring"></div><svg viewBox="0 0 24 24"><path d="M3 2 L3 19 L7.5 14.8 L10.6 21.5 L13.4 20.2 L10.4 13.7 L16.6 13.4 Z" fill="#fff" stroke="#1b2a38" stroke-width="1.6" stroke-linejoin="round"/></svg></div>
<script>
(function(){
'use strict';
const NS='http://www.w3.org/2000/svg';
const $=id=>document.getElementById(id);
const svg=$('scene'), stage=$('stage'), slider=$('speed'), speedOut=$('speedOut');
const bellBtn=$('bellBtn'), muteBtn=$('muteBtn'), restartBtn=$('restartBtn'), hint=$('hint'), ptr=$('ptr');
function mk(tag,attrs,parent){const e=document.createElementNS(NS,tag); if(attrs) for(const a in attrs) e.setAttribute(a,attrs[a]); if(parent) parent.appendChild(e); return e;}
function mulberry32(a){return function(){a|=0;a=(a+0x6D2B79F5)|0;let t=Math.imul(a^(a>>>15),1|a);t=(t+Math.imul(t^(t>>>7),61|t))^t;return((t^(t>>>14))>>>0)/4294967296;};}
const mod=(a,n)=>((a%n)+n)%n;
const lerp=(a,b,u)=>a+(b-a)*u;
const lp=(a,b,u)=>({x:a.x+(b.x-a.x)*u,y:a.y+(b.y-a.y)*u});
const clamp=(v,a,b)=>v<a?a:(v>b?b:v);
const F=v=>Math.round(v*10)/10;
const F2=v=>Math.round(v*100)/100;
const pt=p=>F(p.x)+','+F(p.y);
const DEG=180/Math.PI, TAU=Math.PI*2;
/* ---------- Bicycle + pelican geometry (bike-local units, ground at y=0) ---------- */
const R=38, RA={x:-62,y:-38}, FA={x:62,y:-38}, BB={x:-6,y:-34};
const CR=11, SR=5, CRp=CR+0.6, SRp=SR+0.6, CRANK=15;
const STT={x:-20,y:-86}, HTT={x:46,y:-84}, HTB={x:50,y:-68};
const GRIP={x:37,y:-100.5}, GRIP2={x:41,y:-101.3};
const HIP={x:-10,y:-98}, HIP2={x:-6,y:-99};
const L1=39, L2=38;
const SH={x:2,y:-124}, BK={x:-36,y:-114}, EREST={x:24,y:-118}, EUP={x:-6,y:-162}, TUP={x:-40,y:-196};
const FOOT='M-2,-2 L2,-2 L5,3 L15,1.5 Q12.5,3.6 16,5 Q12,5.6 14,7.6 L3,6.8 Q-3.5,6 -2,-2 Z';
const FEATHER='M0,0 Q4.5,-2.6 10,0 Q4.5,2.6 0,0 Z M0,0 L11,0';
const FLAP_DUR=0.8;
/* ---------- State ---------- */
const S={dist:0,speed:0,target:0,t:0,flapT:-1,flapQ:0,bellT:99,pv:0,pp:0,halfIdx:0,tipW:{x:0,y:-150}};
const prng=mulberry32(4242);
/* ---------- Layout ---------- */
let vbW=800,vbH=600,k=1,W=400,H=300,groundY=260,railBase=230,horizonY=150,seaH=80,bikeX=200;
let world=null,bike=null,items=[],repeats=[],particles=[],partG=null,sunRays=null,qGroups={};
let builtW=0,builtH=0;
function layout(){
const r=stage.getBoundingClientRect();
const w=Math.max(50,r.width), h=Math.max(50,r.height), a=w/h;
builtW=r.width; builtH=r.height;
vbH=600; vbW=vbH*a; if(vbW<520){vbW=520; vbH=vbW/a;}
k=Math.min(vbW/280, vbH/320);
W=vbW/k; H=vbH/k;
groundY=H-34; railBase=groundY-30; seaH=clamp(railBase*0.35,50,110); horizonY=railBase-seaH;
bikeX=W*0.46;
svg.setAttribute('viewBox','0 0 '+F2(vbW)+' '+F2(vbH));
}
function addItem(el,x0,y,P,pad,f,drift,upd){items.push({el,x0,y,P,pad,f,drift,upd});}
/* ---------- Scene build ---------- */
function build(){
layout();
while(svg.firstChild) svg.removeChild(svg.firstChild);
items=[]; repeats=[]; particles=[]; qGroups={};
const rng=mulberry32(1337);
const defs=mk('defs',null,svg);
const grad=(id,stops)=>{const g=mk('linearGradient',{id,x1:0,y1:0,x2:0,y2:1},defs); stops.forEach(s=>mk('stop',{offset:s[0],'stop-color':s[1]},g));};
grad('gSky',[[0,'#58b7e6'],[0.7,'#b9e5f7'],[1,'#f4f1dc']]);
grad('gSea',[[0,'#86cfe4'],[0.18,'#4aa7cf'],[1,'#22709d']]);
grad('gPave',[[0,'#e0d2b6'],[1,'#efe5d0']]);
world=mk('g',{transform:'scale('+F2(k*1000)/1000+')'},svg);
// Sky
mk('rect',{x:-2,y:-2,width:F(W+4),height:F(horizonY+4),fill:'url(#gSky)'},world);
// Sun
const sunX=W*0.8, sunY=clamp(horizonY*0.32,26,80);
const sunG=mk('g',{transform:'translate('+F(sunX)+','+F(sunY)+')'},world);
mk('circle',{r:36,fill:'#fff8cf',opacity:0.4},sunG);
sunRays=mk('g',null,sunG);
for(let i=0;i<12;i++){const a=i/12*TAU; mk('line',{x1:F(Math.cos(a)*25),y1:F(Math.sin(a)*25),x2:F(Math.cos(a)*31),y2:F(Math.sin(a)*31),stroke:'#ffe27a','stroke-width':2.4,'stroke-linecap':'round'},sunRays);}
mk('circle',{r:20,fill:'#ffe680',stroke:'#ffd34d','stroke-width':2},sunG);
// Far island with lighthouse (layer 1: slowest)
const islL=mk('g',{opacity:0.92},world); qGroups.island=islL;
{
const isl=mk('g',null,islL);
mk('path',{d:'M-95,0 Q-70,-16 -40,-13 Q-15,-24 15,-12 Q45,-9 60,-4 Q80,-2 95,0 Z',fill:'#8fb8c8'},isl);
mk('path',{d:'M-34,-15 L-26,-15 L-27.6,-40 L-32.4,-40 Z',fill:'#f7f7f2'},isl);
mk('path',{d:'M-33.6,-21 L-26.4,-21 L-26.8,-26 L-33.2,-26 Z M-32.9,-31 L-27.1,-31 L-27.4,-35 L-32.6,-35 Z',fill:'#e4574b'},isl);
mk('rect',{x:-32.5,y:-44.5,width:5,height:4.5,fill:'#ffe680',stroke:'#7b8a94','stroke-width':0.6},isl);
mk('path',{d:'M-33.5,-44.5 L-30,-48.5 L-26.5,-44.5 Z',fill:'#c0392b'},isl);
addItem(isl,W*0.28,horizonY+0.5,W+260,130,0.02,0);
}
// Clouds (layer 2)
const cloudL=mk('g',null,world);
const nC=Math.max(4,Math.ceil(W/150)+1), Pc=W+260;
for(let i=0;i<nC;i++){
const s=0.55+rng()*0.75, y=14+rng()*Math.max(20,horizonY*0.55);
const g=mk('g',null,cloudL); const c=mk('g',{transform:'scale('+F2(s)+')'},g);
mk('ellipse',{cx:0,cy:3,rx:50,ry:8,fill:'#dcedf6'},c);
const n=4+Math.floor(rng()*2);
for(let j=0;j<n;j++){
const mid=(j>0&&j<n-1)?6:0;
mk('circle',{cx:F(-38+j*(76/(n-1))+(rng()-0.5)*8),cy:F(-4-rng()*10-mid),r:F(11+rng()*9),fill:'#fff'},c);
}
mk('rect',{x:-44,y:-6,width:88,height:9,rx:4.5,fill:'#fff'},c);
addItem(g,(i+rng()*0.6)*Pc/nC,y,Pc,130,0.03+0.05*s,3);
}
// Gulls
const gullL=mk('g',null,world); qGroups.gulls=gullL;
for(let i=0;i<3;i++){
const g=mk('g',null,gullL);
mk('path',{d:'M-8,0 Q-4,-5 0,0 Q4,-5 8,0',fill:'none',stroke:'#3f4b5a','stroke-width':1.5,'stroke-linecap':'round','stroke-linejoin':'round'},g);
const ph=rng()*TAU, y=20+rng()*Math.max(10,horizonY*0.45), sc=0.8+i*0.15;
addItem(g,(i+rng()*0.5)*(W+80)/3,y,W+80,40,0.1,14,(it,x)=>{
const fl=0.35+0.65*Math.abs(Math.sin(S.t*5+ph));
it.el.setAttribute('transform','translate('+F2(x)+','+F2(it.y+Math.sin(S.t*0.8+ph)*4)+') scale('+sc+','+F2(fl*sc)+')');
});
}
// Sea
mk('rect',{x:-2,y:F(horizonY),width:F(W+4),height:F(railBase-horizonY+4),fill:'url(#gSea)'},world);
mk('rect',{x:-2,y:F(horizonY-0.8),width:F(W+4),height:1.6,fill:'#fff',opacity:0.55},world);
// Boats (layer 3, depth-based parallax)
const boatL=mk('g',null,world);
const nB=Math.max(3,Math.round(W/170)), Pb=W+120;
const bl=[];
for(let i=0;i<nB;i++) bl.push({d:rng(),x0:(i+rng()*0.5)*Pb/nB,type:rng(),ph:rng()*TAU,dr:1+rng()*4,c1:rng(),c2:rng()});
bl.sort((a,b)=>a.d-b.d);
bl.forEach(b=>{
const y=horizonY+5+b.d*seaH*0.5, s=0.38+b.d*0.7, f=0.08+b.d*0.22;
const g=mk('g',null,boatL);
drawBoat(g,b);
addItem(g,b.x0,y,Pb,60,f,b.dr,(it,x)=>{
it.el.setAttribute('transform','translate('+F2(x)+','+F2(it.y+Math.sin(S.t*1.5+b.ph)*0.7)+') scale('+F2(s)+') rotate('+F2(Math.sin(S.t*1.2+b.ph)*3)+')');
});
});
// Waves (layer 4: three rows at different speeds)
const waveDefs=[[0.18,26,1.0,0.1,0.35],[0.48,34,1.6,0.18,0.45],[0.8,44,2.3,0.3,0.55]];
waveDefs.forEach((wd,ri)=>{
const fy=wd[0], sp=wd[1], amp=wd[2], f=wd[3], op=wd[4];
const y=horizonY+fy*seaH, P=sp*5;
const jit=[]; for(let j=0;j<5;j++) jit.push({dx:rng()*sp*0.5,w:sp*(0.25+rng()*0.3),dy:(rng()-0.5)*seaH*0.07});
let d=''; const cnt=Math.ceil((W+3*P)/sp);
for(let n=0;n<cnt;n++){const j=jit[n%5]; const x=-P+n*sp+j.dx, yy=y+j.dy; d+='M'+F(x)+','+F(yy)+'q'+F(j.w/2)+','+(-amp)+' '+F(j.w)+',0';}
const el=mk('path',{d,fill:'none',stroke:'#fff','stroke-opacity':op,'stroke-width':1.3,'stroke-linecap':'round'},world);
if(ri===0) qGroups.waveFar=el;
repeats.push({el,period:P,f,drift:3+ri*2});
});
// Sparkles
const spG=mk('g',null,world); qGroups.sparks=spG;
const sps=[]; for(let i=0;i<5;i++) sps.push({x:rng()*140,y:horizonY+6+rng()*(seaH-14),w:3+rng()*5,dl:F(rng()*2.6)});
for(let base=-140;base<W+280;base+=140) sps.forEach(s=>mk('rect',{x:F(base+s.x),y:F(s.y),width:F(s.w),height:1.2,rx:0.6,fill:'#fff',class:'spark',style:'animation-delay:-'+s.dl+'s'},spG));
repeats.push({el:spG,period:140,f:0.16,drift:1});
// Sea wall coping
mk('rect',{x:-2,y:F(railBase-2),width:F(W+4),height:5,fill:'#d6c8ad'},world);
// Railing, bunting and lampposts (layer 5)
const rl=mk('g',null,world);
const top=railBase-17, x0=-150, x1=W+300;
let rd=''; for(let x=x0;x<=x1;x+=15) rd+='M'+x+','+F(railBase-1)+'V'+F(top);
mk('path',{d:rd,stroke:'#f5f8fa','stroke-width':1.8},rl);
mk('path',{d:'M'+x0+','+F(top+1.4)+'H'+F(x1)+'M'+x0+','+F(railBase-7.6)+'H'+F(x1),stroke:'#8fa4b2','stroke-width':1},rl);
mk('path',{d:'M'+x0+','+F(top)+'H'+F(x1)+'M'+x0+','+F(railBase-9)+'H'+F(x1),stroke:'#fbfdfe','stroke-width':2.4},rl);
const bunt=mk('g',null,rl); qGroups.bunting=bunt;
const flagC=['#e4574b','#f2c14e','#3fa7d6','#7bc96f','#f28ab2'];
const lampTop=railBase-100;
for(let lx=-75;lx<=x1;lx+=150){
const a={x:lx+1,y:lampTop+6}, b={x:lx+149,y:lampTop+6}, c={x:lx+75,y:lampTop+34};
mk('path',{d:'M'+pt(a)+' Q'+pt(c)+' '+pt(b),fill:'none',stroke:'#ffffff','stroke-width':0.7,opacity:0.9},bunt);
for(let j=1;j<12;j++){
const u=j/12, px=(1-u)*(1-u)*a.x+2*(1-u)*u*c.x+u*u*b.x, py=(1-u)*(1-u)*a.y+2*(1-u)*u*c.y+u*u*b.y;
mk('path',{d:'M'+F(px-3)+','+F(py)+'L'+F(px+3)+','+F(py)+'L'+F(px)+','+F(py+7)+'Z',fill:flagC[j%5]},bunt);
}
mk('path',{d:'M'+(lx-4)+','+F(railBase+1)+'h8l-1.5,-9h-5z',fill:'#24506b'},rl);
mk('rect',{x:lx-1.6,y:F(lampTop),width:3.2,height:F(railBase-8-lampTop),fill:'#24506b'},rl);
mk('circle',{cx:lx,cy:F(lampTop-1),r:2.6,fill:'#24506b'},rl);
mk('path',{d:'M'+lx+','+F(lampTop+4)+'q0,-12 10,-12q7,0 7,7',fill:'none',stroke:'#24506b','stroke-width':2.2},rl);
mk('path',{d:'M'+(lx+12)+','+F(lampTop-1)+'h10l-2,10h-6z',fill:'#fff1b0',stroke:'#24506b','stroke-width':1.2},rl);
mk('path',{d:'M'+(lx+11)+','+F(lampTop-1)+'h12l-6,-4.5z',fill:'#24506b'},rl);
}
repeats.push({el:rl,period:150,f:0.85,drift:0});
// Promenade pavement (layer 6: same speed as the wheels' contact point)
mk('rect',{x:-2,y:F(railBase+3),width:F(W+4),height:F(groundY+10-railBase-3),fill:'url(#gPave)'},world);
mk('path',{d:'M-2,'+F(railBase+11)+'H'+F(W+2)+'M-2,'+F(groundY-24)+'H'+F(W+2),stroke:'#d3c3a3','stroke-width':0.8},world);
mk('rect',{x:-2,y:F(groundY+10),width:F(W+4),height:6,fill:'#f4ede0'},world);
mk('rect',{x:-2,y:F(groundY+15),width:F(W+4),height:1.2,fill:'#cbbd9f'},world);
mk('rect',{x:-2,y:F(groundY+16),width:F(W+4),height:F(H-groundY-14),fill:'#5e9a45'},world);
mk('rect',{x:-2,y:F(groundY+16),width:F(W+4),height:2.5,fill:'#4a7d36'},world);
const pv=mk('g',null,world);
let jd=''; for(let x=-48;x<=W+96;x+=24) jd+='M'+x+','+F(railBase+3)+'L'+(x-9)+','+F(groundY+10);
mk('path',{d:jd,stroke:'#cdbb98','stroke-width':0.8},pv);
mk('path',{d:'M-48,'+F(groundY-13)+'H'+F(W+96),stroke:'#ffffff','stroke-width':2,'stroke-dasharray':'14 10',opacity:0.85},pv);
let kd=''; for(let x=-48;x<=W+96;x+=48) kd+='M'+x+','+F(groundY+10)+'V'+F(groundY+15);
mk('path',{d:kd,stroke:'#cbbd9f','stroke-width':0.9},pv);
repeats.push({el:pv,period:48,f:1,drift:0});
// Bike + pelican
bike=buildBike(mk('g',{transform:'translate('+F2(bikeX)+','+F2(groundY)+')'},world));
// Foreground flowers (layer 7: faster than the ground)
const flL=mk('g',null,world);
const fc=['#ff6f91','#ffd23f','#ffffff','#b28dff','#ff8c42'];
const nF=Math.ceil(W/22)+3, Pf=W+40;
for(let i=0;i<nF;i++){
const g=mk('g',null,flL);
const y=groundY+20+rng()*Math.max(2,H-groundY-25);
const inner=mk('g',{transform:'scale('+F2(0.8+rng()*0.5)+')'},g);
mk('path',{d:'M-4,0 Q-5,-6 -8,-9 M0,0 Q0,-7 1,-12 M3,0 Q5,-5 8,-8',fill:'none',stroke:'#3d7a2b','stroke-width':1.5,'stroke-linecap':'round'},inner);
if(rng()<0.6){
const c=fc[Math.floor(rng()*fc.length)], fx=F((rng()-0.5)*4);
mk('path',{d:'M'+fx+',0 L'+fx+',-13',stroke:'#3d7a2b','stroke-width':1.1},inner);
for(let j=0;j<5;j++){const a=j/5*TAU; mk('circle',{cx:F(fx+Math.cos(a)*2.4),cy:F(-14+Math.sin(a)*2.4),r:1.9,fill:c},inner);}
mk('circle',{cx:fx,cy:-14,r:1.4,fill:'#ffcf3a'},inner);
}
addItem(g,(i+rng()*0.7)*Pf/nF,y,Pf,20,1.25,0);
}
partG=mk('g',null,world);
}
function drawBoat(g,b){
const hulls=['#e4574b','#2f6f9f','#f2b134','#3c8f6d','#ffffff'], sails=['#ffffff','#fff1d0','#ffd9cc','#e2f3ff'];
const hc=hulls[Math.floor(b.c1*hulls.length)], sc=sails[Math.floor(b.c2*sails.length)];
if(b.type<0.65){
mk('line',{x1:0,y1:-3,x2:0,y2:-44,stroke:'#5b4636','stroke-width':1.6},g);
mk('path',{d:'M1.5,-42 L1.5,-6 L24,-6 Z',fill:sc,stroke:'#9aa6b2','stroke-width':0.8},g);
mk('path',{d:'M-1.5,-36 L-1.5,-6 L-17,-6 Z',fill:sc,stroke:'#9aa6b2','stroke-width':0.8},g);
mk('path',{d:'M1.5,-20 L13,-17.5 L15,-14 L1.5,-16 Z',fill:'#e4574b',opacity:0.75},g);
mk('path',{d:'M0,-44 L7,-42 L0,-40 Z',fill:'#e4574b'},g);
} else {
mk('rect',{x:-8,y:-14,width:12,height:10,fill:'#f4f4f4',stroke:'#5b6672','stroke-width':0.8},g);
mk('rect',{x:-6,y:-12,width:3.5,height:3.5,fill:'#8fd0ee'},g);
mk('rect',{x:-1,y:-12,width:3.5,height:3.5,fill:'#8fd0ee'},g);
mk('line',{x1:8,y1:-4,x2:8,y2:-26,stroke:'#5b4636','stroke-width':1.4},g);
mk('path',{d:'M8,-26 L18,-6 M8,-22 L-12,-5',stroke:'#5b4636','stroke-width':0.6,fill:'none'},g);
mk('rect',{x:-10,y:-16,width:16,height:2.4,fill:hc==='#ffffff'?'#e4574b':hc},g);
}
mk('path',{d:'M-24,-5 L24,-5 Q21,4 14,5 L-16,5 Q-22,3 -24,-5 Z',fill:hc,stroke:'#3e4a59','stroke-width':1},g);
mk('path',{d:'M-23,-2 L23,-2',stroke:hc==='#ffffff'?'#3fa7d6':'#ffffff','stroke-width':1.2,opacity:0.85},g);
mk('path',{d:'M-20,5 Q0,8 18,5',stroke:'#ffffff',fill:'none','stroke-width':1,opacity:0.6},g);
}
/* ---------- Bicycle & pelican ---------- */
function chainPath(){
const c1=BB, c2=RA, r1=CRp, r2=SRp;
const dx=c2.x-c1.x, dy=c2.y-c1.y, d=Math.hypot(dx,dy), th=Math.atan2(dy,dx), ph=Math.acos((r1-r2)/d);
const p=(c,r,a)=>({x:c.x+r*Math.cos(a),y:c.y+r*Math.sin(a)});
const p1a=p(c1,r1,th+ph), p2a=p(c2,r2,th+ph), p2b=p(c2,r2,th-ph), p1b=p(c1,r1,th-ph);
return 'M'+pt(p1a)+' L'+pt(p2a)+' A'+F(r2)+','+F(r2)+' 0 0 0 '+pt(p2b)+' L'+pt(p1b)+' A'+F(r1)+','+F(r1)+' 0 1 0 '+pt(p1a)+' Z';
}
function buildWheel(g){
const w=mk('g',null,g);
mk('circle',{r:R-2.2,fill:'none',stroke:'#2a2d33','stroke-width':4.4},w);
mk('circle',{r:R-2.2,fill:'none',stroke:'#4c525b','stroke-width':1,'stroke-dasharray':'2 3'},w);
mk('circle',{r:R-5,fill:'none',stroke:'#cfd6dd','stroke-width':1.8},w);
let d='';
for(let i=0;i<18;i++){const a=i/18*TAU, off=(i%2?1:-1)*0.25; d+='M'+F(Math.cos(a+off)*2.6)+','+F(Math.sin(a+off)*2.6)+'L'+F(Math.cos(a)*(R-5.6))+','+F(Math.sin(a)*(R-5.6));}
mk('path',{d,stroke:'#9aa4ae','stroke-width':0.7},w);
mk('circle',{r:3.6,fill:'#cfd6dd',stroke:'#59606b','stroke-width':1},w);
mk('rect',{x:-1.2,y:-(R-4.5),width:2.4,height:4,rx:0.6,fill:'#e4574b'},w);
mk('rect',{x:13,y:-2,width:6,height:4,rx:1,fill:'#ffb238',stroke:'#c47f12','stroke-width':0.6},w);
return w;
}
function buildLeg(g,far){
const lg=mk('g',{'stroke-linecap':'round','stroke-linejoin':'round',fill:'none'},g);
const lo=mk('path',{stroke:'#8e5214','stroke-width':6},lg);
const lc=mk('path',{stroke:far?'#c27720':'#f39b2f','stroke-width':4.2},lg);
const fo=mk('path',{stroke:'#3e4a59','stroke-width':11},lg);
const fw=mk('path',{stroke:far?'#d9d3c6':'#f6f3ea','stroke-width':8.6},lg);
const ft=mk('path',{d:FOOT,fill:far?'#c27720':'#f6a43c',stroke:'#8e5214','stroke-width':0.8},lg);
return {lo,lc,fo,fw,ft};
}
function buildBike(g){
const B={};
mk('ellipse',{cx:RA.x,cy:1.6,rx:30,ry:3.2,fill:'#203040',opacity:0.18},g);
mk('ellipse',{cx:FA.x,cy:1.6,rx:30,ry:3.2,fill:'#203040',opacity:0.18},g);
mk('ellipse',{cx:0,cy:2,rx:44,ry:3,fill:'#203040',opacity:0.1},g);
// far side (behind the wheels)
B.farCrank=mk('line',{x1:BB.x,y1:BB.y,x2:BB.x,y2:BB.y,stroke:'#5d6570','stroke-width':3,'stroke-linecap':'round'},g);
B.farPedal=mk('rect',{x:-5.5,y:-1.5,width:11,height:3,rx:1,fill:'#2c2f36'},g);
B.farLeg=buildLeg(g,true);
// wheels
B.rear=buildWheel(g); B.front=buildWheel(g);
// diamond frame
const fr=mk('g',{'stroke-linecap':'round','stroke-linejoin':'round',fill:'none'},g);
const L=(a,b,c,w)=>mk('line',{x1:a.x,y1:a.y,x2:b.x,y2:b.y,stroke:c,'stroke-width':w},fr);
const FC='#e4574b', FD='#b8392f', MET='#aab3bd';
L(BB,RA,FC,3.2);
L({x:-19.5,y:-80},RA,FC,3);
mk('path',{d:'M50,-68 Q55,-52 62,-38',stroke:FC,'stroke-width':3.6},fr);
L(BB,HTB,FC,5);
L(BB,STT,FC,4.6);
L({x:-19.5,y:-82},{x:46.5,y:-80.5},FC,4.2);
L(HTT,HTB,FD,6.5);
L(STT,{x:-22.5,y:-92},MET,3);
// fish + basket
B.fish=mk('g',null,fr);
mk('path',{d:'M60,-92 Q58,-100 60,-104 L56,-110 L61,-107.5 L65,-110.5 L63,-104 Q66,-99 65,-92 Z',fill:'#86b6cc',stroke:'#3f6a80','stroke-width':0.9},B.fish);
L({x:46,y:-87},{x:52,y:-89},MET,1.6);
mk('path',{d:'M52,-80 L71,-80 L74,-95 L50,-95 Z',fill:'#c98d4f',stroke:'#7e5428','stroke-width':1.1},fr);
mk('path',{d:'M51.3,-90 H72.8 M51.8,-85 H71.8 M57,-95 V-80 M63,-95 V-80 M69,-95 V-80',stroke:'#9b6935','stroke-width':0.8},fr);
mk('path',{d:'M49.5,-95.5 H74.5',stroke:'#7e5428','stroke-width':2},fr);
// stem, handlebar, grip
L(HTT,{x:47.5,y:-96},MET,3);
mk('path',{d:'M47.5,-96 C55,-96.5 55,-102 46,-101.5 L37,-100.5',stroke:'#3a3f47','stroke-width':2.8},fr);
mk('line',{x1:41,y1:-101,x2:34.5,y2:-100.3,stroke:'#1f2329','stroke-width':4.6,'stroke-linecap':'round'},fr);
// rear sprocket
B.spr=mk('g',null,g);
mk('circle',{r:SR,fill:'#6b737e',stroke:'#3d434b','stroke-width':1.6,'stroke-dasharray':'1.3 1.32'},B.spr);
mk('circle',{r:1.6,fill:'#c9d0d7'},B.spr);
mk('line',{x1:0,y1:0,x2:SR-1.2,y2:0,stroke:'#c9d0d7','stroke-width':1},B.spr);
// chainring
B.ring=mk('g',null,g);
mk('circle',{r:CR,fill:'none',stroke:'#4b525c','stroke-width':2.4,'stroke-dasharray':'1.5 1.38'},B.ring);
mk('circle',{r:CR-1.5,fill:'none',stroke:'#aeb7c0','stroke-width':1.5},B.ring);
let sd=''; for(let i=0;i<5;i++){const a=i/5*TAU; sd+='M0,0L'+F(Math.cos(a)*(CR-1.5))+','+F(Math.sin(a)*(CR-1.5));}
mk('path',{d:sd,stroke:'#aeb7c0','stroke-width':1.8,'stroke-linecap':'round'},B.ring);
// chain
const cd=chainPath();
mk('path',{d:cd,fill:'none',stroke:'#3a3f47','stroke-width':2},g);
B.chain=mk('path',{d:cd,fill:'none',stroke:'#c3cad2','stroke-width':1.1,'stroke-dasharray':'1.6 1.4'},g);
// near crank + pedal
B.nearCrank=mk('line',{x1:BB.x,y1:BB.y,x2:BB.x,y2:BB.y,stroke:'#8e97a1','stroke-width':3.2,'stroke-linecap':'round'},g);
mk('circle',{cx:BB.x,cy:BB.y,r:2.2,fill:'#c9d0d7',stroke:'#59606b','stroke-width':0.8},g);
B.nearPedal=mk('rect',{x:-5.5,y:-1.5,width:11,height:3,rx:1,fill:'#2c2f36'},g);
// saddle
mk('path',{d:'M-38,-95.5 Q-26,-100 -11,-95 Q-12,-91 -22,-91.5 Q-34,-91 -38,-95.5 Z',fill:'#5a3b2a',stroke:'#2e1e15','stroke-width':1},g);
// far wing
B.farWing=mk('path',{fill:'#d8d3c7',stroke:'#3e4a59','stroke-width':1,'stroke-linejoin':'round'},g);
B.farPrim=mk('path',{fill:'#2a2f38'},g);
// near leg
B.nearLeg=buildLeg(g,false);
// body
B.body=mk('g',null,g);
const bd=B.body;
mk('path',{d:'M-38,-113 L-57,-121 L-55,-112 L-58,-106 L-40,-104 Z',fill:'#ece7dc',stroke:'#3e4a59','stroke-width':1.2,'stroke-linejoin':'round'},bd);
const neckD='M2,-121 C22,-131 2,-149 14,-163';
mk('path',{d:neckD,fill:'none',stroke:'#3e4a59','stroke-width':15,'stroke-linecap':'round'},bd);
mk('ellipse',{cx:-12,cy:-113,rx:31,ry:20,transform:'rotate(-14 -12 -113)',fill:'#fbfaf6',stroke:'#3e4a59','stroke-width':1.4},bd);
mk('ellipse',{cx:-10,cy:-103,rx:22,ry:7,transform:'rotate(-10 -10 -103)',fill:'#efeadf'},bd);
mk('path',{d:neckD,fill:'none',stroke:'#fbfaf6','stroke-width':12.2,'stroke-linecap':'round'},bd);
// head
B.head=mk('g',null,bd);
const hd=B.head;
mk('circle',{cx:18,cy:-168,r:11.5,fill:'#3e4a59'},hd);
B.pouch=mk('path',{fill:'#f4a07a',stroke:'#c4643f','stroke-width':1.1,'stroke-linejoin':'round'},hd);
mk('circle',{cx:18,cy:-168,r:10.2,fill:'#fbfaf6'},hd);
mk('path',{d:'M9,-176 Q3,-182 -2,-178 Q4,-177 8,-172 Z',fill:'#f3e3a1',stroke:'#c9b56a','stroke-width':0.7},hd);
mk('path',{d:'M25,-165 C46,-164 70,-161.5 92,-159.8',fill:'none',stroke:'#d9862b','stroke-width':1.6},hd);
mk('path',{d:'M23,-174 C40,-172 70,-167 93,-162.5 Q99,-161.5 98.5,-158 Q97,-155.8 94.5,-158.6 L92,-159.8 C70,-162 44,-164.5 25,-165 Z',fill:'#f6b434',stroke:'#b8761c','stroke-width':1,'stroke-linejoin':'round'},hd);
mk('path',{d:'M27,-170.5 C50,-168 74,-164.5 92,-161.2',fill:'none',stroke:'#de952a','stroke-width':0.9},hd);
mk('ellipse',{cx:23,cy:-170,rx:4.6,ry:3.7,fill:'#f7c8a4'},hd);
mk('circle',{cx:23.5,cy:-170.3,r:2,fill:'#1d232b'},hd);
mk('circle',{cx:24.2,cy:-171,r:0.7,fill:'#fff'},hd);
// near wing
B.nearWing=mk('path',{fill:'#fbfaf6',stroke:'#3e4a59','stroke-width':1.2,'stroke-linejoin':'round'},g);
B.nearPrim=mk('path',{fill:'#2a2f38'},g);
// bell
B.bell=mk('g',null,g);
mk('path',{d:'M40.5,-102 q0,-6 4.5,-6 q4.5,0 4.5,6 z',fill:'#e8c04a',stroke:'#9c7a16','stroke-width':0.9},B.bell);
mk('rect',{x:40,y:-102.6,width:10,height:1.6,rx:0.8,fill:'#c99a1e'},B.bell);
mk('circle',{cx:45,cy:-108.6,r:1.1,fill:'#9c7a16'},B.bell);
// speech bubble
B.bub=mk('g',{opacity:0},g);
const bt=mk('text',{x:52,y:-118,'font-size':11,'font-weight':800,'font-family':'system-ui,sans-serif',fill:'#23405a',stroke:'#ffffff','stroke-width':3,'paint-order':'stroke','stroke-linejoin':'round'},B.bub);
bt.textContent='Ring ring!';
mk('path',{d:'M50,-109 q3,-3 0,-6 M53.5,-107 q5,-5 0,-10',stroke:'#23405a','stroke-width':1.2,fill:'none','stroke-linecap':'round'},B.bub);
return B;
}
function ik(Hp,Fp,l1,l2){
const dx=Fp.x-Hp.x, dy=Fp.y-Hp.y;
let d=Math.hypot(dx,dy);
d=clamp(d,Math.abs(l1-l2)+0.01,l1+l2-0.01);
const a=Math.atan2(dy,dx);
const b=Math.acos(clamp((l1*l1+d*d-l2*l2)/(2*l1*d),-1,1));
const k1={x:Hp.x+l1*Math.cos(a-b),y:Hp.y+l1*Math.sin(a-b)};
const k2={x:Hp.x+l1*Math.cos(a+b),y:Hp.y+l1*Math.sin(a+b)};
return k1.x>k2.x?k1:k2;
}
function setLeg(Lg,hip,p,sn){
const an={x:p.x-7,y:p.y-8.3};
const kn=ik(hip,an,L1,L2);
const legD='M'+pt(hip)+'L'+pt(kn)+'L'+pt(an);
Lg.lo.setAttribute('d',legD); Lg.lc.setAttribute('d',legD);
const mid=lp(hip,kn,0.55);
const fd='M'+pt(hip)+'L'+pt(mid);
Lg.fo.setAttribute('d',fd); Lg.fw.setAttribute('d',fd);
Lg.ft.setAttribute('transform','translate('+F2(an.x)+','+F2(an.y)+') rotate('+F2(-4+9*sn)+')');
}
function wingD(Sp,E,T,Bk,spread){
const n=7, dx=Bk.x-T.x, dy=Bk.y-T.y, Ln=Math.hypot(dx,dy)||1, ux=dx/Ln, uy=dy/Ln;
let nx=-uy, ny=ux;
if(nx*((T.x+Bk.x)/2-E.x)+ny*((T.y+Bk.y)/2-E.y)<0){nx=-nx;ny=-ny;}
const P=i=>({x:T.x+dx*i/n,y:T.y+dy*i/n});
let d='M'+pt(Sp)+' Q'+pt(E)+' '+pt(T), pr='M'+pt(T);
for(let i=0;i<n;i++){
const a=P(i), b=P(i+1), fl=(3.5+14*spread)*(1-i*0.085);
const tip={x:a.x+nx*fl-ux*fl*0.55,y:a.y+ny*fl-uy*fl*0.55};
d+=' L'+pt(tip)+' L'+pt(b);
if(i<3) pr+=' L'+pt(tip)+' L'+pt(b);
}
const inr=lp(P(1.5),E,0.22);
pr+=' L'+pt(inr)+' Z';
d+=' Q'+F((Sp.x+Bk.x)/2)+','+F(Math.min(Sp.y,Bk.y)-5)+' '+pt(Sp)+' Z';
return [d,pr];
}
function setWings(B,bob,lift){
const o=(p,dx,dy)=>({x:p.x+dx,y:p.y+dy+bob});
let Sp=o(SH,5,-3), Bk=o(BK,5,-3), E=lp(o(EREST,5,-3),o(EUP,6,-3),lift), T=lp(GRIP2,o(TUP,10,-2),lift);
let w=wingD(Sp,E,T,Bk,lift);
B.farWing.setAttribute('d',w[0]); B.farPrim.setAttribute('d',w[1]);
Sp=o(SH,0,0); Bk=o(BK,0,0); E=lp(o(EREST,0,0),o(EUP,0,0),lift); T=lp(GRIP,o(TUP,0,0),lift);
w=wingD(Sp,E,T,Bk,lift);
B.nearWing.setAttribute('d',w[0]); B.nearPrim.setAttribute('d',w[1]);
S.tipW=T;
}
function pouchD(s){
return 'M25,-165 C46,-164 70,-161.5 92,-159.8 C78,'+F(-158+s*0.9)+' 48,'+F(-157+s*1.55)+' 28,'+F(-156+s*1.2)+' Q14,-156 12,-160 Q16,-164 25,-165 Z';
}
function flapLift(){
const f=S.flapT; if(f<0) return 0;
return f<0.4 ? Math.sin(f/0.4*Math.PI/2) : Math.cos((f-0.4)/0.6*Math.PI/2);
}
/* ---------- Audio (starts only after a user gesture) ---------- */
let actx=null, master=null, noiseBuf=null, muted=false;
try{ muted=localStorage.getItem('pelicanBikeMuted')==='1'; }catch(e){}
function ensureAudio(){
if(actx){ if(actx.state==='suspended'){ try{actx.resume();}catch(e){} } return; }
const AC=window.AudioContext||window.webkitAudioContext; if(!AC) return;
try{ actx=new AC(); }catch(e){ actx=null; return; }
master=actx.createGain(); master.gain.value=muted?0:0.8; master.connect(actx.destination);
const len=actx.sampleRate*2; noiseBuf=actx.createBuffer(1,len,actx.sampleRate);
const d=noiseBuf.getChannelData(0), r=mulberry32(7);
for(let i=0;i<len;i++) d[i]=r()*2-1;
// gentle surf ambience
try{
const src=actx.createBufferSource(); src.buffer=noiseBuf; src.loop=true;
const lpf=actx.createBiquadFilter(); lpf.type='lowpass'; lpf.frequency.value=480;
const g=actx.createGain(); g.gain.value=0.05;
const lfo=actx.createOscillator(); lfo.frequency.value=0.13;
const lg=actx.createGain(); lg.gain.value=0.035;
lfo.connect(lg); lg.connect(g.gain);
src.connect(lpf); lpf.connect(g); g.connect(master);
src.start(); lfo.start();
}catch(e){}
}
function playBell(){
if(!actx||!master) return;
const now=actx.currentTime;
[0,0.17].forEach((dt,j)=>{
const t0=now+dt, dur=j?1.1:0.45;
[[2850,0.2],[2873,0.14],[4310,0.08],[6020,0.045]].forEach(pa=>{
const o=actx.createOscillator(); o.type='sine'; o.frequency.value=pa[0];
const g=actx.createGain();
g.gain.setValueAtTime(0.0001,t0);
g.gain.exponentialRampToValueAtTime(pa[1]*(j?0.9:1),t0+0.004);
g.gain.exponentialRampToValueAtTime(0.0001,t0+dur);
o.connect(g); g.connect(master); o.start(t0); o.stop(t0+dur+0.05);
});
});
}
function playWhoosh(){
if(!actx||!master||!noiseBuf) return;
const now=actx.currentTime;
const src=actx.createBufferSource(); src.buffer=noiseBuf;
const bp=actx.createBiquadFilter(); bp.type='bandpass'; bp.Q.value=1.4;
bp.frequency.setValueAtTime(350,now); bp.frequency.exponentialRampToValueAtTime(1500,now+0.18); bp.frequency.exponentialRampToValueAtTime(300,now+0.55);
const g=actx.createGain();
g.gain.setValueAtTime(0.0001,now); g.gain.exponentialRampToValueAtTime(0.4,now+0.1); g.gain.exponentialRampToValueAtTime(0.0001,now+0.6);
src.connect(bp); bp.connect(g); g.connect(master); src.start(now); src.stop(now+0.65);
}
const ICON_ON='<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 9h4l5-4v14l-5-4H4z" fill="currentColor"/><path d="M16 8.5a5 5 0 0 1 0 7M18.6 6a8.6 8.6 0 0 1 0 12" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>';
const ICON_OFF='<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 9h4l5-4v14l-5-4H4z" fill="currentColor"/><path d="M16.5 9.5l5 5M21.5 9.5l-5 5" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round"/></svg>';
function updMute(){
muteBtn.innerHTML=(muted?ICON_OFF:ICON_ON)+'<span>'+(muted?'Muted':'Sound')+'</span>';
muteBtn.setAttribute('aria-pressed',muted?'true':'false');
muteBtn.setAttribute('aria-label',muted?'Unmute sound':'Mute sound');
}
function toggleMute(){
muted=!muted;
try{ localStorage.setItem('pelicanBikeMuted',muted?'1':'0'); }catch(e){}
if(master&&actx){ try{ master.gain.setTargetAtTime(muted?0:0.8,actx.currentTime,0.02); }catch(e){ master.gain.value=muted?0:0.8; } }
updMute();
}
/* ---------- Actions ---------- */
function startFlap(){ S.flapT=0; S.pv+=38; playWhoosh(); }
function flap(){
if(S.flapT<0) startFlap();
else if(S.flapT>0.45&&S.flapQ<1) S.flapQ++;
}
function ring(){ S.bellT=0; S.pv+=16; playBell(); }
function setSpeed(v){
v=clamp(Math.round(v),0,100);
slider.value=v; S.target=v/100*300;
speedOut.textContent=v===0?'Parked':(Math.round(v*0.25)+' km/h');
}
function restart(){
S.dist=0; S.speed=0; S.flapT=-1; S.flapQ=0; S.pv=0; S.pp=0; S.bellT=99; S.halfIdx=0;
particles.forEach(p=>{ if(p.el.parentNode) p.el.parentNode.removeChild(p.el); });
particles=[];
setSpeed(40);
}
function pressVis(btn){ btn.classList.add('pressed'); setTimeout(()=>btn.classList.remove('pressed'),180); }
let hintTimer=null;
function hideHint(delay){
clearTimeout(hintTimer);
hintTimer=setTimeout(()=>hint.classList.add('hide'),delay||0);
}
function spawnFeathers(){
for(let i=0;i<2;i++){
if(particles.length>16) break;
const el=mk('path',{d:FEATHER,fill:'#ffffff',stroke:'#9aa4ae','stroke-width':0.6},partG);
particles.push({el,x:bikeX+S.tipW.x+i*6,y:groundY+S.tipW.y+i*8,vx:-6-prng()*16,vy:-10,ph:prng()*TAU,life:0});
}
}
/* ---------- Simulation ---------- */
function step(dt){
S.t+=dt;
S.speed+=(S.target-S.speed)*(1-Math.exp(-dt*1.8));
if(S.target===0&&S.speed<0.05) S.speed=0;
S.dist+=S.speed*dt;
if(S.flapT>=0){
const prev=S.flapT; S.flapT+=dt/FLAP_DUR;
if(prev<0.4&&S.flapT>=0.4) spawnFeathers();
if(S.flapT>=1){ S.flapT=-1; if(S.flapQ>0){ S.flapQ--; startFlap(); } }
}
S.bellT+=dt;
const ca=(S.dist/R)*SRp/CRp;
const hi=Math.floor(ca/Math.PI);
if(hi!==S.halfIdx){ S.halfIdx=hi; S.pv+=6*Math.min(1,S.speed/150); }
const acc=-60*S.pp-3.5*S.pv; S.pv+=acc*dt; S.pp=clamp(S.pp+S.pv*dt,-6,12);
for(let i=particles.length-1;i>=0;i--){
const p=particles[i]; p.life+=dt;
const sw=Math.sin(S.t*3+p.ph);
p.vy=lerp(p.vy,14,Math.min(1,dt*1.5));
p.x+=(p.vx-S.speed+sw*10)*dt; p.y+=p.vy*dt;
const op=p.life>2.2?Math.max(0,1-(p.life-2.2)/0.8):1;
if(p.life>3||p.y>groundY-2||p.x<-30){ if(p.el.parentNode) p.el.parentNode.removeChild(p.el); particles.splice(i,1); continue; }
p.el.setAttribute('transform','translate('+F2(p.x)+','+F2(p.y)+') rotate('+F2(sw*35+200)+')');
p.el.setAttribute('opacity',F2(op));
}
}
function render(){
for(let i=0;i<items.length;i++){
const it=items[i];
const x=mod(it.x0-(S.dist*it.f-it.drift*S.t),it.P)-it.pad;
if(it.upd) it.upd(it,x); else it.el.setAttribute('transform','translate('+F2(x)+','+F2(it.y)+')');
}
for(let i=0;i<repeats.length;i++){
const r=repeats[i];
r.el.setAttribute('transform','translate('+F2(-mod(S.dist*r.f+r.drift*S.t,r.period))+',0)');
}
if(sunRays) sunRays.setAttribute('transform','rotate('+F2((S.t*8)%360)+')');
const B=bike; if(!B) return;
// everything below is driven by one distance value, so wheels, cranks, chain and legs stay in sync
const wa=S.dist/R, ca=wa*SRp/CRp;
const wd=F2((wa*DEG)%360), cd=F2((ca*DEG)%360);
B.rear.setAttribute('transform','translate('+RA.x+','+RA.y+') rotate('+wd+')');
B.front.setAttribute('transform','translate('+FA.x+','+FA.y+') rotate('+wd+')');
B.spr.setAttribute('transform','translate('+RA.x+','+RA.y+') rotate('+wd+')');
B.ring.setAttribute('transform','translate('+BB.x+','+BB.y+') rotate('+cd+')');
B.chain.setAttribute('stroke-dashoffset',F2(mod(wa*SRp,3)));
const c=Math.cos(ca), s=Math.sin(ca);
const pN={x:BB.x+CRANK*c,y:BB.y+CRANK*s}, pF={x:BB.x-CRANK*c,y:BB.y-CRANK*s};
B.nearCrank.setAttribute('x2',F2(pN.x)); B.nearCrank.setAttribute('y2',F2(pN.y));
B.farCrank.setAttribute('x2',F2(pF.x)); B.farCrank.setAttribute('y2',F2(pF.y));
B.nearPedal.setAttribute('transform','translate('+F2(pN.x)+','+F2(pN.y)+')');
B.farPedal.setAttribute('transform','translate('+F2(pF.x)+','+F2(pF.y)+')');
const lift=flapLift();
const sp=Math.min(1,S.speed/90);
const bob=Math.cos(2*ca)*0.9*sp-2.4*lift;
B.body.setAttribute('transform','translate(0,'+F2(bob)+')');
setLeg(B.nearLeg,{x:HIP.x,y:HIP.y+bob},pN,s);
setLeg(B.farLeg,{x:HIP2.x,y:HIP2.y+bob},pF,-s);
const nod=Math.sin(2*ca+0.6)*1.6*sp-9*lift;
B.head.setAttribute('transform','rotate('+F2(nod)+' 14 -163)');
const sag=clamp(9+S.pp+Math.sin(S.t*2.3)*0.5,2,22);
B.pouch.setAttribute('d',pouchD(sag));
setWings(B,bob,lift);
const bt=S.bellT;
const ba=bt<1.4?Math.sin(bt*42)*16*Math.exp(-bt*3.2):0;
B.bell.setAttribute('transform','rotate('+F2(ba)+' 45 -102)');
const op=bt<1.2?Math.min(1,(1.2-bt)*5):0;
const sc=0.5+0.5*Math.min(1,bt*7)+Math.sin(Math.min(bt*7,Math.PI))*0.12;
B.bub.setAttribute('opacity',F2(op));
B.bub.setAttribute('transform','translate(52,-118) scale('+F2(sc)+') translate(-52,118)');
const wig=Math.pow(Math.max(0,Math.sin(S.t*0.7)),10);
B.fish.setAttribute('transform','rotate('+F2(Math.sin(S.t*16)*9*wig)+' 62 -93)');
}
/* ---------- Adaptive quality ---------- */
let q=2, fpsT=0, fpsN=0, warm=0;
function applyQ(){
const show=(g,v)=>{ if(g) g.style.display=v?'':'none'; };
show(qGroups.sparks,q>=2); show(qGroups.gulls,q>=2);
show(qGroups.bunting,q>=1); show(qGroups.island,q>=1); show(qGroups.waveFar,q>=1);
svg.setAttribute('shape-rendering',q===0?'optimizeSpeed':'auto');
}
function perf(raw){
warm+=raw; if(warm<2) return;
fpsT+=raw; fpsN++;
if(fpsT>=2){ const fps=fpsN/fpsT; fpsT=0; fpsN=0; if(fps<42&&q>0){ q--; applyQ(); } }
}
/* ---------- Demo mode ---------- */
const demo={on:false,t:0};
function toScreen(wx,wy){
const r=stage.getBoundingClientRect();
const s=Math.max(r.width/vbW,r.height/vbH);
const ox=r.left+(r.width-vbW*s)/2, oy=r.top+(r.height-vbH*s)/2;
return {x:ox+wx*k*s,y:oy+wy*k*s};
}
function thumbPos(v){ const r=slider.getBoundingClientRect(); return {x:r.left+15+(v/100)*(r.width-30),y:r.top+r.height/2}; }
function btnPos(el){ const r=el.getBoundingClientRect(); return {x:r.left+r.width/2,y:r.top+r.height/2}; }
const bodyPos=()=>toScreen(bikeX-12,groundY-112);
const headPos=()=>toScreen(bikeX+45,groundY-160);
const skyPos=()=>toScreen(W*0.25,horizonY*0.5);
const KF=[
{t:0,at:()=>thumbPos(40),val:40},
{t:1.4,at:bodyPos},
{t:1.7,at:bodyPos,act:'tap'},
{t:3.2,at:()=>thumbPos(40),val:40,act:'down'},
{t:5.6,at:()=>thumbPos(90),val:90,drag:true,act:'up'},
{t:6.8,at:()=>btnPos(bellBtn)},
{t:7.0,at:()=>btnPos(bellBtn),act:'bell'},
{t:8.4,at:headPos},
{t:8.6,at:headPos,act:'tap'},
{t:10.2,at:skyPos},
{t:10.4,at:skyPos,act:'tap'},
{t:11.8,at:()=>btnPos(bellBtn)},
{t:12.0,at:()=>btnPos(bellBtn),act:'bell'},
{t:13.4,at:()=>thumbPos(90),val:90,act:'down'},
{t:15.6,at:()=>thumbPos(12),val:12,drag:true,act:'up'},
{t:17.0,at:bodyPos},
{t:17.2,at:bodyPos,act:'tap'},
{t:18.6,at:()=>thumbPos(12),val:12,act:'down'},
{t:20.0,at:()=>thumbPos(40),val:40,drag:true,act:'up'}
];
const DEMO_LEN=20;
function ptrTap(){ ptr.classList.remove('tap'); void ptr.offsetWidth; ptr.classList.add('tap'); }
function doAct(a){
if(a==='down') ptr.classList.add('down');
else if(a==='up') ptr.classList.remove('down');
else if(a==='tap'){ ptrTap(); flap(); }
else if(a==='bell'){ ptrTap(); pressVis(bellBtn); ring(); }
}
function demoUpdate(dt){
const prev=demo.t; demo.t+=dt;
const tau=demo.t%DEMO_LEN, ptau=prev%DEMO_LEN;
if(prev>0){
for(const kf of KF){
if(!kf.act) continue;
const hit=ptau<=tau?(kf.t>ptau&&kf.t<=tau):(kf.t>ptau||kf.t<=tau);
if(hit) doAct(kf.act);
}
}
let i=0; while(i<KF.length-2&&KF[i+1].t<=tau) i++;
const a=KF[i], b=KF[i+1];
const u=clamp((tau-a.t)/(b.t-a.t),0,1);
let pos;
if(b.drag){ const v=lerp(a.val,b.val,u); setSpeed(v); pos=thumbPos(v); }
else { const pa=a.at(), pb=b.at(); const e=u<0.5?2*u*u:1-Math.pow(-2*u+2,2)/2; pos={x:lerp(pa.x,pb.x,e),y:lerp(pa.y,pb.y,e)}; }
ptr.style.transform='translate('+F(pos.x)+'px,'+F(pos.y)+'px)';
}
function endDemo(){
if(!demo.on) return;
demo.on=false; document.body.classList.remove('demo');
ptr.style.display='none'; ptr.classList.remove('down','tap');
hint.textContent='Tap anywhere to flap!'; hint.classList.remove('hide'); hideHint(4000);
}
/* ---------- Input ---------- */
['pointerdown','keydown'].forEach(ev=>window.addEventListener(ev,()=>ensureAudio(),{capture:true}));
['pointerdown','keydown','wheel','touchstart'].forEach(ev=>window.addEventListener(ev,e=>{ if(e.isTrusted) endDemo(); },{capture:true,passive:true}));
stage.addEventListener('pointerdown',e=>{ if(e.button>0) return; flap(); hideHint(600); });
stage.addEventListener('contextmenu',e=>e.preventDefault());
slider.addEventListener('input',()=>setSpeed(+slider.value));
bellBtn.addEventListener('click',()=>ring());
muteBtn.addEventListener('click',()=>toggleMute());
restartBtn.addEventListener('click',()=>restart());
window.addEventListener('keydown',e=>{
const tag=e.target&&e.target.tagName, key=e.key;
if(key===' '||key==='Spacebar'||key==='f'||key==='F'){
if((tag==='BUTTON'||tag==='INPUT')&&(key===' '||key==='Spacebar')) return;
e.preventDefault(); flap(); hideHint(600);
} else if(key==='b'||key==='B'){ ring(); pressVis(bellBtn); }
else if(key==='m'||key==='M'){ toggleMute(); pressVis(muteBtn); }
else if(key==='r'||key==='R'){ restart(); pressVis(restartBtn); }
else if((key==='ArrowRight'||key==='ArrowUp')&&tag!=='INPUT'){ e.preventDefault(); setSpeed(+slider.value+10); }
else if((key==='ArrowLeft'||key==='ArrowDown')&&tag!=='INPUT'){ e.preventDefault(); setSpeed(+slider.value-10); }
});
/* ---------- Resize ---------- */
let rebuildPending=false;
function scheduleRebuild(){
if(rebuildPending) return;
rebuildPending=true;
requestAnimationFrame(()=>{
rebuildPending=false;
const r=stage.getBoundingClientRect();
if(Math.abs(r.width-builtW)<1&&Math.abs(r.height-builtH)<1) return;
build(); applyQ(); render();
});
}
if(window.ResizeObserver){ try{ new ResizeObserver(scheduleRebuild).observe(stage); }catch(e){ window.addEventListener('resize',scheduleRebuild); } }
else window.addEventListener('resize',scheduleRebuild);
window.addEventListener('orientationchange',scheduleRebuild);
/* ---------- Start ---------- */
updMute();
build(); applyQ(); setSpeed(40); S.speed=S.target*0.6;
if(/#demo$/.test(location.href)){
demo.on=true; document.body.classList.add('demo'); ptr.style.display='block';
hint.textContent='Demo: tap anywhere to take over';
} else {
hideHint(7000);
}
render();
let lastNow=0, frames=0, readySent=false;
function loop(now){
if(!lastNow) lastNow=now;
const raw=(now-lastNow)/1000; lastNow=now;
const dt=Math.min(0.05,Math.max(0,raw));
if(demo.on) demoUpdate(dt);
step(dt);
render();
if(raw>0&&raw<0.25) perf(raw);
if(!readySent){ frames++; if(frames>=2){ readySent=true; try{ window.parent.postMessage({vcb:'ready'},'*'); }catch(e){} } }
requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
})();
</script>
</body>
</html>
