Skip to content
VibeCodeBench
Back to the app

The code

Notebook Mini-Golf by Claude Fable 5.1

Exactly as the AI wrote it. Shown as text, so nothing runs on this page.

Try 1 · index.html

worksshown27.7 KB · 255 lines · no libraries
Download
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Notebook Mini Golf</title>
<style>
html,body{margin:0;height:100%;background:#d8d2c2;overflow:hidden;font-family:"Segoe Print","Bradley Hand","Comic Sans MS","Chalkboard SE",cursive;touch-action:none;-webkit-user-select:none;user-select:none}
#app{position:fixed;inset:0;display:flex;flex-direction:column}
#hud{display:flex;align-items:center;gap:6px;padding:5px 8px;background:#f7f4e9;border-bottom:2px solid #b9c7e6;color:#1f3a93;font-size:15px}
#hud .info{flex:1;min-width:90px;font-size:14px;line-height:1.15}
button{min-width:44px;min-height:44px;border:2px solid #1f3a93;background:#fffdf5;color:#1f3a93;border-radius:12px 16px 13px 17px/15px 11px 17px 13px;font:inherit;font-size:14px;padding:0 10px;cursor:pointer;touch-action:manipulation}
button:active{background:#ffe866}
#wrap{flex:1;position:relative;min-height:0}
canvas{position:absolute;inset:0;width:100%;height:100%;display:block}
#card{position:absolute;inset:0;display:none;align-items:center;justify-content:center;background:rgba(60,60,40,.35)}
#card.show{display:flex}
#card .paper{background:#fffdf3;border:2px solid #1f3a93;border-radius:14px 18px 12px 20px/18px 12px 20px 14px;padding:14px 18px;color:#1f3a93;max-width:92vw;max-height:90%;overflow:auto;box-shadow:5px 7px 0 rgba(31,58,147,.25);text-align:center}
#card h2{margin:4px 0 8px;font-size:22px}
table{border-collapse:collapse;font-size:14px;margin:0 auto}
td,th{padding:3px 6px;border-bottom:1px dashed #9db0dc;text-align:center}
#msg{margin:10px 0;font-size:16px;background:rgba(255,236,80,.6);display:inline-block;padding:2px 8px}
</style>
</head>
<body>
<div id="app">
  <div id="hud">
    <div class="info" id="info">Hole 1</div>
    <button id="bReset" title="Reset hole (R)">Reset</button>
    <button id="bMute" title="Mute (M)">Mute</button>
    <button id="bNew" title="Start a new game">New</button>
  </div>
  <div id="wrap">
    <canvas id="c"></canvas>
    <div id="card"><div class="paper"><h2>Scorecard</h2><div id="tbl"></div><div id="msg"></div><br><button id="bAgain">Play again</button></div></div>
  </div>
</div>
<script>
(()=>{
'use strict';
const W=420,H=600,B={x:70,y:70,w:330,h:500},BR=10,CR=16,MAXV=820;
const INK='#1f3a93',RED='#d6453f',HL='rgba(255,236,80,.55)',FONT='"Segoe Print","Bradley Hand","Comic Sans MS","Chalkboard SE",cursive';
let seed=20260930;
const rnd=()=>{seed|=0;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};
const hash=n=>{const s=Math.sin(n*127.1+311.7)*43758.5453;return s-Math.floor(s)};
const wob=k=>hash(k)-.5;
const clamp=(v,a,b)=>v<a?a:v>b?b:v;
const ease=u=>u<.5?2*u*u:1-Math.pow(-2*u+2,2)/2;

const HOLES=[
{name:'the paper-clip bumper',par:2,tee:[235,520],cup:[235,135],clips:[[235,320,22]]},
{name:'the eraser block',par:3,tee:[120,520],cup:[350,135],erasers:[[150,290,170,46],[300,420,70,36]]},
{name:'the shaded slope',par:3,tee:[120,520],cup:[350,140],slopes:[[70,230,330,150,0,230]]},
{name:'ink-blot water',par:3,tee:[235,520],cup:[235,135],water:[[235,320,62],[175,300,38],[295,345,40]]},
{name:'the sticky-tape bridge',par:3,tee:[235,520],cup:[235,135],water:[[110,320,55],[175,318,55],[235,322,55],[295,318,55],[360,320,55]],bridges:[[200,250,70,140]]},
{name:'the ruler gate',par:3,tee:[235,520],cup:[235,135],erasers:[[70,300,60,20],[340,300,60,20]],gates:[{x:235,y:310,w:150,h:18,range:110,speed:1.4}]},
{name:'the staple ramp',par:3,tee:[235,520],cup:[235,135],erasers:[[70,296,330,22]],ramps:[[195,360,80,44]]},
{name:'the spiral tunnel',par:3,tee:[235,520],cup:[235,135],erasers:[[70,296,330,22]],tunnels:[{in:[92,440],out:[92,200],dir:[.95,-.32]}]},
{name:'the works',par:4,tee:[120,530],cup:[350,120],clips:[[235,420,20]],water:[[320,260,46]],slopes:[[70,180,180,90,-130,0]],erasers:[[130,250,80,40]],gates:[{x:235,y:340,w:130,h:16,range:120,speed:1.7}]}
];
const DEMO_SHOTS={0:[[-101,.78]],3:[[-90,.52],[-118,.9]],5:[[-90,.85]],8:[[-72,.95]]};

// ---------- DOM / canvas ----------
const cv=document.getElementById('c'),ctx=cv.getContext('2d'),wrap=document.getElementById('wrap'),info=document.getElementById('info'),card=document.getElementById('card'),tbl=document.getElementById('tbl'),msg=document.getElementById('msg');
let dpr=Math.min(2,window.devicePixelRatio||1),cw=1,ch=1,scale=1,ox=0,oy=0,bg=null;
function resize(){cw=wrap.clientWidth||1;ch=wrap.clientHeight||1;cv.width=Math.round(cw*dpr);cv.height=Math.round(ch*dpr);scale=Math.min(cw/W,ch/H);ox=(cw-W*scale)/2;oy=(ch-H*scale)/2;bg=null;}
window.addEventListener('resize',resize);
function world(e){const r=cv.getBoundingClientRect();return[(e.clientX-r.left-ox)/scale,(e.clientY-r.top-oy)/scale]}

// ---------- sound ----------
let AC=null,muted=false;
function audio(){if(!AC){try{AC=new(window.AudioContext||window.webkitAudioContext)()}catch(e){AC=null}}if(AC&&AC.state==='suspended'){try{AC.resume()}catch(e){}}}
function tone(f,d,type,g,slide,delay){if(!AC||muted)return;try{const o=AC.createOscillator(),G=AC.createGain(),t=AC.currentTime+(delay||0);o.type=type||'sine';o.frequency.setValueAtTime(f,t);if(slide)o.frequency.exponentialRampToValueAtTime(slide,t+d);G.gain.setValueAtTime(g||.15,t);G.gain.exponentialRampToValueAtTime(.001,t+d);o.connect(G);G.connect(AC.destination);o.start(t);o.stop(t+d+.02)}catch(e){}}
const SFX={putt:()=>tone(180,.09,'triangle',.25,60),bump:()=>tone(300,.06,'square',.07,200),boing:()=>tone(420,.2,'sine',.15,950),sink:()=>{[523,659,784,1046].forEach((f,i)=>tone(f,.25,'triangle',.14,0,i*.09))},splash:()=>{tone(600,.35,'sawtooth',.08,120);tone(200,.3,'sine',.12,60,.05)},lip:()=>tone(700,.12,'square',.06,300),jump:()=>tone(250,.25,'sine',.14,700),tunnel:()=>tone(300,.3,'triangle',.1,90),tape:()=>tone(900,.04,'square',.03)};
function setMute(m){muted=m;document.getElementById('bMute').textContent=muted?'Unmute':'Mute'}

// ---------- wobbly drawing helpers ----------
function wpath(c,pts,a,sd,close){c.beginPath();let k=sd;const P=close?pts.concat([pts[0]]):pts;c.moveTo(P[0][0],P[0][1]);
 for(let i=1;i<P.length;i++){const x1=P[i-1][0],y1=P[i-1][1],x2=P[i][0],y2=P[i][1],L=Math.hypot(x2-x1,y2-y1)||1,n=Math.max(1,Math.ceil(L/12)),nx=-(y2-y1)/L,ny=(x2-x1)/L;
  for(let j=1;j<=n;j++){const t=j/n,o=(j<n?wob(k+=1.7):0)*a;c.lineTo(x1+(x2-x1)*t+nx*o,y1+(y2-y1)*t+ny*o)}}
 if(close)c.closePath()}
function wrect(c,x,y,w,h,a,sd){wpath(c,[[x,y],[x+w,y],[x+w,y+h],[x,y+h]],a,sd,true)}
function wcirc(c,cx,cy,r,a,sd){c.beginPath();const n=Math.max(10,Math.ceil(r*.9));for(let i=0;i<=n;i++){const t=i/n*Math.PI*2,rr=r+wob(sd+(i%n)*2.3)*a,X=cx+Math.cos(t)*rr,Y=cy+Math.sin(t)*rr;i?c.lineTo(X,Y):c.moveTo(X,Y)}c.closePath()}
function pen(c,w,col,al){c.strokeStyle=col||INK;c.lineWidth=w||1.6;c.lineCap='round';c.lineJoin='round';c.globalAlpha=al==null?1:al}
function txt(c,s,x,y,size,col,align,bold){c.font=(bold?'bold ':'')+size+'px '+FONT;c.fillStyle=col||INK;c.textAlign=align||'left';c.textBaseline='middle';c.fillText(s,x,y)}
function arrow(c,x,y,dx,dy,len,sd){const L=Math.hypot(dx,dy)||1;dx/=L;dy/=L;const ex=x+dx*len,ey=y+dy*len;wpath(c,[[x,y],[ex,ey]],1,sd);c.stroke();wpath(c,[[ex-dx*8-dy*5,ey-dy*8+dx*5],[ex,ey],[ex-dx*8+dy*5,ey-dy*8-dx*5]],.6,sd+3);c.stroke()}

// ---------- static layer ----------
function drawPaper(c,y0,y1){
 c.fillStyle='#fdfcf5';c.fillRect(0,y0,W,y1-y0);
 c.strokeStyle='rgba(120,160,220,.5)';c.lineWidth=1;
 for(let y=Math.floor(y0/24)*24;y<y1;y+=24){c.beginPath();c.moveTo(0,y+.5);c.lineTo(W,y+.5);c.stroke()}
 c.strokeStyle='rgba(230,90,90,.6)';c.lineWidth=1.5;c.beginPath();c.moveTo(58,y0);c.lineTo(58,y1);c.stroke();
 for(let y=Math.floor(y0/40)*40+20;y<y1;y+=40){c.fillStyle='#cfcabb';c.beginPath();c.arc(17,y,6,0,7);c.fill();pen(c,2.4,'#6a6a6a');c.beginPath();c.arc(8,y-6,11,Math.PI*.55,Math.PI*1.75);c.stroke()}
 c.globalAlpha=1;
}
function drawCourse(c,h,i){
 const sd=i*101+7;
 c.fillStyle='rgba(150,235,130,.2)';wrect(c,B.x,B.y,B.w,B.h,2,sd);c.fill();
 pen(c,2.4,INK);wrect(c,B.x,B.y,B.w,B.h,2,sd);c.stroke();pen(c,1.2,INK,.45);wrect(c,B.x,B.y,B.w,B.h,2.5,sd+9);c.stroke();c.globalAlpha=1;
 c.fillStyle=HL;wrect(c,B.x-4,B.y-45,190,22,2,sd+1);c.fill();
 txt(c,'Hole '+(i+1)+'  ·  par '+h.par,B.x,B.y-34,18,INK,'left',true);
 txt(c,h.name,B.x+200,B.y-34,14,INK,'left');
 txt(c,'tee',h.tee[0]+14,h.tee[1]+10,11,'rgba(31,58,147,.7)','left');
 (h.slopes||[]).forEach((s,k)=>{c.fillStyle=HL;wrect(c,s[0],s[1],s[2],s[3],2,sd+k*5);c.fill();pen(c,1,INK,.3);
  for(let x=s[0]-s[3];x<s[0]+s[2];x+=18){c.beginPath();c.moveTo(clamp(x,s[0],s[0]+s[2]),clamp(s[1]+(x<s[0]?s[0]-x:0),s[1],s[1]+s[3]));c.lineTo(clamp(x+s[3],s[0],s[0]+s[2]),clamp(s[1]+s[3]-(x+s[3]>s[0]+s[2]?x+s[3]-s[0]-s[2]:0),s[1],s[1]+s[3]));c.stroke()}
  pen(c,1.8,INK,.8);for(let a=0;a<4;a++){const ax=s[0]+s[2]*(a+.5)/4-s[4]/Math.hypot(s[4],s[5])*12,ay=s[1]+s[3]/2-s[5]/Math.hypot(s[4],s[5])*12;arrow(c,ax,ay,s[4],s[5],26,sd+a)}
  txt(c,'slope!',s[0]+6,s[1]+12,12,INK,'left');c.globalAlpha=1});
 (h.water||[]).forEach((w,k)=>{c.fillStyle='#1b2f7a';c.globalAlpha=.92;wcirc(c,w[0],w[1],w[2],w[2]*.3,sd+k*7);c.fill();
  for(let d=0;d<7;d++){const a=hash(sd+k*13+d)*6.28,r=w[2]*(1+hash(sd+k*17+d)*.45);c.beginPath();c.arc(w[0]+Math.cos(a)*r,w[1]+Math.sin(a)*r,1.5+hash(sd+d*3+k)*3,0,7);c.fill()}
  pen(c,1.5,'rgba(255,255,255,.5)');wpath(c,[[w[0]-w[2]*.4,w[1]-w[2]*.35],[w[0]-w[2]*.1,w[1]-w[2]*.5]],1,sd+k);c.stroke();c.globalAlpha=1});
 (h.bridges||[]).forEach((b,k)=>{const z=[];for(let x=b[0];x<=b[2]+b[0]-.1;x+=8)z.push([x,b[1]+(z.length%2?3:0)]);z.push([b[0]+b[2],b[1]+3]);const z2=[];for(let x=b[0]+b[2];x>=b[0]+.1;x-=8)z2.push([x,b[1]+b[3]-(z2.length%2?3:0)]);z2.push([b[0],b[1]+b[3]]);
  c.fillStyle='rgba(245,225,160,.9)';wpath(c,z.concat(z2),.6,sd+k,true);c.fill();pen(c,1,'#b89b4a',.8);c.stroke();
  pen(c,1,'#b89b4a',.35);for(let y=b[1]+10;y<b[1]+b[3];y+=10){c.beginPath();c.moveTo(b[0]+4,y);c.lineTo(b[0]+b[2]-4,y);c.stroke()}
  c.save();c.translate(b[0]+b[2]/2,b[1]+b[3]/2);c.rotate(-Math.PI/2);txt(c,'sticky tape',0,0,13,'#8a6d1f','center',true);c.restore();c.globalAlpha=1});
 (h.erasers||[]).forEach((e,k)=>{c.fillStyle='#f5b7cf';wrect(c,e[0],e[1],e[2],e[3],1.5,sd+k*3);c.fill();c.fillStyle='rgba(120,70,100,.25)';c.fillRect(e[0]+2,e[1]+e[3]*.65,e[2]-4,e[3]*.33);pen(c,2,INK);wrect(c,e[0],e[1],e[2],e[3],1.5,sd+k*3);c.stroke();pen(c,1,INK,.4);wrect(c,e[0],e[1],e[2],e[3],2.2,sd+k*3+50);c.stroke();c.globalAlpha=1;
  if(e[2]>=60)txt(c,'ERASER',e[0]+e[2]/2,e[1]+e[3]*.36,Math.min(13,e[3]*.5),'#8a3a5c','center',true)});
 (h.clips||[]).forEach((p,k)=>{c.fillStyle='rgba(110,190,255,.4)';wcirc(c,p[0],p[1],p[2],1.5,sd+k);c.fill();c.save();c.translate(p[0],p[1]);c.rotate(.6);pen(c,2.4,'#5d6a7a');
  const r=p[2]*.9;c.beginPath();if(c.roundRect){c.roundRect(-r*.45,-r,r*.9,r*2,r*.45);c.stroke();c.beginPath();c.roundRect(-r*.28,-r*.72,r*.56,r*1.5,r*.28)}else{c.rect(-r*.45,-r,r*.9,r*2);c.stroke();c.beginPath();c.rect(-r*.28,-r*.72,r*.56,r*1.5)}c.stroke();c.restore();
  txt(c,'boing',p[0]+p[2]+4,p[1]-p[2],11,'rgba(31,58,147,.7)','left')});
 (h.ramps||[]).forEach((r,k)=>{c.fillStyle='rgba(120,230,140,.5)';wrect(c,r[0],r[1],r[2],r[3],1.5,sd+k);c.fill();pen(c,4,'#7a7f88');wpath(c,[[r[0]+8,r[1]+r[3]-4],[r[0]+8,r[1]+4],[r[0]+r[2]-8,r[1]+4],[r[0]+r[2]-8,r[1]+r[3]-4]],.8,sd+k);c.stroke();pen(c,1.5,INK);wpath(c,[[r[0]+8,r[1]+r[3]-4],[r[0]+8,r[1]+4],[r[0]+r[2]-8,r[1]+4],[r[0]+r[2]-8,r[1]+r[3]-4]],.8,sd+k+2);c.stroke();
  pen(c,2,INK);arrow(c,r[0]+r[2]/2,r[1]+r[3]+22,0,-1,18,sd+k);txt(c,'staple ramp - hit it fast!',r[0]+r[2]/2,r[1]+r[3]+36,11,INK,'center')});
 (h.tunnels||[]).forEach((t,k)=>{pen(c,1.6,INK,.7);c.setLineDash([4,5]);c.beginPath();c.moveTo(t.in[0],t.in[1]);c.lineTo(28,t.in[1]);c.lineTo(28,t.out[1]);c.lineTo(t.out[0],t.out[1]);c.stroke();c.setLineDash([]);
  [t.in,t.out].forEach((p,j)=>{c.fillStyle='#262626';wcirc(c,p[0],p[1],18,1.5,sd+k+j);c.fill();pen(c,2.4,INK);c.beginPath();c.arc(p[0],p[1]+2,22,Math.PI,Math.PI*2);c.stroke();txt(c,j?'out':'in',p[0],p[1]-30,12,INK,'center',true)});c.globalAlpha=1});
 c.fillStyle='#1a1a1a';wcirc(c,h.cup[0],h.cup[1],CR,1.2,sd+77);c.fill();pen(c,1.5,INK);c.stroke();
 pen(c,2.2,INK);wpath(c,[[h.cup[0]+2,h.cup[1]-2],[h.cup[0]+4,h.cup[1]-46]],1,sd+3);c.stroke();c.fillStyle=RED;wpath(c,[[h.cup[0]+4,h.cup[1]-46],[h.cup[0]+30,h.cup[1]-36],[h.cup[0]+4,h.cup[1]-26]],1,sd+5,true);c.fill();pen(c,1.4,INK);c.stroke();
 pen(c,2,INK);wpath(c,[[h.tee[0]-6,h.tee[1]-6],[h.tee[0]+6,h.tee[1]+6]],.6,sd+21);c.stroke();wpath(c,[[h.tee[0]+6,h.tee[1]-6],[h.tee[0]-6,h.tee[1]+6]],.6,sd+22);c.stroke();
 if(i===0){txt(c,'drag back from the ball like a slingshot, let go to putt',B.x+B.w/2,B.y+B.h+16,12,'rgba(31,58,147,.75)','center')}
}
function buildBG(){bg=document.createElement('canvas');bg.width=cv.width;bg.height=cv.height;const c=bg.getContext('2d');const S=scale*dpr;c.setTransform(S,0,0,S,ox*dpr,oy*dpr);
 const x0=-ox/scale,y0=-oy/scale,x1=x0+cw/scale,y1=y0+ch/scale;c.fillStyle='#d8d2c2';c.fillRect(x0-2,y0-2,x1-x0+4,y1-y0+4);c.fillStyle='rgba(0,0,0,.14)';c.fillRect(7,y0,W,y1-y0);drawPaper(c,y0,y1);drawCourse(c,HOLES[hole],hole)}

// ---------- game state ----------
const ball={x:0,y:0,vx:0,vy:0,moving:false,air:0,hide:0,exit:null,tcd:0,lipcd:0,rcd:0};
let hole=0,strokes=0,scores=[],state='play',lastShot=[0,0],T=0,banner=0,sunkT=0,splash=null,aim=null,notes=[],parts=[],readySent=false;
let demo=location.hash==='#demo',dm=null;
function note(text,col,y,size){notes.push({text,col:col||INK,y:clamp(y==null?ball.y:y,120,H-60),life:0,max:2.6,size:size||14,rot:(hash(notes.length*3+T)-.5)*.15})}
function loadHole(i){hole=i;strokes=0;const h=HOLES[i];ball.x=h.tee[0];ball.y=h.tee[1];ball.vx=ball.vy=0;ball.moving=false;ball.air=0;ball.hide=0;ball.exit=null;ball.tcd=0;ball.lipcd=0;ball.rcd=0;lastShot=[ball.x,ball.y];state='play';banner=2.4;splash=null;aim=null;parts=[];bg=null;hud()}
function newGame(){scores=[];card.classList.remove('show');notes=[];loadHole(0)}
function resetHole(){if(state==='card')return;loadHole(hole)}
function hud(){const h=HOLES[hole];let tot=0,tp=0;for(let k=0;k<scores.length;k++){tot+=scores[k];tp+=HOLES[k].par}const d=tot-tp;info.innerHTML='Hole '+(hole+1)+'/9 · par '+h.par+' · strokes '+strokes+'<br>total '+tot+' ('+(d>0?'+':'')+d+' vs par)'}
function shoot(dx,dy,p){SFX.putt();ball.vx=dx*p*MAXV;ball.vy=dy*p*MAXV;ball.moving=true;lastShot=[ball.x,ball.y];strokes++;if(p>.96)note('FULL SEND!!',RED);hud()}
function finishHole(){scores[hole]=strokes;state='sunk';sunkT=0;SFX.sink();const h=HOLES[hole],d=strokes-h.par;note(strokes===1?'HOLE IN ONE!!!':d<=-2?'EAGLE!! wow':d===-1?'birdie! nice':d===0?'par. solid.':d===1?'bogey, meh':'well... it went in',d<=0?INK:RED,ball.y,d<0?17:14);
 for(let k=0;k<(dpr>1?34:22);k++){const a=rnd()*6.28,s=90+rnd()*220;parts.push({x:ball.x,y:ball.y,vx:Math.cos(a)*s,vy:Math.sin(a)*s-120,r:rnd()*6.28,vr:(rnd()-.5)*8,life:0,max:1+rnd()*.6,type:(rnd()*3)|0,col:[INK,RED,'#e0b000','#2a9d4a'][(rnd()*4)|0],sz:5+rnd()*7})}}
function nextHole(){if(demo){nextDemoHole();return}if(hole+1>=HOLES.length){showCard()}else loadHole(hole+1)}
function showCard(){state='card';let r1='<tr><th>hole</th>',r2='<tr><td>par</td>',r3='<tr><td>you</td>',tp=0,ts=0;HOLES.forEach((h,k)=>{r1+='<th>'+(k+1)+'</th>';r2+='<td>'+h.par+'</td>';const s=scores[k]==null?'-':scores[k];r3+='<td style="color:'+(scores[k]<h.par?'#2a9d4a':scores[k]>h.par?RED:INK)+'">'+s+'</td>';tp+=h.par;ts+=scores[k]||0});
 tbl.innerHTML='<table>'+r1+'<th>total</th></tr>'+r2+'<td>'+tp+'</td></tr>'+r3+'<td><b>'+ts+'</b></td></tr></table>';const d=ts-tp;msg.textContent=d<0?(-d)+' under par - you doodle like a pro!':d===0?'exactly par. very tidy.':d<=5?d+' over par. respectable scribbling.':d+' over par... the pen slipped a lot.';card.classList.add('show')}

// ---------- physics ----------
function gateRect(g){const x=g.x+Math.sin(T*g.speed)*g.range-g.w/2;return[x,g.y-g.h/2,g.w,g.h,Math.cos(T*g.speed)*g.range*g.speed]}
function inRect(r){return ball.x>r[0]&&ball.x<r[0]+r[2]&&ball.y>r[1]&&ball.y<r[1]+r[3]}
function hitRect(rx,ry,rw,rh,rest){const cx=clamp(ball.x,rx,rx+rw),cy=clamp(ball.y,ry,ry+rh);let dx=ball.x-cx,dy=ball.y-cy;const d2=dx*dx+dy*dy;if(d2>=BR*BR)return false;let d=Math.sqrt(d2);
 if(d<1e-6){const l=ball.x-rx,r=rx+rw-ball.x,t=ball.y-ry,b=ry+rh-ball.y,m=Math.min(l,r,t,b);if(m===l){dx=-1;dy=0}else if(m===r){dx=1;dy=0}else if(m===t){dx=0;dy=-1}else{dx=0;dy=1}ball.x+=dx*(m+BR);ball.y+=dy*(m+BR)}else{dx/=d;dy/=d;ball.x+=dx*(BR-d);ball.y+=dy*(BR-d)}
 const vn=ball.vx*dx+ball.vy*dy;if(vn<0){ball.vx-=(1+rest)*vn*dx;ball.vy-=(1+rest)*vn*dy;return-vn}return 0}
function hitCirc(cx,cy,r,rest){let dx=ball.x-cx,dy=ball.y-cy;const d=Math.hypot(dx,dy)||.001;if(d>=r+BR)return 0;dx/=d;dy/=d;ball.x=cx+dx*(r+BR);ball.y=cy+dy*(r+BR);const vn=ball.vx*dx+ball.vy*dy;if(vn<0){ball.vx-=(1+rest)*vn*dx;ball.vy-=(1+rest)*vn*dy;return-vn}return 0}
function doSplash(){state='splash';splash={x:ball.x,y:ball.y,t:0};ball.moving=false;ball.vx=ball.vy=0;SFX.splash();strokes++;note('SPLASH! +1 stroke',RED,ball.y,15);hud()}
function step(dt){
 T+=dt;const h=HOLES[hole];
 if(state==='sunk'){sunkT+=dt;if(sunkT>1.6)nextHole();return}
 if(state==='splash'){splash.t+=dt;if(splash.t>1){ball.x=lastShot[0];ball.y=lastShot[1];state='play';splash=null}return}
 if(state!=='play'||!ball.moving)return;
 if(ball.hide>0){ball.hide-=dt;if(ball.hide<=0&&ball.exit){const sp=Math.max(160,Math.hypot(ball.vx,ball.vy)*.9),e=ball.exit;ball.x=e[0];ball.y=e[1];ball.vx=e[2]*sp;ball.vy=e[3]*sp;ball.exit=null;ball.tcd=1}return}
 ball.tcd=Math.max(0,ball.tcd-dt);ball.lipcd=Math.max(0,ball.lipcd-dt);ball.rcd=Math.max(0,ball.rcd-dt);
 const sp0=Math.hypot(ball.vx,ball.vy),n=Math.max(1,Math.ceil(sp0*dt/4)),sdt=dt/n;
 for(let i=0;i<n;i++){
  if(ball.air<=0)(h.slopes||[]).forEach(s=>{if(inRect(s)){ball.vx+=s[4]*sdt;ball.vy+=s[5]*sdt}});
  const sp=Math.hypot(ball.vx,ball.vy);if(sp>0){const f=Math.max(0,sp-(sp*.9+50)*sdt)/sp;ball.vx*=f;ball.vy*=f}
  ball.x+=ball.vx*sdt;ball.y+=ball.vy*sdt;
  if(ball.air>0){ball.air-=sdt;if(ball.air<=0){ball.air=0;let wet=false;(h.water||[]).forEach(w=>{if(Math.hypot(ball.x-w[0],ball.y-w[1])<w[2]-4)wet=true});if(wet){doSplash();return}SFX.bump()}}
  let b=0;if(ball.x<B.x+BR){ball.x=B.x+BR;if(ball.vx<0){b=-ball.vx;ball.vx*=-.6}}if(ball.x>B.x+B.w-BR){ball.x=B.x+B.w-BR;if(ball.vx>0){b=ball.vx;ball.vx*=-.6}}if(ball.y<B.y+BR){ball.y=B.y+BR;if(ball.vy<0){b=-ball.vy;ball.vy*=-.6}}if(ball.y>B.y+B.h-BR){ball.y=B.y+B.h-BR;if(ball.vy>0){b=ball.vy;ball.vy*=-.6}}if(b>80)SFX.bump();
  if(ball.air>0)continue;
  (h.erasers||[]).forEach(e=>{if(hitRect(e[0],e[1],e[2],e[3],.55)>80)SFX.bump()});
  (h.gates||[]).forEach(g=>{const r=gateRect(g);if(hitRect(r[0],r[1],r[2],r[3],.7)>0){ball.vx+=r[4]*.4;SFX.tape()}});
  (h.clips||[]).forEach(p=>{const v=hitCirc(p[0],p[1],p[2],1.15);if(v>60){SFX.boing();if(v>300)note('boing!',INK,p[1])}});
  (h.ramps||[]).forEach(r=>{if(ball.rcd<=0&&inRect(r)&&ball.vy<-180){ball.air=.55;ball.rcd=1;SFX.jump();note('AIRBORNE!',INK)}else if(ball.rcd<=0&&inRect(r)&&ball.vy<0&&ball.vy>-180&&Math.hypot(ball.vx,ball.vy)<120){ball.rcd=1;note('too slow for the ramp',RED)}});
  (h.tunnels||[]).forEach(t=>{if(ball.tcd<=0&&Math.hypot(ball.x-t.in[0],ball.y-t.in[1])<18){ball.hide=.45;ball.exit=[t.out[0],t.out[1],t.dir[0],t.dir[1]];ball.x=t.in[0];ball.y=t.in[1];SFX.tunnel();note('through the binding!',INK,t.in[1])}});
  if(ball.hide>0)return;
  let wet=false;(h.water||[]).forEach(w=>{if(Math.hypot(ball.x-w[0],ball.y-w[1])<w[2]-4)wet=true});if(wet&&!(h.bridges||[]).some(inRect)){doSplash();return}
  const dx=ball.x-h.cup[0],dy=ball.y-h.cup[1],d=Math.hypot(dx,dy),s2=Math.hypot(ball.vx,ball.vy);
  if(d<CR+BR&&ball.lipcd<=0){
   if(s2<260){if(d<CR-3){ball.moving=false;ball.vx=ball.vy=0;ball.x=h.cup[0];ball.y=h.cup[1];finishHole();return}ball.vx-=dx/d*700*sdt;ball.vy-=dy/d*700*sdt}
   else if(d<CR+2&&s2<700){const a=Math.atan2(ball.vy,ball.vx)+(hash(T*13)>.5?1:-1)*1.15,ns=s2*.42;ball.vx=Math.cos(a)*ns;ball.vy=Math.sin(a)*ns;ball.lipcd=.6;SFX.lip();note('lipped out!!',RED,ball.y-30,15)}
   else if(d<CR+2){ball.lipcd=.5;note('too fast! flew right over',RED,ball.y-30)}
  }
 }
 if(Math.hypot(ball.vx,ball.vy)<12){ball.vx=ball.vy=0;ball.moving=false;const d=Math.hypot(ball.x-h.cup[0],ball.y-h.cup[1]);
  if(d<45)note('so close!!',INK);else if(strokes===h.par+3)note('oh no...',RED);else if(strokes===h.par+5)note('this is fine.',RED);
  if(strokes>=10){note('max 10, pick up',RED,ball.y,13);finishHole()}}
}

// ---------- dynamic drawing ----------
function drawRuler(c,r){c.fillStyle='rgba(255,228,120,.95)';wrect(c,r[0],r[1],r[2],r[3],.8,hole*7);c.fill();pen(c,1.6,INK);c.stroke();pen(c,1,INK,.8);
 for(let x=r[0]+6,k=0;x<r[0]+r[2]-2;x+=6,k++){c.beginPath();c.moveTo(x,r[1]);c.lineTo(x,r[1]+(k%5?3.5:7));c.stroke()}c.globalAlpha=1;txt(c,'ruler',r[0]+r[2]/2,r[1]+r[3]*.62,10,INK,'center')}
function drawBall(c){if(ball.hide>0)return;const z=ball.air>0?Math.sin(Math.PI*(1-ball.air/.55))*38:0;
 if(z>0){c.fillStyle='rgba(0,0,0,.2)';c.beginPath();c.ellipse(ball.x,ball.y+4,BR*.9,BR*.5,0,0,7);c.fill()}
 const k=1+z/70;c.fillStyle='#fff';wcirc(c,ball.x,ball.y-z,BR*k,.9,hole+3);c.fill();pen(c,1.8,INK);c.stroke();pen(c,1,INK,.5);wcirc(c,ball.x,ball.y-z,BR*k,1.4,hole+40);c.stroke();
 c.beginPath();c.arc(ball.x-3,ball.y-z-3,3.5*k,Math.PI*1.1,Math.PI*1.7);c.stroke();c.globalAlpha=1}
function drawAim(c){if(!aim||ball.moving)return;const a=(aim.id==='demo'?aimVec():aimVec());if(a.p<=0)return;const len=30+a.p*150;pen(c,2,INK,.9);c.setLineDash([2,7]);wpath(c,[[ball.x+a.dx*BR,ball.y+a.dy*BR],[ball.x+a.dx*len,ball.y+a.dy*len]],1.2,T*2|0);c.stroke();c.setLineDash([]);
 pen(c,2,INK);const ex=ball.x+a.dx*len,ey=ball.y+a.dy*len;wpath(c,[[ex-a.dx*9-a.dy*6,ey-a.dy*9+a.dx*6],[ex,ey],[ex-a.dx*9+a.dy*6,ey-a.dy*9-a.dx*6]],.6,3);c.stroke();
 const mx=ball.x-32,my=ball.y+26;c.fillStyle=a.p>.9?'rgba(230,80,80,.6)':a.p>.6?'rgba(255,170,60,.6)':HL;c.fillRect(mx,my,64*a.p,11);pen(c,1.6,INK);wrect(c,mx,my,64,11,.8,9);c.stroke();txt(c,'power',mx+32,my+20,10,INK,'center')}
function drawParts(c,dt){parts=parts.filter(p=>p.life<p.max);parts.forEach(p=>{p.life+=dt;p.x+=p.vx*dt;p.y+=p.vy*dt;p.vy+=160*dt;p.vx*=.985;p.r+=p.vr*dt;const u=1-p.life/p.max;c.save();c.translate(p.x,p.y);c.rotate(p.r);c.globalAlpha=Math.min(1,u*2);pen(c,1.6,p.col,Math.min(1,u*2));c.fillStyle=p.col;
  if(p.type===0){c.beginPath();for(let i=0;i<10;i++){const r=i%2?p.sz*.45:p.sz,a=i/10*6.283;c.lineTo(Math.cos(a)*r,Math.sin(a)*r)}c.closePath();c.stroke()}
  else if(p.type===1){c.beginPath();for(let i=0;i<=8;i++)c.lineTo(i*3-12,Math.sin(i*1.6)*4);c.stroke()}
  else{c.beginPath();c.arc(0,0,p.sz*.35,0,7);c.fill()}c.restore()});
 if(state==='sunk'){const u=Math.min(1,sunkT/.4);c.save();c.translate(HOLES[hole].cup[0],HOLES[hole].cup[1]-60);c.rotate(-.1);c.scale(u,u);c.fillStyle=HL;wrect(c,-44,-14,88,28,2,5);c.fill();txt(c,'IN!!',0,1,22,INK,'center',true);c.restore()}}
function drawNotes(c,dt){notes=notes.filter(n=>n.life<n.max);notes.forEach((n,i)=>{n.life+=dt;const u=n.life/n.max,s=Math.min(1,n.life*6),al=u>.75?(1-u)*4:1;c.save();c.translate(44,n.y+i*0);c.rotate(-Math.PI/2+n.rot);c.scale(s,s);c.globalAlpha=al;c.fillStyle=HL;c.fillRect(-n.text.length*n.size*.3,-n.size*.65,n.text.length*n.size*.6,n.size*1.3);txt(c,n.text,0,0,n.size,n.col,'center',true);c.restore()})}
function drawBanner(c){if(banner<=0)return;const u=banner>2?(2.4-banner)/.4:banner<.4?banner/.4:1,h=HOLES[hole];c.save();c.globalAlpha=u;c.translate(W/2,H/2-10);c.rotate(-.04);c.scale(.8+.2*u,.8+.2*u);c.fillStyle='rgba(255,253,243,.95)';wrect(c,-150,-40,300,80,2.5,hole+9);c.fill();pen(c,2,INK);c.stroke();c.fillStyle=HL;wrect(c,-120,-28,240,24,1.5,hole+2);c.fill();txt(c,'Hole '+(hole+1)+'  ·  par '+h.par,0,-16,20,INK,'center',true);txt(c,h.name,0,16,15,INK,'center');c.restore()}
function drawSplash(c){if(!splash)return;const u=splash.t;for(let k=0;k<3;k++){const r=10+u*60-k*14;if(r>0){pen(c,2,INK,Math.max(0,1-u-k*.15));wcirc(c,splash.x,splash.y,r,2,k+hole);c.stroke()}}for(let k=0;k<6;k++){const a=k/6*6.28+.4,r=20+u*40;c.fillStyle=INK;c.globalAlpha=Math.max(0,1-u*1.2);c.beginPath();c.arc(splash.x+Math.cos(a)*r,splash.y+Math.sin(a)*r-u*30,2.5,0,7);c.fill()}c.globalAlpha=1;txt(c,'splash!',splash.x,splash.y-50,16,INK,'center',true)}
function drawPointer(c){if(!dm)return;c.save();c.translate(dm.px,dm.py);if(dm.down){pen(c,1.5,INK,.5);c.beginPath();c.arc(0,0,10,0,7);c.stroke()}c.fillStyle='#fff';wpath(c,[[0,0],[0,17],[4.5,13],[8,19],[11,18],[7.5,12],[13,12]],.4,2,true);c.fill();pen(c,1.6,INK);c.stroke();c.restore();txt(c,'demo - tap to play',W/2,B.y+B.h+16,13,RED,'center',true)}

// ---------- demo ----------
function startDemo(){dm={list:[0,3,5,8],li:0,si:0,phase:'move',pt:0,px:W/2,py:H-30,from:[0,0],to:[0,0],elapsed:0,down:false};demo=true;scores=[];loadHole(0)}
function endDemo(){if(!demo)return;demo=false;dm=null;aim=null;newGame()}
function nextDemoHole(){dm.li++;if(dm.li>=dm.list.length||dm.elapsed>21){dm.li=0;dm.elapsed=0;scores=[]}dm.si=0;dm.phase='move';dm.pt=0;loadHole(dm.list[dm.li])}
function demoStep(dt){dm.elapsed+=dt;
 if(dm.phase==='move'){if(dm.pt===0){dm.from=[dm.px,dm.py];dm.to=[ball.x,ball.y]}dm.pt+=dt/.7;const u=ease(Math.min(1,dm.pt));dm.px=dm.from[0]+(dm.to[0]-dm.from[0])*u;dm.py=dm.from[1]+(dm.to[1]-dm.from[1])*u;
  if(dm.pt>=1&&banner<=0){const sh=(DEMO_SHOTS[hole]||[])[dm.si],cp=HOLES[hole].cup;let ang,p;if(sh){ang=sh[0]*Math.PI/180;p=sh[1]}else{ang=Math.atan2(cp[1]-ball.y,cp[0]-ball.x);p=clamp(Math.hypot(cp[0]-ball.x,cp[1]-ball.y)/680,.3,1)}
   dm.from=[ball.x,ball.y];dm.to=[ball.x-Math.cos(ang)*p*160,ball.y-Math.sin(ang)*p*160];aim={sx:ball.x,sy:ball.y,cx:ball.x,cy:ball.y,id:'demo'};dm.down=true;dm.phase='drag';dm.pt=0}}
 else if(dm.phase==='drag'){dm.pt+=dt/.9;const u=ease(Math.min(1,dm.pt));dm.px=dm.from[0]+(dm.to[0]-dm.from[0])*u;dm.py=dm.from[1]+(dm.to[1]-dm.from[1])*u;if(aim){aim.cx=dm.px;aim.cy=dm.py}
  if(dm.pt>=1){const a=aim?aimVec():{p:0};aim=null;dm.down=false;if(a.p>0&&state==='play')shoot(a.dx,a.dy,a.p);dm.si++;dm.phase='wait';dm.pt=0}}
 else{dm.pt+=dt;if(state==='play'&&!ball.moving&&dm.pt>.7){if(dm.si>=2)nextDemoHole();else{dm.phase='move';dm.pt=0}}}}

// ---------- input ----------
function aimVec(){const dx=aim.sx-aim.cx,dy=aim.sy-aim.cy,L=Math.hypot(dx,dy);if(L<2)return{dx:0,dy:0,p:0};return{dx:dx/L,dy:dy/L,p:Math.min(1,L/160)}}
cv.addEventListener('pointerdown',e=>{e.preventDefault();audio();if(demo){endDemo();return}if(state!=='play'||ball.moving)return;const w=world(e);aim={sx:w[0],sy:w[1],cx:w[0],cy:w[1],id:e.pointerId};try{cv.setPointerCapture(e.pointerId)}catch(_){}});
cv.addEventListener('pointermove',e=>{if(!aim||e.pointerId!==aim.id)return;const w=world(e);aim.cx=w[0];aim.cy=w[1]});
cv.addEventListener('pointerup',e=>{if(!aim||e.pointerId!==aim.id)return;const a=aimVec();aim=null;if(a.p>.05&&state==='play'&&!ball.moving)shoot(a.dx,a.dy,a.p)});
cv.addEventListener('pointercancel',e=>{if(aim&&e.pointerId===aim.id)aim=null});
window.addEventListener('keydown',e=>{if(demo)endDemo();const k=e.key.toLowerCase();if(k==='r')resetHole();if(k==='m')setMute(!muted)});
document.getElementById('bReset').addEventListener('click',()=>{audio();if(demo)endDemo();resetHole()});
document.getElementById('bMute').addEventListener('click',()=>{audio();setMute(!muted)});
document.getElementById('bNew').addEventListener('click',()=>{audio();if(demo)endDemo();else newGame()});
document.getElementById('bAgain').addEventListener('click',()=>{audio();newGame()});

// ---------- main loop ----------
let last=performance.now(),slow=0;
function frame(now){const dt=Math.min(.033,(now-last)/1000)||.016;last=now;
 if(dpr>1){slow=slow*.95+(now-last>0?0:0);}
 if(dt>=.03)slow+=dt;else slow=Math.max(0,slow-dt*.5);if(slow>2&&dpr>1){dpr=1;resize()}
 if(banner>0)banner-=dt;
 if(demo&&dm)demoStep(dt);
 step(dt);
 if(!bg)buildBG();
 ctx.setTransform(1,0,0,1,0,0);ctx.drawImage(bg,0,0);const S=scale*dpr;ctx.setTransform(S,0,0,S,ox*dpr,oy*dpr);
 const h=HOLES[hole];(h.gates||[]).forEach(g=>drawRuler(ctx,gateRect(g)));
 drawSplash(ctx);if(state!=='sunk')drawBall(ctx);drawAim(ctx);drawParts(ctx,dt);drawNotes(ctx,dt);drawBanner(ctx);drawPointer(ctx);
 if(!readySent){readySent=true;try{parent.postMessage({vcb:'ready'},'*')}catch(e){}}
 requestAnimationFrame(frame)}
resize();
if(demo)startDemo();else loadHole(0);
requestAnimationFrame(frame);
})();
</script>
</body>
</html>

Try 2 · index.html

works32.5 KB · 274 lines · no libraries
Download
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Notebook Mini Golf</title>
<style>
html,body{margin:0;height:100%;overflow:hidden;background:#fdfcf5;touch-action:none;font-family:"Segoe Print","Bradley Hand","Comic Sans MS","Chalkboard SE",cursive;user-select:none;-webkit-user-select:none}
#c{position:fixed;inset:0;width:100%;height:100%;display:block}
#hud{position:fixed;top:8px;left:8px;right:8px;display:flex;align-items:center;justify-content:space-between;pointer-events:none;color:#1b2a7a;font-size:17px;gap:8px}
#hud .info{background:rgba(255,253,244,.85);padding:6px 10px;border-radius:9px 11px 8px 12px;transform:rotate(-1deg);white-space:nowrap;border:1.5px solid #1b2a7a}
#hud .btns{display:flex;gap:8px;pointer-events:auto}
button{font:inherit;font-size:17px;min-width:44px;min-height:44px;border:2px solid #1b2a7a;border-radius:10px 12px 9px 11px;background:#fffdf4;color:#1b2a7a;cursor:pointer;padding:0 10px;box-shadow:2px 2px 0 #1b2a7a;touch-action:manipulation}
button:active{transform:translate(1px,1px);box-shadow:1px 1px 0 #1b2a7a}
#card{position:fixed;inset:0;display:none;align-items:center;justify-content:center;background:rgba(40,40,60,.35)}
#card.on{display:flex}
.paper{background:#fffdf4;border:2px solid #1b2a7a;border-radius:12px 10px 14px 9px;padding:18px 22px;color:#1b2a7a;max-width:92vw;max-height:90vh;overflow:auto;box-shadow:4px 4px 0 #1b2a7a;transform:rotate(-1.2deg);background-image:repeating-linear-gradient(transparent 0 27px,#c7dced 27px 28px)}
.paper h2{margin:0 0 8px;font-size:28px}
table{border-collapse:collapse;font-size:16px}
td,th{padding:3px 6px;text-align:center;border-bottom:1px dashed #7a8ac0}
.paper .big{font-size:22px;margin:10px 0}
#card button{font-size:20px;padding:6px 18px}
</style>
</head>
<body>
<canvas id="c"></canvas>
<div id="hud"><div class="info" id="info">hole 1</div><div class="btns"><button id="bm" aria-label="mute">&#128266;</button><button id="br" aria-label="reset hole">&#8635; reset</button></div></div>
<div id="card"><div class="paper"><h2>&#9998; scorecard</h2><div id="tbl"></div><div class="big" id="sum"></div><button id="again">play again &#9654;</button></div></div>
<script>
'use strict';
// ---------- seeded randomness ----------
function mulberry(seed){return function(){seed|=0;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}}
let rng=mulberry(20261001);const R=()=>rng();

// ---------- constants ----------
const W=600,H=820,BR=7,CR=12,LIP=270,MAXV=900,MAXDRAG=160;
const INK='#1c2b7a',RED='#d9413a',PENCIL='#6b6b6b';
const FONT='"Segoe Print","Bradley Hand","Comic Sans MS","Chalkboard SE",cursive';
const cv=document.getElementById('c'),ctx=cv.getContext('2d');
const bg=document.createElement('canvas'),bgx=bg.getContext('2d');
let sw=0,sh=0,dpr=1,scale=1,ox=0,oy=0,quality=2;

function rect(x1,y1,x2,y2){return[[x1,y1],[x2,y1],[x2,y2],[x1,y2]]}
function blob(cx,cy,r,n,seed){const g=mulberry(seed);const p=[];for(let i=0;i<n;i++){const a=i/n*Math.PI*2;const rr=r*(0.72+g()*0.5);p.push([cx+Math.cos(a)*rr,cy+Math.sin(a)*rr])}return p}
function inPoly(x,y,p){let c=false;for(let i=0,j=p.length-1;i<p.length;j=i++){const a=p[i],b=p[j];if((a[1]>y)!==(b[1]>y)&&x<(b[0]-a[0])*(y-a[1])/(b[1]-a[1])+a[0])c=!c}return c}
function bounds(p){let x1=1e9,y1=1e9,x2=-1e9,y2=-1e9;for(const q of p){x1=Math.min(x1,q[0]);y1=Math.min(y1,q[1]);x2=Math.max(x2,q[0]);y2=Math.max(y2,q[1])}return[x1,y1,x2,y2]}

// ---------- holes ----------
const HOLES=[
{par:2,title:'the paper clip',g:[rect(140,120,520,740)],start:[330,680],cup:[330,180],bumpers:[{x:330,y:430,r:30,a:0.5}],tip:'paper-clip bumper: it bounces hard. drag back from the ball to putt'},
{par:3,title:'the eraser',g:[[[140,120],[520,120],[520,450],[330,450],[330,740],[140,740]]],start:[235,690],cup:[460,180],blocks:[{poly:rect(300,250,420,310),e:0.3}],tip:'the eraser block soaks up bounces'},
{par:3,title:'the slope',g:[rect(140,120,520,740)],start:[330,690],cup:[330,180],slopes:[{poly:rect(140,300,520,520),fx:380,fy:0}],tip:'pencil shading = a slope. the ball drifts downhill'},
{par:3,title:'the ink blot',g:[rect(140,120,520,740)],start:[330,690],cup:[330,170],water:[blob(330,430,85,14,7)],tip:'ink blot = water. one stroke penalty and back you go'},
{par:3,title:'tape bridge',g:[rect(140,120,520,740)],start:[330,690],cup:[330,170],water:[rect(140,360,520,480)],tapes:[rect(300,352,362,488)],tip:'the sticky tape is the only safe way across'},
{par:3,title:'ruler gate',g:[rect(140,120,520,740)],start:[330,690],cup:[330,170],rulers:[{y:420,w:230,h:26,x0:140,x1:290,spd:95}],tip:'the ruler slides back and forth. time your putt'},
{par:3,title:'staple jump',g:[rect(140,120,520,740)],start:[330,700],cup:[330,170],blocks:[{poly:rect(140,430,520,446),e:0.7,inner:true}],ramps:[{x1:250,y1:520,x2:410,y2:570,dx:0,dy:-1}],tip:'hit the staple ramp fast to jump the wall'},
{par:3,title:'spiral tunnel',g:[[[140,470],[520,470],[520,740],[140,740],[140,610],[96,610],[96,560],[140,560]],[[140,120],[520,120],[520,390],[140,390],[140,270],[96,270],[96,220],[140,220]]],start:[330,690],cup:[460,170],portals:[{x:110,y:585,r:15,ex:165,ey:245,dx:1,dy:0},{x:110,y:245,r:15,ex:165,ey:585,dx:1,dy:0}],tip:'roll into the tunnel through the spiral binding'},
{par:4,title:'the big mix',g:[rect(140,120,520,740)],start:[330,715],cup:[330,150],bumpers:[{x:250,y:600,r:26,a:-0.4}],slopes:[{poly:rect(140,400,520,500),fx:-300,fy:0}],rulers:[{y:280,w:220,h:24,x0:140,x1:300,spd:110}],water:[blob(440,590,55,12,99)],tip:'everything at once. good luck!'}
];

// ---------- state ----------
const G={hole:0,strokes:0,scores:[],busy:false,over:false,time:0,next:0,picked:false};
let cur=null,segs=[];
const ball={x:0,y:0,vx:0,vy:0,z:0,vz:0,moving:false,sunk:false,sinkT:0,lipT:0,pcd:0,inCup:false,spin:0,bounces:0,lastX:0,lastY:0};
let parts=[],notes=[],cele=null;
const drag={on:false,id:null,x:0,y:0};
const demo={on:location.hash==='#demo',t:0,phase:'idle',pt:0,px:0,py:0,tx:0,ty:0,shots:0,press:false};
let muted=false;
try{muted=localStorage.getItem('nmg_mute')==='1'}catch(e){}

// ---------- audio ----------
let AC=null;
function initAudio(){if(AC)return;try{AC=new (window.AudioContext||window.webkitAudioContext)()}catch(e){AC=null}}
function tone(f,d,type,g,slide,delay){if(!AC||muted)return;try{const t0=AC.currentTime+(delay||0);const o=AC.createOscillator(),v=AC.createGain();o.type=type||'sine';o.frequency.setValueAtTime(f,t0);if(slide)o.frequency.exponentialRampToValueAtTime(slide,t0+d);v.gain.setValueAtTime(g||0.15,t0);v.gain.exponentialRampToValueAtTime(0.001,t0+d);o.connect(v).connect(AC.destination);o.start(t0);o.stop(t0+d+0.02)}catch(e){}}
function noise(d,g,f){if(!AC||muted)return;try{const n=Math.floor(AC.sampleRate*d);const b=AC.createBuffer(1,n,AC.sampleRate);const ch=b.getChannelData(0);for(let i=0;i<n;i++)ch[i]=(Math.random()*2-1)*(1-i/n);const s=AC.createBufferSource();s.buffer=b;const fl=AC.createBiquadFilter();fl.type='bandpass';fl.frequency.value=f;fl.Q.value=0.8;const v=AC.createGain();v.gain.value=g;s.connect(fl).connect(v).connect(AC.destination);s.start()}catch(e){}}
const sndT={};
function snd(k){const now=G.time;if(sndT[k]&&now-sndT[k]<0.06)return;sndT[k]=now;
 switch(k){case'putt':noise(0.07,0.3,1500);tone(170,0.08,'triangle',0.12);break;
 case'wall':tone(150,0.07,'square',0.06);noise(0.04,0.12,900);break;
 case'eraser':tone(90,0.1,'sine',0.1);break;
 case'bump':tone(620,0.2,'sine',0.18,220);break;
 case'cup':tone(500,0.12,'sine',0.2,180);tone(523,0.12,'triangle',0.12,0,0.15);tone(659,0.12,'triangle',0.12,0,0.27);tone(784,0.12,'triangle',0.12,0,0.39);tone(1047,0.3,'triangle',0.14,0,0.51);break;
 case'splash':noise(0.35,0.35,500);tone(220,0.3,'sine',0.1,60);break;
 case'lip':tone(330,0.25,'sawtooth',0.07,130);break;
 case'jump':tone(220,0.22,'sine',0.14,760);break;
 case'land':tone(120,0.06,'triangle',0.12);break;
 case'portal':tone(300,0.3,'sine',0.12,900);noise(0.2,0.1,600);break;
 case'yay':tone(880,0.1,'square',0.05);tone(1175,0.15,'square',0.05,0,0.1);break;}}

// ---------- drawing helpers ----------
function wpath(c,pts,closed,amp){amp=amp||1.3;c.beginPath();const n=pts.length,m=closed?n:n-1;for(let i=0;i<m;i++){const a=pts[i],b=pts[(i+1)%n];const dx=b[0]-a[0],dy=b[1]-a[1];const L=Math.hypot(dx,dy)||1;const k=Math.max(1,Math.round(L/14));const nx=-dy/L,ny=dx/L;if(i===0)c.moveTo(a[0]+(R()-.5)*amp,a[1]+(R()-.5)*amp);for(let j=1;j<=k;j++){const t=j/k;const w=(j===k&&!closed)?0:(R()-.5)*2*amp;c.lineTo(a[0]+dx*t+nx*w,a[1]+dy*t+ny*w)}}if(closed)c.closePath()}
function pen(c,pts,closed,col,lw){c.strokeStyle=col||INK;c.lineWidth=lw||2;c.lineCap='round';c.lineJoin='round';wpath(c,pts,closed);c.stroke();if(quality>1){c.globalAlpha=.3;wpath(c,pts,closed,2.2);c.stroke();c.globalAlpha=1}}
function wcircle(cx,cy,r,n){n=n||24;const p=[];for(let i=0;i<n;i++){const a=i/n*Math.PI*2;p.push([cx+Math.cos(a)*r,cy+Math.sin(a)*r])}return p}
function hl(c,pts,col){c.fillStyle=col;c.globalCompositeOperation='multiply';wpath(c,pts,true,4);c.fill();c.globalCompositeOperation='source-over'}
function polyPath(c,p){c.beginPath();c.moveTo(p[0][0],p[0][1]);for(let i=1;i<p.length;i++)c.lineTo(p[i][0],p[i][1]);c.closePath()}
function arrow(c,x,y,dx,dy,len,col){const ex=x+dx*len,ey=y+dy*len;c.strokeStyle=col||INK;c.lineWidth=2;c.lineCap='round';c.beginPath();c.moveTo(x,y);c.lineTo(ex,ey);c.moveTo(ex-dx*9-dy*7,ey-dy*9+dx*7);c.lineTo(ex,ey);c.lineTo(ex-dx*9+dy*7,ey-dy*9-dx*7);c.stroke()}

function drawClip(c,s){c.strokeStyle='#7d8590';c.lineWidth=3;c.lineCap='round';c.lineJoin='round';c.beginPath();c.moveTo(-10*s,14*s);c.lineTo(-10*s,-14*s);c.arc(0,-14*s,10*s,Math.PI,0);c.lineTo(10*s,20*s);c.arc(0,20*s,10*s,0,Math.PI);c.lineTo(-10*s,-4*s);c.arc(-5*s,-4*s,5*s,Math.PI,0);c.lineTo(0,12*s);c.stroke();c.strokeStyle='rgba(255,255,255,.6)';c.lineWidth=1;c.beginPath();c.moveTo(-9*s,12*s);c.lineTo(-9*s,-13*s);c.stroke()}

function drawRuler(c,r,x){const y=r.y;c.fillStyle='#f6d57c';c.fillRect(x,y,r.w,r.h);c.strokeStyle='#8a6a1f';c.lineWidth=1.5;c.strokeRect(x,y,r.w,r.h);c.strokeStyle='#5a4410';c.lineWidth=1;c.beginPath();for(let k=0;k<=r.w;k+=10){const t=(k%50===0)?10:(k%25===0?7:4);c.moveTo(x+k,y);c.lineTo(x+k,y+t)}c.stroke();c.fillStyle='#5a4410';c.font='9px '+FONT;c.textAlign='center';for(let k=50;k<r.w;k+=50)c.fillText(k/10,x+k,y+r.h-4);c.font='italic 10px '+FONT;c.textAlign='left';c.fillText('cm',x+4,y+r.h-4)}

function drawStatic(){
 rng=mulberry(777+G.hole*13);
 const g=bgx;
 g.setTransform(dpr,0,0,dpr,0,0);g.fillStyle='#fdfcf5';g.fillRect(0,0,sw,sh);
 g.setTransform(scale*dpr,0,0,scale*dpr,ox*dpr,oy*dpr);
 const vx0=-ox/scale,vy0=-oy/scale,vx1=(sw-ox)/scale,vy1=(sh-oy)/scale;
 // ruled lines
 g.strokeStyle='#b4cfe8';g.lineWidth=1;g.beginPath();for(let y=Math.floor(vy0/28)*28;y<vy1;y+=28){g.moveTo(vx0,y+0.5);g.lineTo(vx1,y+0.5)}g.stroke();
 g.strokeStyle='#eb9a9a';g.lineWidth=1.5;g.beginPath();g.moveTo(120,vy0);g.lineTo(120,vy1);g.stroke();
 // spiral binding
 for(let y=Math.floor(vy0/56)*56+22;y<vy1;y+=56){g.fillStyle='#ebe7da';g.strokeStyle='#b9b4a6';g.lineWidth=1.5;g.beginPath();g.ellipse(40,y,9,7,0,0,7);g.fill();g.stroke();g.strokeStyle='#858b96';g.lineWidth=3.5;g.beginPath();g.ellipse(22,y-10,24,15,-0.35,0.3,3.6);g.stroke();g.strokeStyle='rgba(255,255,255,.5)';g.lineWidth=1;g.beginPath();g.ellipse(22,y-10,24,15,-0.35,1.2,2.6);g.stroke()}
 const h=cur;
 // title
 g.fillStyle=INK;g.font='22px '+FONT;g.textAlign='left';g.fillText('hole '+(G.hole+1)+': '+h.title,140,104);g.textAlign='right';g.fillText('par '+h.par,520,104);
 g.strokeStyle='rgba(255,230,60,.55)';g.lineWidth=14;g.lineCap='round';g.beginPath();g.moveTo(138,98);g.lineTo(150+h.title.length*11+70,99);g.globalCompositeOperation='multiply';g.stroke();g.globalCompositeOperation='source-over';
 // greens
 for(const p of h.g){hl(g,p,'rgba(206,242,110,.9)');}
 // tunnel route (hole 8)
 if(h.portals){const A=h.portals[0],B=h.portals[1];g.setLineDash([6,6]);g.strokeStyle=PENCIL;g.lineWidth=2;g.beginPath();g.moveTo(A.x,A.y);g.lineTo(40,A.y);g.lineTo(40,B.y);g.lineTo(B.x,B.y);g.stroke();g.setLineDash([]);for(const p of h.portals){g.fillStyle='#2b2b33';g.beginPath();g.arc(p.x,p.y,p.r,0,7);g.fill();g.strokeStyle='rgba(255,255,255,.35)';g.lineWidth=1.5;g.beginPath();for(let a=0;a<12;a+=0.2){const rr=p.r*0.9*(1-a/12);g.lineTo(p.x+Math.cos(a)*rr,p.y+Math.sin(a)*rr)}g.stroke();arrow(g,p.ex-14,p.ey,p.dx,p.dy,26,PENCIL)}g.fillStyle=PENCIL;g.font='italic 12px '+FONT;g.textAlign='center';g.save();g.translate(28,(A.y+B.y)/2);g.rotate(-Math.PI/2);g.fillText('tunnel',0,0);g.restore()}
 for(const p of h.g)pen(g,p,true);
 // slopes
 for(const s of (h.slopes||[])){const [x1,y1,x2,y2]=bounds(s.poly);g.save();polyPath(g,s.poly);g.clip();const ux=Math.sign(s.fx),uy=Math.sign(s.fy);const gr=g.createLinearGradient(ux>0?x1:x2,uy>0?y1:y2,ux>0?x2:(ux<0?x1:x1),uy>0?y2:(uy<0?y1:y1));gr.addColorStop(0,'rgba(80,80,90,0)');gr.addColorStop(1,'rgba(80,80,90,.28)');g.fillStyle=gr;g.fillRect(x1,y1,x2-x1,y2-y1);g.strokeStyle='rgba(70,70,80,.3)';g.lineWidth=1.2;g.beginPath();for(let k=x1-(y2-y1);k<x2;k+=8){g.moveTo(k+(R()-.5)*2,y1);g.lineTo(k+(y2-y1)+(R()-.5)*2,y2)}g.stroke();g.restore();const cx=(x1+x2)/2,cy=(y1+y2)/2,L=Math.hypot(s.fx,s.fy);for(let i=-1;i<=1;i++)arrow(g,cx-s.fx/L*30+(-s.fy/L)*i*50,cy-s.fy/L*30+(s.fx/L)*i*50,s.fx/L,s.fy/L,60,INK);g.fillStyle=PENCIL;g.font='italic 13px '+FONT;g.textAlign='left';g.fillText('downhill',x1+8,y2-8)}
 // water
 for(const w of (h.water||[])){g.fillStyle='#17215f';wpath(g,w,true,3);g.fill();const [x1,y1,x2,y2]=bounds(w);const cx=(x1+x2)/2,cy=(y1+y2)/2;for(let i=0;i<14;i++){const a=R()*6.28,d=R()*0.35+1.02;const px=cx+Math.cos(a)*(x2-x1)/2*d,py=cy+Math.sin(a)*(y2-y1)/2*d;if(inPoly(px,py,h.g[0])&&!inPoly(px,py,w)){g.beginPath();g.arc(px,py,1+R()*2.5,0,7);g.fill()}}g.fillStyle='rgba(255,255,255,.18)';g.beginPath();g.ellipse(cx-(x2-x1)*0.15,cy-(y2-y1)*0.2,(x2-x1)*0.12,(y2-y1)*0.06,-0.4,0,7);g.fill()}
 // tapes
 for(const t of (h.tapes||[])){const x1=t[0][0],y1=t[0][1],x2=t[2][0],y2=t[2][1];g.fillStyle='rgba(255,226,110,.72)';g.beginPath();g.moveTo(x1,y1);for(let x=x1;x<x2;x+=6)g.lineTo(x+3,y1+R()*5);g.lineTo(x2,y1);g.lineTo(x2,y2);for(let x=x2;x>x1;x-=6)g.lineTo(x-3,y2-R()*5);g.lineTo(x1,y2);g.closePath();g.fill();g.strokeStyle='rgba(190,150,40,.6)';g.lineWidth=1;g.stroke();g.strokeStyle='rgba(255,255,255,.55)';g.lineWidth=2;g.beginPath();for(let y=y1+14;y<y2-6;y+=20){g.moveTo(x1+6,y);g.lineTo(x2-6,y+3)}g.stroke();g.fillStyle=PENCIL;g.font='italic 12px '+FONT;g.textAlign='center';g.save();g.translate(x2+12,(y1+y2)/2);g.rotate(Math.PI/2);g.fillText('sticky tape',0,0);g.restore()}
 // blocks
 for(const b of (h.blocks||[])){const [x1,y1,x2,y2]=bounds(b.poly);if(b.e<0.5){g.fillStyle='#f4a7b9';wpath(g,b.poly,true,2);g.fill();g.fillStyle='#c96f8f';g.fillRect(x1,y1,(x2-x1)*0.28,y2-y1);g.fillStyle='rgba(255,255,255,.35)';g.fillRect(x1,y1,x2-x1,5);pen(g,b.poly,true);g.fillStyle='#7a2d4a';g.font='bold 12px '+FONT;g.textAlign='center';g.fillText('ERASER',(x1+x2)/2+(x2-x1)*0.14,(y1+y2)/2+4)}else{g.save();polyPath(g,b.poly);g.clip();g.fillStyle='rgba(120,120,130,.25)';g.fillRect(x1,y1,x2-x1,y2-y1);g.strokeStyle='rgba(60,60,70,.5)';g.lineWidth=1;g.beginPath();for(let k=x1-(y2-y1);k<x2;k+=6){g.moveTo(k,y1);g.lineTo(k+(y2-y1),y2)}g.stroke();g.restore();pen(g,b.poly,true)}}
 // ramps
 for(const r of (h.ramps||[])){g.fillStyle='rgba(160,170,180,.35)';g.fillRect(r.x1,r.y1,r.x2-r.x1,r.y2-r.y1);g.strokeStyle='#6f7781';g.lineWidth=6;g.lineCap='round';g.beginPath();g.moveTo(r.x1+4,r.y1+14);g.lineTo(r.x1+4,r.y1);g.lineTo(r.x2-4,r.y1);g.lineTo(r.x2-4,r.y1+14);g.stroke();g.strokeStyle='rgba(255,255,255,.5)';g.lineWidth=1.5;g.beginPath();g.moveTo(r.x1+6,r.y1-1);g.lineTo(r.x2-6,r.y1-1);g.stroke();for(let i=0;i<3;i++){const cx=r.x1+(r.x2-r.x1)*(0.25+i*0.25);const cy=r.y2-10-(i%2)*4;g.strokeStyle=RED;g.lineWidth=2.5;g.beginPath();g.moveTo(cx-9,cy);g.lineTo(cx,cy-10);g.lineTo(cx+9,cy);g.moveTo(cx-9,cy+10);g.lineTo(cx,cy);g.lineTo(cx+9,cy+10);g.stroke()}g.fillStyle=RED;g.font='bold 14px '+FONT;g.textAlign='center';g.fillText('JUMP!',(r.x1+r.x2)/2,r.y2+18)}
 // bumpers
 for(const b of (h.bumpers||[])){hl(g,wcircle(b.x,b.y,b.r+4,18),'rgba(255,120,200,.5)');g.save();g.translate(b.x,b.y);g.rotate(b.a);drawClip(g,b.r/24);g.restore();g.fillStyle='#b23a8a';g.font='italic 11px '+FONT;g.textAlign='center';g.fillText('boing',b.x,b.y+b.r+16)}
 // cup + flag
 const c=h.cup;g.fillStyle=INK;g.beginPath();g.arc(c[0],c[1],CR,0,7);g.fill();g.strokeStyle='rgba(255,255,255,.18)';g.lineWidth=1;g.beginPath();for(let i=0;i<6;i++){g.moveTo(c[0]-8+i*3,c[1]-7);g.lineTo(c[0]-6+i*3,c[1]+7)}g.stroke();
 g.strokeStyle=INK;g.lineWidth=2;g.beginPath();g.moveTo(c[0]+2,c[1]);g.lineTo(c[0]+3,c[1]-42);g.stroke();g.fillStyle=RED;g.beginPath();g.moveTo(c[0]+3,c[1]-42);g.lineTo(c[0]+28,c[1]-33);g.lineTo(c[0]+3,c[1]-24);g.closePath();g.fill();g.fillStyle='#fff';g.font='bold 10px '+FONT;g.textAlign='center';g.fillText(G.hole+1,c[0]+12,c[1]-30);
 // tee
 g.strokeStyle=PENCIL;g.lineWidth=1.2;g.setLineDash([2,3]);g.beginPath();g.arc(h.start[0],h.start[1],12,0,7);g.stroke();g.setLineDash([]);
 // tip
 g.fillStyle=PENCIL;g.font='italic 14px '+FONT;g.textAlign='left';g.fillText(h.tip,140,768);
 g.textAlign='left';
}

// ---------- game setup ----------
function buildSegs(){segs=[];const add=(p,e,outer,inner)=>{for(let i=0;i<p.length;i++){const a=p[i],b=p[(i+1)%p.length];segs.push({x1:a[0],y1:a[1],x2:b[0],y2:b[1],e:e,outer:outer,inner:inner})}};for(const p of cur.g)add(p,0.72,true,false);for(const b of (cur.blocks||[]))add(b.poly,b.e,false,!!b.inner)}
function placeBall(x,y){ball.x=x;ball.y=y;ball.vx=ball.vy=0;ball.z=ball.vz=0;ball.moving=false;ball.sunk=false;ball.sinkT=0;ball.inCup=false;ball.lipT=0}
function loadHole(i){G.hole=i;cur=HOLES[i];G.strokes=0;G.busy=false;G.next=0;G.picked=false;placeBall(cur.start[0],cur.start[1]);ball.lastX=ball.x;ball.lastY=ball.y;notes=[];parts=[];cele=null;demo.shots=0;demo.phase='idle';buildSegs();drawStatic();updHud()}
function resetGame(){G.scores=[];G.over=false;document.getElementById('card').classList.remove('on');loadHole(0)}
function updHud(){let tot=0;for(let i=0;i<G.hole;i++)tot+=G.scores[i]||0;tot+=G.strokes;document.getElementById('info').textContent='hole '+(G.hole+1)+'/9  ·  par '+cur.par+'  ·  strokes '+G.strokes+'  ·  total '+tot}
function note(text,col){if(notes.length>=3)notes.shift();notes.push({text:text,col:col||INK,t:0,life:3.2,y:ball.y,rot:(R()-.5)*0.12})}

function shoot(px,py){const dx=ball.x-px,dy=ball.y-py;const L=Math.hypot(dx,dy);if(L<12)return false;const pw=Math.min(L,MAXDRAG)/MAXDRAG;const v=MAXV*pw;ball.vx=dx/L*v;ball.vy=dy/L*v;ball.moving=true;ball.bounces=0;ball.lastX=ball.x;ball.lastY=ball.y;G.strokes++;snd('putt');updHud();if(pw>0.97)note('SMASH!!',RED);return true}

// ---------- physics ----------
function collideSeg(sg){const b=ball;const ax=sg.x1,ay=sg.y1,dx=sg.x2-ax,dy=sg.y2-ay;const L2=dx*dx+dy*dy||1;let t=((b.x-ax)*dx+(b.y-ay)*dy)/L2;t=Math.max(0,Math.min(1,t));const px=ax+dx*t,py=ay+dy*t;let nx=b.x-px,ny=b.y-py;const d=Math.hypot(nx,ny);if(d>=BR||d===0)return false;nx/=d;ny/=d;b.x=px+nx*BR;b.y=py+ny*BR;const vn=b.vx*nx+b.vy*ny;if(vn<0){b.vx-=(1+sg.e)*vn*nx;b.vy-=(1+sg.e)*vn*ny;if(-vn>60){snd(sg.e<0.5?'eraser':'wall');b.bounces++}}return true}
function rulerX(r){const span=r.x1-r.x0;const p=(G.time*r.spd)%(2*span);return r.x0+(p<span?p:2*span-p)}
function splash(){snd('splash');G.strokes++;note('splash! +1 :(',INK);for(let i=0;i<14;i++)parts.push({x:ball.x,y:ball.y,vx:(R()-.5)*300,vy:-R()*300-60,rot:0,vr:0,t:0,life:0.7,type:4,col:'#17215f'});placeBall(ball.lastX,ball.lastY);updHud();onStop()}
function sink(){ball.sunk=true;ball.sinkT=0;ball.moving=false;G.busy=true;snd('cup');snd('yay');G.scores[G.hole]=G.strokes;const d=G.strokes-cur.par;let txt='YAY!';if(G.strokes===1){txt='HOLE IN ONE!!!';note('HOLE IN ONE!!! ★★★',RED)}else if(d<0){txt='NICE!';note('under par!! nice one',RED)}else if(d===0){note('par. solid.',INK)}else if(d>=3){txt='finally...';note('oof. that was rough',PENCIL)}else{note('in! moving on',INK)}cele={t:0,txt:txt};const cols=[INK,RED,'#e6b800','#2a9d4a','#b23a8a'];const n=quality>1?34:18;for(let i=0;i<n;i++)parts.push({x:cur.cup[0],y:cur.cup[1],vx:(R()-.5)*520,vy:-R()*480-100,rot:R()*6,vr:(R()-.5)*9,t:0,life:1.2+R()*.7,type:i%4,col:cols[i%5]});G.next=2.1;updHud()}
function onStop(){const d=Math.hypot(ball.x-cur.cup[0],ball.y-cur.cup[1]);if(!ball.sunk&&G.strokes>0){if(d<45)note('sooo close!',RED);else if(ball.bounces>=4)note('pinball!!',INK);else if(G.strokes>=cur.par+3&&!G.picked)note('take your time...',PENCIL)}if(G.strokes>=10&&!ball.sunk&&!G.busy){G.picked=true;note('picking up. next!',PENCIL);G.scores[G.hole]=10;G.busy=true;G.next=1.4}}

function physics(dt){const b=ball;const N=4,h=dt/N;
 for(let s=0;s<N;s++){
  const air=b.z>0||b.vz>0;
  if(air){b.vz-=1500*h;b.z+=b.vz*h;if(b.z<=0){b.z=0;b.vz=0;snd('land')}}
  if(!air)for(const sl of (cur.slopes||[]))if(inPoly(b.x,b.y,sl.poly)){b.vx+=sl.fx*h;b.vy+=sl.fy*h}
  b.x+=b.vx*h;b.y+=b.vy*h;
  if(!air){const sp=Math.hypot(b.vx,b.vy);if(sp>0){const f=Math.max(0,sp-200*h)/sp*(1-0.6*h);b.vx*=f;b.vy*=f}}
  for(const sg of segs){if(air&&sg.inner)continue;collideSeg(sg)}
  for(const r of (cur.rulers||[])){const x=rulerX(r);const p=rect(x,r.y,x+r.w,r.y+r.h);for(let i=0;i<4;i++){const a=p[i],c=p[(i+1)%4];collideSeg({x1:a[0],y1:a[1],x2:c[0],y2:c[1],e:0.55})}if(b.x>x&&b.x<x+r.w&&b.y>r.y&&b.y<r.y+r.h){b.y=(b.y-r.y<r.h/2)?r.y-BR-1:r.y+r.h+BR+1}}
  if(air)continue;
  for(const bp of (cur.bumpers||[])){let nx=b.x-bp.x,ny=b.y-bp.y;const d=Math.hypot(nx,ny);if(d<bp.r+BR&&d>0){nx/=d;ny/=d;b.x=bp.x+nx*(bp.r+BR);b.y=bp.y+ny*(bp.r+BR);const vn=b.vx*nx+b.vy*ny;if(vn<0){b.vx-=2*vn*nx;b.vy-=2*vn*ny;b.vx+=nx*200;b.vy+=ny*200;snd('bump');b.bounces++;if(-vn>250)note('boing!!',RED)}}}
  for(const rp of (cur.ramps||[])){if(b.x>rp.x1&&b.x<rp.x2&&b.y>rp.y1&&b.y<rp.y2){const vd=b.vx*rp.dx+b.vy*rp.dy;if(vd>260){b.vz=Math.min(640,420+(vd-260)*0.5);b.z=0.01;snd('jump');note('wheee!',RED)}}}
  if(b.pcd<=0)for(const p of (cur.portals||[])){if(Math.hypot(b.x-p.x,b.y-p.y)<p.r+2){const sp=Math.max(Math.hypot(b.vx,b.vy),260);b.x=p.ex;b.y=p.ey;b.vx=p.dx*sp;b.vy=p.dy*sp;b.pcd=0.6;snd('portal');note('through the spiral!',INK)}}
  let wet=false;for(const w of (cur.water||[]))if(inPoly(b.x,b.y,w)){wet=true;break}
  if(wet)for(const t of (cur.tapes||[]))if(inPoly(b.x,b.y,t)){wet=false;break}
  if(wet){splash();return}
  const cx=cur.cup[0],cy=cur.cup[1];const dc=Math.hypot(b.x-cx,b.y-cy);const sp=Math.hypot(b.vx,b.vy);
  if(b.inCup){if(dc<6||sp<30){sink();return}b.vx+=(cx-b.x)/dc*1800*h;b.vy+=(cy-b.y)/dc*1800*h;b.vx*=(1-3*h);b.vy*=(1-3*h)}
  else if(dc<CR+1){if(sp<LIP){b.inCup=true}else if(b.lipT<=0){b.lipT=0.5;const ang=Math.atan2(b.vy,b.vx)+(R()<.5?-1:1)*0.75;const ns=sp*0.55;b.vx=Math.cos(ang)*ns;b.vy=Math.sin(ang)*ns;b.x=cx+(b.x-cx)/dc*(CR+2);b.y=cy+(b.y-cy)/dc*(CR+2);snd('lip');note('lipped out!! unlucky',RED)}}
  let inside=false;for(const p of cur.g)if(inPoly(b.x,b.y,p)){inside=true;break}
  if(!inside){placeBall(b.lastX,b.lastY);onStop();return}
 }
 if(!b.sunk){const sp=Math.hypot(b.vx,b.vy);if(sp<9&&b.z===0&&b.vz===0&&!b.inCup){b.vx=b.vy=0;b.moving=false;onStop()}}
}

// ---------- demo ----------
function demoTarget(){const c=cur;let tx=c.cup[0],ty=c.cup[1],pw=0;const hI=G.hole;
 if(hI===1&&ball.y>450){tx=235;ty=200}
 else if(hI===2){tx-=150}
 else if(hI===3){tx=ball.x<330?210:450;ty=ball.y>500?300:c.cup[1];if(ball.y<=300){tx=c.cup[0];ty=c.cup[1]}}
 else if(hI===4&&ball.y>480){tx=331;ty=250}
 else if(hI===6){if(ball.y>450){tx=ball.x;ty=ball.y-400;pw=1}}
 else if(hI===7&&ball.y>450){tx=112;ty=585;pw=0.6}
 else if(hI===8){if(ball.y>520){tx=330;ty=350;pw=0.8}else{tx+=10}}
 const dx=tx-ball.x,dy=ty-ball.y;const d=Math.hypot(dx,dy)||1;if(!pw)pw=Math.max(0.45,Math.min(1,d/620));const ang=Math.atan2(dy,dx)+(R()-.5)*0.08;demo.tx=ball.x-Math.cos(ang)*pw*MAXDRAG;demo.ty=ball.y-Math.sin(ang)*pw*MAXDRAG}
function updateDemo(dt){demo.t+=dt;if(demo.t>21){demo.t=0;resetGame();return}
 if(G.over){resetGame();return}
 if(demo.phase==='idle'){demo.press=false;drag.on=false;if(!ball.moving&&!G.busy){if(demo.shots>=2){G.scores[G.hole]=G.strokes;G.busy=true;G.next=0.3;return}demoTarget();demo.phase='move';demo.pt=0;demo.px=ball.x;demo.py=ball.y}else{demo.px+=(ball.x+30-demo.px)*0.1;demo.py+=(ball.y+30-demo.py)*0.1}}
 else if(demo.phase==='move'){demo.pt+=dt;const k=Math.min(1,demo.pt/0.9);const e=k<.5?2*k*k:1-Math.pow(-2*k+2,2)/2;demo.px=ball.x+(demo.tx-ball.x)*e;demo.py=ball.y+(demo.ty-ball.y)*e;demo.press=true;drag.on=true;drag.x=demo.px;drag.y=demo.py;if(k>=1){demo.phase='hold';demo.pt=0}}
 else if(demo.phase==='hold'){demo.pt+=dt;if(demo.pt>0.35){drag.on=false;demo.press=false;shoot(demo.px,demo.py);demo.shots++;demo.phase='idle'}}}
function endDemo(){if(!demo.on)return;demo.on=false;drag.on=false;demo.press=false;try{history.replaceState(null,'',location.pathname+location.search)}catch(e){}resetGame()}

// ---------- update & draw ----------
function advance(){if(G.hole<8)loadHole(G.hole+1);else showCard()}
function showCard(){G.over=true;let hp='',hs='',hr='',tp=0,ts=0;for(let i=0;i<9;i++){hp+='<td>'+HOLES[i].par+'</td>';const s=G.scores[i]||0;hs+='<td>'+s+'</td>';hr+='<th>'+(i+1)+'</th>';tp+=HOLES[i].par;ts+=s}document.getElementById('tbl').innerHTML='<table><tr><th>hole</th>'+hr+'<th>tot</th></tr><tr><td>par</td>'+hp+'<td>'+tp+'</td></tr><tr><td>you</td>'+hs+'<td><b>'+ts+'</b></td></tr></table>';const d=ts-tp;document.getElementById('sum').textContent=(d<0?'wow! '+d+' under par ★':d===0?'exactly par. neat.':'+'+d+' over par. practice makes perfect!');document.getElementById('card').classList.add('on')}

function update(dt){if(ball.lipT>0)ball.lipT-=dt;if(ball.pcd>0)ball.pcd-=dt;
 if(ball.moving&&!ball.sunk)physics(dt);
 if(ball.sunk)ball.sinkT+=dt;
 ball.spin+=Math.hypot(ball.vx,ball.vy)*dt/BR;
 for(const p of parts){p.t+=dt;p.vy+=520*dt;p.x+=p.vx*dt;p.y+=p.vy*dt;p.rot+=p.vr*dt}parts=parts.filter(p=>p.t<p.life);
 for(const n of notes)n.t+=dt;notes=notes.filter(n=>n.t<n.life);
 if(cele)cele.t+=dt;
 if(G.next>0){G.next-=dt;if(G.next<=0){G.next=0;advance()}}}

function drawParticle(g,p){const a=1-Math.max(0,(p.t-p.life*0.6)/(p.life*0.4));g.globalAlpha=a;g.save();g.translate(p.x,p.y);g.rotate(p.rot);g.strokeStyle=p.col;g.fillStyle=p.col;g.lineWidth=2;g.lineCap='round';g.beginPath();
 if(p.type===0){for(let i=0;i<10;i++){const r=i%2?4:9,an=i/10*Math.PI*2-Math.PI/2;g.lineTo(Math.cos(an)*r,Math.sin(an)*r)}g.closePath();g.fill()}
 else if(p.type===1){g.moveTo(0,7);g.bezierCurveTo(-11,-2,-6,-10,0,-4);g.bezierCurveTo(6,-10,11,-2,0,7);g.fill()}
 else if(p.type===2){g.moveTo(-10,0);for(let i=-10;i<=10;i+=2)g.lineTo(i,Math.sin(i*0.9)*4);g.stroke()}
 else if(p.type===3){g.moveTo(-7,0);g.lineTo(-2,5);g.lineTo(8,-6);g.stroke()}
 else{g.arc(0,0,2.5,0,7);g.fill()}
 g.restore();g.globalAlpha=1}

function draw(){const g=ctx;g.setTransform(dpr,0,0,dpr,0,0);g.drawImage(bg,0,0,sw,sh);g.setTransform(scale*dpr,0,0,scale*dpr,ox*dpr,oy*dpr);
 const b=ball;
 for(const r of (cur.rulers||[]))drawRuler(g,r,rulerX(r));
 // ball
 if(!b.sunk||b.sinkT<0.35){let bx=b.x,by=b.y,s=1+b.z/150;if(b.sunk){const k=Math.min(1,b.sinkT/0.35);bx=b.x+(cur.cup[0]-b.x)*k;by=b.y+(cur.cup[1]-b.y)*k;s=1-k}
  if(b.z>0){g.fillStyle='rgba(40,40,60,.22)';g.beginPath();g.ellipse(b.x+b.z*0.2,b.y+b.z*0.3,BR*1.1,BR*0.8,0,0,7);g.fill();by-=b.z*0.5}
  g.fillStyle='#fff';g.strokeStyle=INK;g.lineWidth=1.8;g.beginPath();g.arc(bx,by,BR*s,0,7);g.fill();g.stroke();g.strokeStyle='rgba(80,80,90,.45)';g.lineWidth=2;g.beginPath();g.arc(bx,by,Math.max(0.5,BR*s-2.6),0.4,1.9);g.stroke();g.fillStyle='rgba(80,80,90,.5)';g.beginPath();g.arc(bx+Math.cos(b.spin)*3*s,by+Math.sin(b.spin)*3*s,1.4*s,0,7);g.fill()}
 // aim
 if(drag.on&&!b.moving&&!G.busy){const dx=b.x-drag.x,dy=b.y-drag.y;const L=Math.hypot(dx,dy);if(L>=12){const pw=Math.min(L,MAXDRAG)/MAXDRAG,ux=dx/L,uy=dy/L;g.setLineDash([3,7]);g.strokeStyle=INK;g.lineWidth=2.2;g.lineCap='round';g.beginPath();g.moveTo(b.x+ux*BR,b.y+uy*BR);const len=30+pw*210;g.lineTo(b.x+ux*len,b.y+uy*len);g.stroke();g.setLineDash([]);arrow(g,b.x+ux*(len-12),b.y+uy*(len-12),ux,uy,12,INK);
  g.setLineDash([2,4]);g.strokeStyle='rgba(90,90,100,.6)';g.lineWidth=1.5;g.beginPath();g.moveTo(b.x,b.y);g.lineTo(b.x-ux*Math.min(L,MAXDRAG),b.y-uy*Math.min(L,MAXDRAG));g.stroke();g.setLineDash([]);
  g.strokeStyle='rgba(90,90,100,.7)';g.beginPath();g.arc(b.x-ux*Math.min(L,MAXDRAG),b.y-uy*Math.min(L,MAXDRAG),5,0,7);g.stroke();
  // power meter
  const mx=544,my=440,mh=260,mw=18;const col=pw<.5?'rgba(120,220,80,.75)':pw<.8?'rgba(255,200,40,.8)':'rgba(255,80,80,.8)';g.fillStyle=col;g.globalCompositeOperation='multiply';g.fillRect(mx,my+mh-mh*pw,mw,mh*pw);g.globalCompositeOperation='source-over';pen(g,rect(mx,my,mx+mw,my+mh),true,INK,1.8);g.fillStyle=INK;g.font='13px '+FONT;g.textAlign='center';g.fillText(Math.round(pw*100)+'%',mx+mw/2,my-8);g.save();g.translate(mx+mw+14,my+mh/2);g.rotate(Math.PI/2);g.fillText('power',0,0);g.restore()}}
 // particles
 for(const p of parts)drawParticle(g,p);
 // celebration text
 if(cele&&cele.t<2){const k=cele.t;const sc=1+Math.sin(Math.min(k,0.5)*Math.PI*2)*0.25;g.save();g.translate(cur.cup[0],cur.cup[1]-62);g.rotate(Math.sin(k*9)*0.08);g.scale(sc,sc);g.globalAlpha=Math.min(1,(2-k)*2);g.font='bold 30px '+FONT;g.textAlign='center';g.lineWidth=5;g.strokeStyle='#fffdf4';g.strokeText(cele.txt,0,0);g.fillStyle=RED;g.fillText(cele.txt,0,0);g.restore();g.globalAlpha=1;if(k<0.5){g.strokeStyle='#e6b800';g.lineWidth=2.5;g.beginPath();for(let i=0;i<10;i++){const an=i/10*Math.PI*2,r1=CR+6+k*60,r2=r1+14;g.moveTo(cur.cup[0]+Math.cos(an)*r1,cur.cup[1]+Math.sin(an)*r1);g.lineTo(cur.cup[0]+Math.cos(an)*r2,cur.cup[1]+Math.sin(an)*r2)}g.stroke()}}
 // margin notes
 g.font='bold 18px '+FONT;g.textAlign='center';for(const n of notes){const a=Math.min(1,n.t*4)*Math.min(1,(n.life-n.t)*1.5);const tw=g.measureText(n.text).width;const y=Math.max(150+tw/2,Math.min(740-tw/2,n.y));g.save();g.globalAlpha=a;g.translate(100,y);g.rotate(-Math.PI/2+n.rot);g.fillStyle=n.col;g.fillText(n.text,0,0);g.strokeStyle=n.col;g.lineWidth=1.2;g.beginPath();g.moveTo(-tw/2,6);for(let x=-tw/2;x<=tw/2;x+=6)g.lineTo(x,6+Math.sin(x*0.7)*1.5);g.stroke();g.restore()}
 g.globalAlpha=1;
 // demo pointer
 if(demo.on){g.save();g.translate(demo.px,demo.py);if(demo.press){g.strokeStyle='rgba(217,65,58,.6)';g.lineWidth=2;g.beginPath();g.arc(0,0,12,0,7);g.stroke()}g.fillStyle='#fff';g.strokeStyle=INK;g.lineWidth=2;g.lineJoin='round';g.beginPath();g.moveTo(0,0);g.lineTo(0,19);g.lineTo(5,14);g.lineTo(9,22);g.lineTo(12,20);g.lineTo(8,13);g.lineTo(14,13);g.closePath();g.fill();g.stroke();g.restore();g.fillStyle=INK;g.font='16px '+FONT;g.textAlign='center';g.fillText('demo mode — tap anywhere to play',330,800)}
}

// ---------- layout ----------
function resize(){sw=window.innerWidth;sh=window.innerHeight;dpr=Math.min(window.devicePixelRatio||1,quality>1?2:1);cv.width=Math.round(sw*dpr);cv.height=Math.round(sh*dpr);bg.width=cv.width;bg.height=cv.height;scale=Math.min(sw/W,(sh-8)/H);ox=(sw-W*scale)/2;oy=(sh-H*scale)/2;if(cur)drawStatic()}
window.addEventListener('resize',resize);
function toL(e){const r=cv.getBoundingClientRect();return{x:(e.clientX-r.left-ox)/scale,y:(e.clientY-r.top-oy)/scale}}

// ---------- input ----------
cv.addEventListener('pointerdown',e=>{e.preventDefault();initAudio();if(AC&&AC.state==='suspended'){try{AC.resume()}catch(x){}}if(demo.on){endDemo();return}if(G.over||G.busy||ball.moving||drag.on)return;drag.on=true;drag.id=e.pointerId;const p=toL(e);drag.x=p.x;drag.y=p.y;try{cv.setPointerCapture(e.pointerId)}catch(x){}});
cv.addEventListener('pointermove',e=>{if(drag.on&&!demo.on&&e.pointerId===drag.id){const p=toL(e);drag.x=p.x;drag.y=p.y}});
function up(e){if(drag.on&&!demo.on&&e.pointerId===drag.id){drag.on=false;if(!ball.moving&&!G.busy&&!G.over)shoot(drag.x,drag.y)}}
cv.addEventListener('pointerup',up);cv.addEventListener('pointercancel',up);
function setMute(m){muted=m;document.getElementById('bm').innerHTML=muted?'&#128263;':'&#128266;';try{localStorage.setItem('nmg_mute',muted?'1':'0')}catch(e){}}
document.getElementById('bm').addEventListener('click',()=>{initAudio();if(demo.on)endDemo();setMute(!muted)});
document.getElementById('br').addEventListener('click',()=>{initAudio();if(demo.on)endDemo();if(G.over)return;loadHole(G.hole)});
document.getElementById('again').addEventListener('click',()=>{initAudio();resetGame()});
window.addEventListener('keydown',e=>{if(demo.on){endDemo();return}const k=e.key.toLowerCase();if(k==='m')setMute(!muted);else if(k==='r'&&!G.over)loadHole(G.hole);else if(k==='enter'&&G.over)resetGame()});
setMute(muted);

// ---------- loop ----------
let last=performance.now(),ready=false,ft=0,fn=0;
function loop(now){const dt=Math.min(0.05,(now-last)/1000);last=now;G.time+=dt;ft+=dt;fn++;if(fn>=90){if(ft/fn>0.034&&quality>1){quality=1;resize()}ft=0;fn=0}
 if(demo.on)updateDemo(dt);update(dt);draw();
 if(!ready){ready=true;try{parent.postMessage({vcb:'ready'},'*')}catch(e){}}
 requestAnimationFrame(loop)}
resize();loadHole(0);requestAnimationFrame(loop);
</script>
</body>
</html>