SOURCE / PINNED RELEASE
Made of little things.
Rain pond
- Release
- 7e45a85ea2f5…
- 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>Rain pond</title>
<style>
html,body{height:100%;margin:0;overflow:hidden}body{background:#17333e}
canvas{display:block;width:100%;height:100%}
</style>
</head>
<body>
<canvas role="img" aria-label="Rain pond"></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=['Rain falling on a quiet surface','静かな水面に降る雨'];
function prepare(){
const colors=night?['#162f3b','#244e56','#102c39']:['#b7cfce','#7babad','#527e8c'];
let g=bg.createLinearGradient(w*.28,0,w*.57,h);g.addColorStop(0,colors[0]);g.addColorStop(.53,colors[1]);g.addColorStop(1,colors[2]);bg.fillStyle=g;bg.fillRect(0,0,w,h);
g=bg.createRadialGradient(w*.72,h*.17,0,w*.72,h*.17,Math.max(w,h)*.8);
g.addColorStop(0,night?'rgba(154,205,199,.18)':'rgba(241,252,239,.36)');g.addColorStop(1,'rgba(154,205,199,0)');bg.fillStyle=g;bg.fillRect(0,0,w,h);
}
function ring(x,y,r,depth,a){
ctx.beginPath();ctx.ellipse(x,y,r,r*(.28+.25*depth),0,0,TAU);
ctx.lineWidth=Math.max(.55,unit*.0015*(.65+depth));
ctx.strokeStyle=rgba(night?'196,236,230':'250,255,247',a);ctx.stroke();
}
function paint(){
ctx.drawImage(back,0,0);
const density=10+26*flow;
for(let i=0;i<36;i++){
const weight=clamp(density-i);if(!weight)continue;
const duration=4.4+rand(i+13)*3.4,cycle=time/duration+rand(i+4),epoch=Math.floor(cycle),p=cycle-epoch;
const x=(.04+.92*rand(i*11+epoch*53+78))*w,depth=.08+.85*rand(i*7+epoch*31+49),y=depth*h;
const radius=unit*(.064+.12*depth)*(.75+.5*rand(i+21));
if(p<.115){
const f=p/.115,dy=unit*(.04+.10*depth)*(1-f);
const g=ctx.createLinearGradient(x-2,y-dy-unit*.036,x,y-dy);g.addColorStop(0,'rgba(223,245,243,0)');g.addColorStop(1,rgba('235,252,248',.52*weight*(.5+.5*f)));
ctx.strokeStyle=g;ctx.lineWidth=Math.max(.65,unit*.0018);ctx.beginPath();ctx.moveTo(x-2,y-dy-unit*.036);ctx.lineTo(x,y-dy);ctx.stroke();
}else{
const q=(p-.115)/.885,ease=Math.sqrt(q),a=weight*(night?.69:.83)*(1-q)**1.75*Math.min(1,q*20+.2);
ring(x,y,radius*(.025+.975*ease),depth,a);
if(q>.10)ring(x,y,radius*(.015+.72*Math.sqrt(q-.10)),depth,a*.41);
if(q<.07){ctx.fillStyle=rgba('245,255,252',weight*.6*(1-q/.07));ctx.beginPath();ctx.arc(x,y,Math.max(.7,unit*.003*depth),0,TAU);ctx.fill()}
}
}
}
start();
</script>
</body>
</html>
