Building a 60-Weather Canvas 2D Focus App in Vanilla JS & Web Audio
Most productivity apps treat your nervous system like an assembly line: bright red countdowns, buzzing alarms that snap you out of hyperfocus, and broken-streak penalties that make you quit the first time life gets messy
Most productivity apps treat your nervous system like an assembly line: bright red countdowns, buzzing alarms that snap you out of hyperfocus, and broken-streak penalties that make you quit the first time life gets messy.
Over the past few months, my wife Jessica and I (while raising two kids after regular shifts) built Daysark β a calm, offline-first campfire focus sanctuary right in the browser with no signup wall, no ads, and no third-party trackers.
Instead of reaching for a heavy SPA bundle, we built the entire client in vanilla JavaScript, Canvas 2D, and Web Audio, backed by Cloudflare Workers, D1, and Durable Objects. Here are the three most interesting engineering challenges we solved along the way.
1. Eliminating AAC Loop Clicks in Web Audio (815 KB Total Real Recordings)
Early on, our Sound Console synthesized rain, fire crackle, and wind purely with filtered noise buffers. It worked offline with a tiny footprint, but real recorded crackling birch logs and a real Koshi bamboo chime feel completely different on headphones.
The catch: raw uncompressed WAV loops are 10β30 MB each, while compact .m4a (AAC-LC) files add 20β50 ms of encoder priming/trailing padding that causes an audible click if you naively set AudioBufferSourceNode.loop = true.
Our fix was two-part:
- Master 7 verified CC0 / Public Domain recordings down to compact stereo AAC-LC loops (
75 KB β 243 KBeach,815 KBtotal across rain, fireplace, brown noise, night wind, warm pad, rolling thunder, and chime) cached offline by our Service Worker. - Right after
ctx.decodeAudioData(), trim near-silent encoder padding and equal-power crossfade the final0.40sinto the opening0.40sinside a clean PCMAudioBuffer:
function createSeamlessLoopBuffer(audioCtx, decoded) {
const numCh = decoded.numberOfChannels || 1;
const sr = decoded.sampleRate || 44100;
const total = decoded.length;
const maxTrim = Math.min(Math.floor(sr * 0.12), Math.floor(total * 0.05));
const ch0 = decoded.getChannelData(0);
let start = 0;
while (start < maxTrim && Math.abs(ch0[start]) < 0.0008) start++;
let end = total - 1;
while (end > total - 1 - maxTrim && Math.abs(ch0[end]) < 0.0008) end--;
const trimmedLen = end - start + 1;
const fadeLen = Math.min(Math.floor(sr * 0.40), Math.floor(trimmedLen * 0.12));
const outLen = trimmedLen - fadeLen;
const outBuf = audioCtx.createBuffer(numCh, outLen, sr);
for (let c = 0; c < numCh; c++) {
const src = decoded.getChannelData(c);
const dst = outBuf.getChannelData(c);
for (let i = 0; i < fadeLen; i++) {
const p = i / fadeLen;
dst[i] = src[start + i] * Math.sin(p * Math.PI * 0.5)
+ src[start + outLen + i] * Math.cos(p * Math.PI * 0.5);
}
for (let i = fadeLen; i < outLen; i++) {
dst[i] = src[start + i];
}
}
return outBuf;
}
The result is sample-accurate, click-free looping across Chrome, Safari, iOS, and Firefox β AND we still route real recordings through live Web Audio biquad filters so stepping inside the Lantern Library Cabin smoothly low-passes the rain down to 820 Hz (genuine muffled rain on window glass) and opens it back up to 14,000 Hz outdoors.
2. A 60-Scene Living Weather Engine in Pure Canvas 2D
Daysark has 4 illustrated realms (Pine Forest Camp, Rainy Watchtower, Lantern Library Cabin, and Ark Deck) across 3 solar moods (Day, Dusk, Night) and 5 weather states (Clear, Cloudy, Fog, Rain, Snow).
On top of those plates, our Canvas 2D engine runs:
- Aerodynamic velocity-drag particles so raindrops and snowflakes bend coherently with gusting wind fields,
- Recursive fractal lightning (forked bolts, cloud crawls, horizon flashes) paired with distance-delayed rolling thunder,
-
Masked sprite lighting with a clean per-frame cleared scratch buffer (
+12pxsafety padding so GPU bilinear filtering never bleeds neighboring character edges during subtle breathing animations).
Here is a 60-second look at Living Skies and the audio engine in action:
3. Designing Around ADHD Focus (2-Minute Spark + Flow Overtime)
The most important architectural choice wasn't technical β it was behavioral:
- 2-Minute Spark: Clicking Light the Spark drops you into a calm 2-minute timer immediately (no account needed) to beat task-initiation paralysis.
-
Flow Overtime: When your timer hits
00:00, there is no harsh alarm. The clock turns gold and counts upward silently so hyperfocus is never interrupted. - No Broken Streaks: Taking rest days never wipes your progress or harms your 19 sheltered wildlife companions.
- Quiet Group Campfires: Built on Cloudflare Durable Objects (WebSocket Hibernation API) so up to 8 friends can focus side-by-side without webcams, mics, or chat noise.
You can try a 2-minute Spark right in your browser (no signup required) here: daysark.com.
Would love feedback from fellow web devs β especially on Canvas performance, Web Audio tricks, or Cloudflare Durable Objects patterns!
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.