// 03 BUZZ WIRE — playable pointer/keyboard precision game. const gamePaths={easy:'M75 270 C200 270 245 270 370 270 S610 270 722 270',medium:'M75 278 C165 282 127 133 258 165 S375 377 481 293 S608 128 722 254',hard:'M75 295 C183 320 95 107 253 120 S370 397 462 304 S511 75 624 161 S665 324 722 238'}; function initBuzz(){ const svg=$('#game-svg'),guide=$('#game-guide'),wide=$('#game-wide'),progress=$('#game-completed'); let level='easy',running=false,dragging=false,position=0,misses=0,started=0,elapsed=0,pointerID=null,sound=false,audioCtx=null; let interval=null; const path=()=>guide; const total=()=>path().getTotalLength(); function xy(frac){return path().getPointAtLength(Math.max(0,Math.min(1,frac))*total());} function pointFromEvent(ev){const c=svg.createSVGPoint();c.x=ev.clientX;c.y=ev.clientY;return c.matrixTransform(svg.getScreenCTM().inverse());} function place(){const p=xy(position);for(const sel of ['#game-handle','#game-handle-glow']){$(sel).setAttribute('cx',p.x);$(sel).setAttribute('cy',p.y);}$('#game-progress').textContent=Math.round(position*100)+'% complete';const len=Math.max(.1,position*total());progress.setAttribute('d',guide.getAttribute('d'));progress.style.strokeDasharray=`${len} ${total()+1}`;} function setState(v){setText('#buzz-state',v);} function paintLevel(){ const d=gamePaths[level];guide.setAttribute('d',d);wide.setAttribute('d',d);progress.setAttribute('d',d); const origin=xy(0),end=xy(1); $('#game-origin').setAttribute('cx',origin.x);$('#game-origin').setAttribute('cy',origin.y); $('#game-finish').setAttribute('cx',end.x);$('#game-finish').setAttribute('cy',end.y); $('#game-star').setAttribute('x',end.x);$('#game-star').setAttribute('y',end.y); $('#game-level').textContent=String(['easy','medium','hard'].indexOf(level)+1)+' / 3';place(); } function stopTimer(){if(interval!==null){clearInterval(interval);interval=null;}} function updateTime(){if(running)elapsed=(performance.now()-started)/1000;setText('#game-time',elapsed.toFixed(1)+'s');} function start(){svg.scrollIntoView({block:'center',behavior:'instant'});stopTimer();running=true;dragging=false;position=0;misses=0;elapsed=0;started=performance.now();setText('#game-misses','0');setText('#game-result','—');setState('GRAB THE WHITE DOT TO BEGIN');place();updateTime();interval=setInterval(updateTime,100);} function finish(){running=false;dragging=false;stopTimer();updateTime();setText('#game-result',misses===0?'Perfect!':misses<=3?'Nice work!':'Finished');setState('CIRCUIT COMPLETE ✓');} function reset(){running=false;dragging=false;stopTimer();position=0;misses=0;elapsed=0;setText('#game-time','0.0s');setText('#game-misses','0');setText('#game-result','—');setState('CHOOSE LEVEL · PRESS START');place();} function penalty(){dragging=false;misses++;setText('#game-misses',misses);setState('TOUCH! GRAB THE DOT TO RESUME');if(sound){try{audioCtx ||=new (window.AudioContext||window.webkitAudioContext)();const osc=audioCtx.createOscillator(),gain=audioCtx.createGain();osc.type='square';osc.frequency.value=190;gain.gain.setValueAtTime(.025,audioCtx.currentTime);gain.gain.exponentialRampToValueAtTime(.001,audioCtx.currentTime+.13);osc.connect(gain).connect(audioCtx.destination);osc.start();osc.stop(audioCtx.currentTime+.14);}catch(_){}}} function pointerDown(e){if(!running)return;const p=pointFromEvent(e),h=xy(position);if(Math.hypot(p.x-h.x,p.y-h.y)>55)return;dragging=true;pointerID=e.pointerId;svg.setPointerCapture(e.pointerId);e.preventDefault();setState('STAY ON THE GUIDE');} function pointerMove(e){if(!running||!dragging||e.pointerId!==pointerID)return; const p=pointFromEvent(e),current=position;let best={dist:Infinity,pos:current}; // Look ahead a bounded distance, so visitors cannot teleport to the finish. for(let i=0;i<=35;i++){const frac=Math.min(1,current+i*.004);const q=xy(frac),dist=Math.hypot(q.x-p.x,q.y-p.y);if(disttolerance){penalty();return;} position=Math.max(position,best.pos);place();if(position>=.994)finish(); } function pointerUp(e){if(e.pointerId===pointerID){dragging=false;pointerID=null;}} svg.addEventListener('pointerdown',pointerDown);svg.addEventListener('pointermove',pointerMove); svg.addEventListener('pointerup',pointerUp);svg.addEventListener('pointercancel',pointerUp); // Accessible alternative: advance carefully by arrow key. svg.setAttribute('tabindex','0'); svg.addEventListener('keydown',e=>{if(!running)return;if(e.key==='ArrowRight'){position=Math.min(1,position+.027);place();if(position>=.994)finish();e.preventDefault();}}); $('#buzz-start').addEventListener('click',start);$('#buzz-reset').addEventListener('click',reset); $('#buzz-sound').addEventListener('click',e=>{sound=!sound;e.currentTarget.setAttribute('aria-pressed',String(sound));e.currentTarget.textContent=sound?'Sound on ◉':'Sound off ◌';}); $$('[data-level]').forEach(b=>b.addEventListener('click',()=>{level=b.dataset.level;$$('[data-level]').forEach(x=>x.setAttribute('aria-pressed',String(x===b)));reset();paintLevel();})); watch(()=>{stopTimer();audioCtx?.close();});paintLevel(); }