// The one reveal used across the site: a frame settles out of a soft blur the
// first time it comes into view, and stays settled.
//
// This lived twice — once in ProjectPage.jsx and once in GalleryPage.jsx, with
// different thresholds and different properties animated. Both were plain
// globals, so whichever script loaded last silently won and the gallery ran an
// animation its own file did not describe. One definition, one behaviour.
function BlurIn({ children, delay = 0 }) {
const ref = React.useRef(null);
const [visible, setVisible] = React.useState(false);
React.useEffect(() => {
const el = ref.current;
const obs = new IntersectionObserver(
([entry]) => { if (entry.isIntersecting) { setVisible(true); obs.disconnect(); } },
{ threshold: 0.15 },
);
if (el) obs.observe(el);
return () => obs.disconnect();
}, []);
return (
{children}
);
}
window.BlurIn = BlurIn;
// The plate tilt: a frame leans a few degrees towards the pointer and settles
// back when it leaves. Used by the hung frames in a gallery room and by the
// room cards on the gallery index, from here so the two cannot drift apart.
//
// Skipped on a phone, where there is no pointer to follow and it would fight
// the swipe, and for anyone who has asked their system for less motion.
const TILT_DEGREES = 5;
const TILT_REST = "perspective(900px) rotateY(0deg) rotateX(0deg) translateZ(0px)";
function useTilt() {
const isMobile = window.useIsMobile();
const ref = React.useRef(null);
const still = () =>
isMobile || window.matchMedia("(prefers-reduced-motion: reduce)").matches;
const onMouseMove = (e) => {
const el = ref.current;
if (!el || still()) return;
// will-change asks the browser to keep this element on a compositor layer
// of its own. Set here rather than in the style, so a room of 89 frames
// holds one such layer while the pointer is on it, not 89 of them at rest.
el.style.willChange = "transform";
const r = el.getBoundingClientRect();
const px = (e.clientX - r.left) / r.width - 0.5;
const py = (e.clientY - r.top) / r.height - 0.5;
el.style.transform =
`perspective(900px) rotateY(${px * TILT_DEGREES}deg) rotateX(${-py * TILT_DEGREES}deg) translateZ(10px)`;
};
const onMouseLeave = () => {
const el = ref.current;
if (!el) return;
el.style.transform = TILT_REST;
// Cleared once the frame has settled back, so the layer is released.
setTimeout(() => { if (ref.current === el) el.style.willChange = ""; }, 450);
};
return { ref, onMouseMove, onMouseLeave, isMobile };
}
// The slow drift on the hero images runs for as long as the page is open, even
// once the hero is tens of thousands of pixels above the fold — measured still
// "running" 29,000px out of view. Paused while off screen, so a long project
// page is not animating a picture nobody can see.
//
// Left running if anything here fails: the observer only ever pauses what it
// has seen leave, so the fallback is the site's previous behaviour.
(function pauseDriftOffScreen() {
if (typeof IntersectionObserver === "undefined") return;
const seen = new WeakSet();
const io = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
entry.target.style.animationPlayState = entry.isIntersecting ? "" : "paused";
});
},
{ rootMargin: "10% 0px" },
);
const sweep = () => {
document.querySelectorAll(".bv-drift").forEach((el) => {
if (seen.has(el)) return;
seen.add(el);
io.observe(el);
});
};
// React swaps these in and out on every route change, so the document is
// watched rather than swept once.
const start = () => {
sweep();
new MutationObserver(sweep).observe(document.body, { childList: true, subtree: true });
};
if (document.body) start();
else document.addEventListener("DOMContentLoaded", start);
})();
window.useTilt = useTilt;