// A layered vector illustration; no physics engine or independent animation loop.
export const HOURGLASS_CYCLE_SECONDS = 28;
let instance = 0;
export function createHourglass(items) {
const item = items.find(item => item.image.dataset.heroAsset === "chapter_computational_complexity");
if (!item) return null;
const id = `hero-sand-${++instance}`;
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.setAttribute("viewBox", "0 0 113 178");
svg.setAttribute("aria-hidden", "true");
svg.classList.add("hero-hourglass");
svg.style.cssText = item.image.style.cssText;
svg.innerHTML = `
${Array.from({length:8},(_,i)=>``).join("")}
`;
item.image.insertAdjacentElement("afterend", svg);
const body = svg.querySelector(".hourglass-body");
const sand = svg.querySelector(".hourglass-sand");
const upper = svg.querySelector(".hourglass-upper");
const surface = svg.querySelector(".hourglass-surface");
const lower = svg.querySelector(".hourglass-lower");
const shade = svg.querySelector(".hourglass-pile-shade");
const stream = svg.querySelector(".hourglass-stream");
const thread = svg.querySelector(".hourglass-thread");
const grains = [...svg.querySelectorAll(".hourglass-grain")];
const smooth = x => x*x*(3-2*x);
return {
render(time, enabled) {
svg.style.display = enabled ? "" : "none";
item.image.classList.toggle("hero-replaced", enabled);
if (!enabled) return;
// Slow the entire choreography, including falling grains and the turn.
time *= 14 / HOURGLASS_CYCLE_SECONDS;
const phase = time % 14;
const turns = Math.floor(time / 14);
const progress = Math.min(phase / 11, 1);
const flip = smooth(Math.min(1, Math.max(0, (phase-11.6)/1.6)));
// Keep turning in the same direction; the frame never snaps back at a loop boundary.
body.setAttribute("transform", `rotate(${((turns+flip)*180).toFixed(2)} 56.5 89)`);
sand.setAttribute("transform", `rotate(${turns*180} 56.5 89)`);
const level = 38 + 47*(1-Math.pow(1-progress, 0.45));
const notch = (1-progress)*5;
upper.setAttribute("d", `M8 ${level} Q39 ${level-1} 56.5 ${level+notch} Q75 ${level-1} 105 ${level} V89 H8Z`);
upper.setAttribute("opacity", progress>0.995?"0":"1");
surface.setAttribute("d", `M17 ${level} Q38 ${level-1} 56.5 ${level+notch} Q78 ${level-1} 97 ${level}`);
surface.setAttribute("opacity", progress>0.995?"0":".8");
const pile = Math.sqrt(progress);
// Sand settles toward the neck after inversion, before the next pour begins.
const settle = smooth(Math.max(0, Math.min(1, (phase-13.2)/0.8)));
const peak = 156-pile*43-settle*24;
const base = 160-settle*20;
const radius = pile*45+settle*5;
lower.setAttribute("d", `M${56.5-radius} ${base} Q40 ${peak+14} 56.5 ${peak} Q70 ${peak+14} ${56.5+radius} ${base} Q75 ${base+settle} 56.5 ${base-settle*5} Q39 ${base+settle} ${56.5-radius} ${base}Z`);
shade.setAttribute("d", `M56.5 ${peak} Q70 ${peak+14} ${56.5+radius} ${base} H68Z`);
stream.setAttribute("opacity", phase<10.95?".9":"0");
thread.setAttribute("d", `M56.5 84 Q${56.5+Math.sin(time*8)*0.5} 101 56.5 ${peak}`);
grains.forEach((grain,i)=>{
const travel=(time*1.5+i/8)%1;
grain.setAttribute("cx", (56.5+Math.sin(i*2.4+time*5)*0.75).toFixed(2));
grain.setAttribute("cy", (86+travel*(peak-86)).toFixed(2));
});
},
dispose() { svg.remove(); item.image.classList.remove("hero-replaced"); },
};
}