@fluritheme/fluri
Version:
Interactive snowfall for websites: DOM-aware snow accumulation, mouse interactions, snow physics, Canvas 2D rendering, and Christmas effects.
34 lines • 1.42 kB
JavaScript
/** Bound visual density by the visible scene, even if a preset requests thousands of flakes. */
export function particleBudget(requested, area, coarse, adaptive) {
if (!adaptive)
return requested;
const density = Math.max(80, Math.floor(Math.max(0, area) / 1500));
return Math.max(0, Math.min(requested, coarse ? 320 : 1000, density));
}
/** Respond promptly to sustained slow frames, recover slowly to avoid quality oscillation. */
export class FrameBudget {
slow = 0;
recovery = 0;
work = 0;
observe(work, elapsed, target, quality) {
this.work = this.work === 0 ? work : this.work * 0.8 + work * 0.2;
const pressure = work > target * 0.85 || this.work > target * 0.65 || elapsed > target * 1.65;
this.slow = pressure ? this.slow + 1 : Math.max(0, this.slow - 1);
if (work > target * 1.8 || this.slow >= 3) {
this.slow = 0;
this.recovery = 0;
this.work = work > target * 1.8 ? 0 : Math.min(work, target * 0.65);
return Math.max(0.12, quality * 0.72);
}
if (!pressure && this.work < target * 0.35)
this.recovery += Math.min(100, elapsed);
else
this.recovery = 0;
if (this.recovery >= 3000) {
this.recovery = 0;
return Math.min(1, quality + 0.05);
}
return quality;
}
}
//# sourceMappingURL=Budget.js.map