The question
This site's homepage has an animated figureThe Tesseract Memory · The public siteHomepage figureOne animated figure. On a computer it changes pose as you pass each section. On a phone it breathes in place.See it in the The Tesseract Memory build made of small grains. The figure has a set of poses, and it changes from one to the next as you read down the page. How do I make it follow the sections you're actually passing, on a computer and on a phone?
What made it hard
The sections have different heights, and text wrapping changes those heights again. So I couldn’t split the total scroll distance into equal parts, one for each pose. The poses would land in the wrong sections.
What I chose
On a computer, the figure's code measures each section. It measures the heading, the bottom of the content and the anchor, the scroll position that marks the section. Then it maps the scroll position onto the poses using those measurements. The code only listens to scrolling. It never takes over the mouse wheel or makes the page wait for an animation. Scrolling back returns the figure to the matching pose.
A phone gets different behaviour. On screens narrower than 760 pixels, the first figure, called Reach, sits in the page with the text. It doesn't change pose as the phone scrolls. The figure is drawn into a hidden image, which the browser then shrinks to fit. That image keeps the full 1586 by 992 grid of the source artwork. A smaller image squeezed the grains together and made bands where they overlapped. A touch scatters the grains near it. The scatter clears when the browser takes over to scroll. Scrolling and pinch zoom still work.
When idle, the figure only breathes, on one 5.2-second cycle. It stops drawing new frames when it is paused, off screen or in a hidden tab. In two cases the page shows the source artwork instead. One is when reduced motion is turned on. The other is when the browser loses the ability to draw the figure.
How I know it works
Each behaviour above has its own check. Timeline tests cover how scrolling maps to poses. They include sections of unequal size, scrolling back and large jumps. Motion tests check that every sampled idle movement follows the same breathing phase. Browser checks try the phone layout. They cover its hidden image, touch and scrolling. They also cover pinch and pause. Last, they cover reduced motion and recovery after going off screen.