SOURCE / PINNED RELEASE
Made of little things.
Firefly garden
- Release
- a0722556444c…
- Author-recorded commit
- Not provided
- License
- Check the original source
- Author’s source link
- Open external source ↗May change independently of this release
The hash-verified playable HTML, which may contain bundled or minified code. This view does not execute it.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Firefly garden</title>
<style>
html,body{height:100%;margin:0;overflow:hidden}body{background:#10292e}
canvas{display:block;width:100%;height:100%}
</style>
</head>
<body>
<canvas role="img" aria-label="Firefly garden"></canvas>
<script>
const canvas=document.querySelector('canvas'),ctx=canvas.getContext('2d',{alpha:false});
const back=document.createElement('canvas'),bg=back.getContext('2d',{alpha:false});
const reduced=matchMedia('(prefers-reduced-motion: reduce)'),dark=matchMedia('(prefers-color-scheme: dark)');
const TAU=Math.PI*2,clamp=(x,a=0,b=1)=>Math.max(a,Math.min(b,x));
const rand=n=>{const x=Math.sin(n*127.1+32.4)*43758.545;return x-Math.floor(x)};
const rgba=(rgb,a)=>'rgba('+rgb+','+clamp(a)+')';
let night=dark.matches,themeBg='',w=1,h=1,unit=1,time=0,last=0,raf=0,inView=false,parked=false;
let flow=0,target=0,checked=0,retryAt=0,handle;
const recent=new Map(); // At most 100 public event IDs and timestamps, never note content.
function visible(){return !parked&&inView&&!document.hidden&&canvas.clientWidth>0&&canvas.clientHeight>0}
function updateTarget(){
const now=Date.now()/1000;
for(const [id,at] of recent)if(at<now-60)recent.delete(id);
target=1-Math.exp(-recent.size/18);
}
function watch(){
if(handle||!visible()||reduced.matches||performance.now()<retryAt||!window.napplet?.relay?.subscribe)return;
try{
const sub=window.napplet.relay.subscribe({kinds:[1],since:Math.floor(Date.now()/1000)-60,limit:100});
handle=sub;
sub.onEvent(({event}={})=>{
if(handle!==sub||!visible()||!event||event.kind!==1||typeof event.id!=='string'||!/^[a-f0-9]{64}$/.test(event.id))return;
const now=Date.now()/1000,at=event.created_at;
if(!Number.isInteger(at)||at<now-60||at>now+5||recent.has(event.id))return;
recent.set(event.id,at);
if(recent.size>100){let oldest;for(const [id,t] of recent)if(!oldest||t<oldest[1])oldest=[id,t];recent.delete(oldest[0])}
});
sub.onClosed(()=>{if(handle!==sub)return;handle=undefined;recent.clear();target=0;retryAt=performance.now()+30000});
}catch{stopWatch();retryAt=performance.now()+30000}
}
function stopWatch(){
const sub=handle;handle=undefined;sub?.close();recent.clear();target=0;checked=0;retryAt=0;
}
function theme(t){
const color=t?.colors?.background;
if(/^#[0-9a-f]{6}$/i.test(color||'')){themeBg=color;const v=[1,3,5].map(i=>parseInt(color.slice(i,i+2),16));night=v[0]*.2126+v[1]*.7152+v[2]*.0722<120}
else if(!themeBg)night=dark.matches;
const lang=t?.lang||navigator.language;canvas.setAttribute('aria-label',lang.startsWith('ja')?label[1]:label[0]);
prepare();invalidate();
}
function resize(){
const r=canvas.getBoundingClientRect();if(!r.width||!r.height)return;
const scale=Math.min(devicePixelRatio||1,1.5,Math.sqrt(450000/(r.width*r.height)));
w=Math.max(1,Math.floor(r.width*scale));h=Math.max(1,Math.floor(r.height*scale));unit=Math.min(w,h);
canvas.width=w;canvas.height=h;back.width=w;back.height=h;prepare();invalidate();
}
function stop(){cancelAnimationFrame(raf);raf=0;last=0;stopWatch()}
function frame(now){
raf=0;if(!visible())return;
if(reduced.matches){paint();return}
// Draw at most ~30 fps; keep time continuous and avoid a resume jump.
if(!last||now-last>=32){
const dt=last?Math.min(.15,(now-last)/1000):0;last=now;time+=dt;
if(now>=checked){checked=now+1000;watch();updateTarget()}
flow+=(target-flow)*(1-Math.exp(-dt/12));paint();
}
raf=requestAnimationFrame(frame);
}
function invalidate(){if(visible()&&!raf)raf=requestAnimationFrame(frame)}
function visibility(){stop();if(reduced.matches)flow=0;if(visible())invalidate()}
function start(){
new ResizeObserver(resize).observe(document.body);
new IntersectionObserver(([e])=>{const next=e.isIntersecting&&e.intersectionRatio>0;if(next!==inView){inView=next;visibility()}}).observe(canvas);
document.addEventListener('visibilitychange',visibility);
window.addEventListener('pagehide',()=>{parked=true;stop()});
window.addEventListener('pageshow',()=>{parked=false;visibility()});
reduced.addEventListener('change',visibility);
dark.addEventListener('change',()=>{if(!themeBg)theme({})});
window.napplet?.theme?.get().then(theme,()=>{});window.napplet?.theme?.onChanged(theme);
theme({});resize();
}
const label=['Fireflies beneath a softly blurred moon','ぼんやりとした月の下を舞う蛍'];
const halo=document.createElement('canvas');halo.width=halo.height=64;
const hc=halo.getContext('2d'),hg=hc.createRadialGradient(32,32,0,32,32,32);
hg.addColorStop(0,'rgba(255,254,214,1)');hg.addColorStop(.08,'rgba(246,255,185,.98)');hg.addColorStop(.17,'rgba(215,250,137,.44)');hg.addColorStop(.40,'rgba(179,233,112,.13)');hg.addColorStop(1,'rgba(170,230,112,0)');hc.fillStyle=hg;hc.fillRect(0,0,64,64);
function prepare(){
const stops=night?['#0b192e','#12343c','#0c262b']:['#344b62','#487477','#2b555b'];
let g=bg.createLinearGradient(w*.27,0,w*.67,h);g.addColorStop(0,stops[0]);g.addColorStop(.61,stops[1]);g.addColorStop(1,stops[2]);bg.fillStyle=g;bg.fillRect(0,0,w,h);
const x=w*.72,y=h*.24,r=unit*.103;
// A feathered disk and a wide atmospheric halo, with no surface modelling.
g=bg.createRadialGradient(x,y,0,x,y,r*3.2);g.addColorStop(0,'rgba(223,234,211,.22)');g.addColorStop(.34,'rgba(223,234,211,.11)');g.addColorStop(1,'rgba(223,234,211,0)');bg.fillStyle=g;bg.fillRect(0,0,w,h);
g=bg.createRadialGradient(x,y,0,x,y,r*1.25);
g.addColorStop(0,night?'rgba(248,243,215,.85)':'rgba(254,251,230,.80)');
g.addColorStop(.58,night?'rgba(241,239,215,.78)':'rgba(247,247,225,.73)');
g.addColorStop(.76,'rgba(238,241,218,.40)');g.addColorStop(1,'rgba(234,239,215,0)');bg.fillStyle=g;bg.fillRect(0,0,w,h);
}
function paint(){
ctx.drawImage(back,0,0);
const count=25+51*flow;
ctx.globalCompositeOperation='screen';
for(let i=0;i<76;i++){
const weight=clamp(count-i);if(!weight)continue;
const depth=.16+.84*rand(i+11),phase=rand(i+81)*TAU;
const x=w*(.04+.92*rand(i+17)+.037*Math.sin(time*(.13+.11*depth)+phase)+.014*Math.sin(time*.37+phase));
const y=h*(.16+.8*rand(i+219)+.032*Math.cos(time*(.10+.12*depth)+phase*1.4));
const pulse=(.5+.5*Math.sin(time*(.62+.49*rand(i+51))+phase))**2.3;
const radius=Math.max(3,unit*(.012+.029*depth));
ctx.globalAlpha=weight*(.13+.78*pulse)*(.5+.5*depth);
ctx.drawImage(halo,x-radius,y-radius,radius*2,radius*2);
}
ctx.globalAlpha=1;ctx.globalCompositeOperation='source-over';
}
start();
</script>
</body>
</html>
