Pop-up card
A Christmas pop-up card in the browser. Lift the cover and a paper village stands up, piece by piece, at its own depth. Then find the caganer.
Open a pop-up card and a whole village stands up out of a flat sheet. Close it and everything lies back down without tearing. No glue moves, nothing slides. It’s just folds, in the right places.
I wanted that in a browser, with real depth you can turn, not a video of it. Grab the cover and lift it.
Bon Nadal
obre-la
Aixeca la tapa
Troba el caganer
Once it’s open, have a look around. Somewhere in the snow there’s a caganer, the little man every Catalan nativity scene hides doing his business. It’s tradition. I don’t make the rules. Click him.
How a pop-up actually works
A real pop-up isn’t one sculpture. It’s a stack of flat strips, each one glued to the base at some distance from the spine. When the cover opens, the base pulls every strip upright. When it closes, they all fold flat again. The depth is just how far from the spine each strip is glued.
That’s the whole trick in CSS too. Each piece is a plane hinged on its bottom edge, pushed out from the fold with translateZ, and rotated from lying down to standing up:
// o goes 0 → 1 as the cover opens; each piece starts a bit later
const k = clamp((o - piece.lag) / (1 - piece.lag), 0, 1);
el.style.transform =
`translateZ(${piece.depth * cardWidth}px) rotateX(${(k - 1) * 90}deg)`;
The rule I broke on the first try: a pop-up has to close flat. A piece’s depth plus its height has to fit inside the base, or it pokes out of the shut card like a bookmark. Every piece here is sized to that limit.
Paper, not pixels
My first version was flat coloured shapes and it looked like a slide deck. Paper needs fibres (one feTurbulence noise tile, multiplied over every sheet), thickness (a couple of stacked box-shadows on the front edge) and a cut edge. The cut edge is the fun one, a gentle wave plus a tiny bit of jitter, like scissors in a hand that’s a little tired:
function ridge(width, y, amp, freq, jitter) {
const points = [];
for (let x = 0; x <= width; x += 6) {
const wave = Math.sin(x * freq + p1) * amp + Math.sin(x * freq * 2.3 + p2) * amp * 0.35;
points.push([x, y + wave + random(-jitter, jitter)]);
}
return points;
}
The randomness is seeded, so the village is cut the same way every time you load it.
Every piece is its own card
The hills, the houses, the church, the two firs, the snow, the tió and the caganer are nine separate pieces, each at its own depth. They’re drawn as one SVG illustration, then each piece crops its own part of it with the viewBox, so there’s only one drawing to maintain:
svg.setAttribute('viewBox', `${x0} ${y0} ${x1 - x0} ${y1 - y0}`);
Far pieces get a touch less brightness and saturation, like a painted landscape, so the hills already look far away. Move your pointer once it’s open and the pieces slide past each other.
The cover is a spring
Dragging maps straight to how open the card is. Letting go hands it to a spring, and the speed of your flick decides which way it goes:
// let go: a flick wins, otherwise whichever side of halfway you're on
target = velocity > 0.3 ? 1 : velocity < -0.3 ? 0 : (open > 0.45 ? 1 : 0);
// every frame
openV += (target - open) * 0.075;
openV *= 0.84;
open += openV; // overshoots a touch past open, then settles
That little overshoot is what makes it feel like card stock and not a hinge.
Small lives
Once it’s fully open the card wakes up. The stars twinkle, the windows flicker like there’s a candle behind them, the chimneys smoke and the tió blinks. They’re all plain CSS animations, paused until the card adds a class:
.tw, .win, .smoke, .blink { animation-play-state: paused; }
.lit :is(.tw, .win, .smoke, .blink) { animation-play-state: running; }
/* candlelight: steps(1) makes it jump, not fade */
.win { animation: flicker 2.4s steps(1) infinite; }
The caganer was the fiddliest bit. Safari’s hit-testing inside a 3D transform is unreliable, and on a phone he’s tiny, so I don’t trust the DOM to tell me you clicked him. I check his box myself, with a generous margin:
const box = caganer.getBoundingClientRect(), pad = Math.max(14, box.width * 0.25);
if (e.clientX > box.left - pad && e.clientX < box.right + pad &&
e.clientY > box.top - pad && e.clientY < box.bottom + pad) blush();
With reduced motion on, the card opens without the overshoot, nothing twinkles, smokes or snows, and the parallax stays still. Tab to it and Space opens it, and you can tab to the caganer too. He’ll still blush.