Ghost v1.2 — SAND starts on first touch anywhere (Howler-style unlock), on by default
Browse files
sand.js
CHANGED
|
@@ -7,7 +7,7 @@ const bar=document.createElement('div');bar.id='sandbar';
|
|
| 7 |
bar.innerHTML=`<a href="/">🌍 Ghost</a><a href="/spidey.html">🕸️ Spidey</a><a href="/bgg.html">📊 Gantt</a><a href="/drop/">👻 Drop</a><button id="sandbtn">▶ SAND</button>`;
|
| 8 |
document.body.appendChild(bar);
|
| 9 |
const btn=document.getElementById('sandbtn');
|
| 10 |
-
let ctx,master,timer,step=0;
|
| 11 |
const BPM=96,S=60/BPM/2;
|
| 12 |
const hook=[62,0,69,66,0,64,62,0, 69,0,71,69,66,0,64,0, 62,0,69,66,0,64,66,0, 69,71,74,0,71,69,66,0];
|
| 13 |
const bass=[38,38,0,38,45,0,45,0, 43,43,0,43,40,0,40,0];
|
|
@@ -27,8 +27,14 @@ function tick(){const t=ctx.currentTime+.05;
|
|
| 27 |
if(step%8===0)note(step%32<16?74:78,t,S*6,'sine',.05);
|
| 28 |
step++}
|
| 29 |
function play(){if(!ctx){ctx=new (window.AudioContext||window.webkitAudioContext)();master=ctx.createGain();master.gain.value=.7;master.connect(ctx.destination);waves()}
|
| 30 |
-
ctx.resume();timer=setInterval(tick,S*1000);btn.textContent='❚❚ SAND';btn.classList.add('on');localStorage.sand='on'}
|
| 31 |
-
function stop(){clearInterval(timer);ctx&&ctx.suspend();btn.textContent='▶ SAND';btn.classList.remove('on');localStorage.sand='off'}
|
| 32 |
-
btn.onclick=
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
})();
|
|
|
|
| 7 |
bar.innerHTML=`<a href="/">🌍 Ghost</a><a href="/spidey.html">🕸️ Spidey</a><a href="/bgg.html">📊 Gantt</a><a href="/drop/">👻 Drop</a><button id="sandbtn">▶ SAND</button>`;
|
| 8 |
document.body.appendChild(bar);
|
| 9 |
const btn=document.getElementById('sandbtn');
|
| 10 |
+
let ctx,master,timer=null,step=0;
|
| 11 |
const BPM=96,S=60/BPM/2;
|
| 12 |
const hook=[62,0,69,66,0,64,62,0, 69,0,71,69,66,0,64,0, 62,0,69,66,0,64,66,0, 69,71,74,0,71,69,66,0];
|
| 13 |
const bass=[38,38,0,38,45,0,45,0, 43,43,0,43,40,0,40,0];
|
|
|
|
| 27 |
if(step%8===0)note(step%32<16?74:78,t,S*6,'sine',.05);
|
| 28 |
step++}
|
| 29 |
function play(){if(!ctx){ctx=new (window.AudioContext||window.webkitAudioContext)();master=ctx.createGain();master.gain.value=.7;master.connect(ctx.destination);waves()}
|
| 30 |
+
ctx.resume();if(!timer)timer=setInterval(tick,S*1000);btn.textContent='❚❚ SAND';btn.classList.add('on');localStorage.sand='on'}
|
| 31 |
+
function stop(){clearInterval(timer);timer=null;ctx&&ctx.suspend();btn.textContent='▶ SAND';btn.classList.remove('on');localStorage.sand='off'}
|
| 32 |
+
btn.onclick=e=>{e.stopPropagation();timer?stop():play()};
|
| 33 |
+
// on by default: try right away; if the browser holds it, the first touch/key/scroll anywhere starts it
|
| 34 |
+
if(localStorage.sand!=='off'){
|
| 35 |
+
try{play();if(ctx.state!=='running'){clearInterval(timer);timer=null;btn.textContent='▶ SAND';btn.classList.remove('on')}}catch(e){}
|
| 36 |
+
const unlock=()=>{if(localStorage.sand!=='off'&&!(timer&&ctx&&ctx.state==='running'))play();
|
| 37 |
+
['pointerdown','touchstart','keydown','scroll','wheel'].forEach(ev=>removeEventListener(ev,unlock,true))};
|
| 38 |
+
['pointerdown','touchstart','keydown','scroll','wheel'].forEach(ev=>addEventListener(ev,unlock,{capture:true,passive:true}));
|
| 39 |
+
}
|
| 40 |
})();
|