Skip to content
Redon Emini

Hi, I'm Redon.

I'm a fullstack software engineer based in Pristina, Kosovo.
Currently a fullstack engineer and tech lead at DataCose.

Social Links:

Animations that are also images

How Highreel's story slides animate in plain CSS, and how one rule (every element rests at its final frame) gives reduced motion, saved images and the same Highreel every time for free.

The page a customer opens is the product. It's a set of story slides that work the way Instagram stories do: a progress bar along the top, tap the right side for the next slide and the left side to go back, press and hold to pause. Each kind of number gets its own scene. A count of active days fills in a year of tiny calendar squares. A streak lights up a chain, link by link. A rank shows a hundred dots, lights up the top few, and marks one of them "You're here". There are more than twenty scenes, and a business can pick which one each slide uses.

These pages have a tough audience. They're opened from a chat message, on a phone, often on mobile data, by someone who wasn't expecting them. So they have budgets, checked on a production build: at most 150 KB of gzipped JavaScript, the first slide on screen within 1.5 seconds on 4G, and 60 frames per second on a mid-range Android phone (simulated with a 4× CPU slowdown).

No animation library

The plan was to use Motion. While building the first scenes, I switched to plain CSS keyframes, and every scene since has followed the same constraints:

  • Only transform and opacity animate. Browsers can run those two on the compositor, a separate thread from your JavaScript, so the motion stays smooth even while the main thread is busy, for example while Vue hydrates the page.
  • The first slide plays from the server-rendered HTML. CSS animations start as soon as the HTML and CSS arrive. On a slow connection, the cover is already moving while the JavaScript is still downloading.
  • Pausing is one rule. Holding a finger on the story freezes every animation in the slide:
app/components/recap/recap.css
.recap.is-held .slide *,
.recap.is-held .slide *::before,
.recap.is-held .slide *::after {
  animation-play-state: paused !important;
}
  • It costs 0 KB of JavaScript.

The one thing CSS used to be bad at is springy motion, and the linear() easing function fixes that. Despite the name, it takes a list of points and draws a curve through them, so any easing you can write as a function can become CSS. A small helper samples a JavaScript function into one:

shared/utils/scenes.ts
export function cssEasing(f: (t: number) => number, points = 24): string {
  const values = Array.from({ length: points + 1 }, (_, i) => +f(i / points).toFixed(4))
  return `linear(${values.join(', ')})`
}

The springs in the recap's stylesheet are lists like this, worked out from a stiffness and a damping value, with plain cubic-bezier curves as a fallback for older Safari.

The rule: rest at the end

This is the decision the rest of the post depends on: an element's own style is its final state, and keyframes only describe how it gets there.

Most entrance animations are written the other way around. The element starts hidden (opacity: 0) and an animation brings it into view. Here, an element's CSS describes where it ends up, and its keyframes only have a from:

app/components/recap/recap.css
.recap .rise {
  animation: rcp-rise 700ms var(--ease-out) both;
  animation-delay: var(--d, 0ms);
}
@keyframes rcp-rise {
  from { opacity: 0; transform: translateY(calc(5 * var(--u))); }
}

There's no to. When a keyframe block leaves out the end, the browser animates toward the element's own style. So if you take the animation away, what's left is the finished slide, not an empty one.

The bigger scenes are built the same way. When a streak is too long to fit, the chain pans so the newest link stays in view. Its resting position is the end of the pan:

app/components/recap/scenes/SceneStreak.vue
.chain.pans {
  transform: translateX(var(--pan-end));   /* where it ends up */
  animation: pan var(--pan-dur) var(--pan-ease) var(--pan-start) both;
}
@keyframes pan {
  from { transform: none; }
}

That one rule pays for itself three times.

1. Reduced motion is one rule

People who turn on "reduce motion" in their system settings get each finished scene immediately. Every animation in the slide is switched off, and since every element already rests at its end state, nothing is left invisible or half drawn. No scene needs a second, static version of itself.

app/components/recap/recap.css
@media (prefers-reduced-motion: reduce) {
  .recap .slide *,
  .recap .slide *::before,
  .recap .slide *::after {
    animation: none !important;
  }
}

2. Saved images use the same components

Customers can save any slide as a 1080×1920 image to post in their own stories. There's no second renderer for that. The save picker renders the slide in its reduced-motion state, so it's already finished, and captures that piece of the page as a PNG with modern-screenshot, a small library that only loads when the picker opens.

Numbers that count up are the one part driven by JavaScript, so they need to know they're in a still. They read a shared clock through Vue's provide/inject, and the still provides one that says "reduced", which makes them show their final value straight away:

app/components/recap/RecapStill.vue
<script setup lang="ts">
// Count-ups and other clocked bits show their final value.
provide(RECAP_CLOCK, { held: ref(true), reduced: ref(true) })
</script>

<template>
  <div class="recap still is-reduced">
    <RecapSlide :slide="slide" :payload="payload" :tone="tone" :reduced="true" />
  </div>
</template>

Every scene added later works as an image without anyone thinking about it, because following the rule is what makes it work.

3. The same Highreel plays the same way every time

Scenes also don't use clocks or Math.random(). Their timing comes from the data through CSS variables (--t, --d), and anything that looks random, like the slight jitter in a pile of tokens or the wrong answers in the optional "guess first" quiz, comes from a seeded generator keyed by the slide and its value:

app/components/recap/scenes/SceneCount.vue
const rand = seeded(`count|${props.slide.key}|${value.value}`)

seeded is a tiny pseudo-random generator (mulberry32) whose seed is a hash of that string. The same key always produces the same sequence. So the preview in the business's editor, the page the customer opens and the image they save all show exactly the same composition. If the business likes how a slide looks in the preview, that's the slide the customer gets. It also prevents a classic server-rendering bug, where the server picks one random value, the browser picks a different one while hydrating, and the page flickers or warns about a mismatch.

Keeping the page light

The recap page's JavaScript is around 131 KB gzipped, against the 150 KB budget. Two things keep it there.

Code that only some visitors need only loads for them. The screenshot library loads when the save picker opens. Sign-in code, toasts and tooltips don't exist on this page at all.

The budget is also checked by a test, because some regressions can't be seen in a code review. At one point, the landing page lazy-loaded the recap player so its example phone wouldn't slow down the first paint. That looked harmless. But lazy-loading the player made the bundler create a small runtime helper, and it put that helper inside one of the workspace's chunks. Every recap page then downloaded 21 KB of the workspace's menus and utilities just to get it. Nothing in the diff hinted at that. The budget test measures the real JavaScript of a production build, which is how it was caught. The landing now lazy-loads a thin wrapper around the player instead.

When a link is pasted into WhatsApp or iMessage, the app fetches a preview image for it. Every Highreel has its own: the customer's name, two of their numbers and the business's colors, drawn on the server as SVG and turned into a PNG by resvg compiled to WebAssembly. Each one costs about a third of a second of CPU, and every chat app fetches its own copy.

So each preview is drawn once and stored in R2, under a name that includes a hash of everything the image shows:

server/api/r/[token]/og.png.get.ts
const key = `${folder}/og/${token}-${await hashOf(recap, logoPath)}.png`
const stored = await getMedia(event, key)
if (stored)
  return stored.data

The hash covers the Highreel's data, minus the signed photo URLs that change every hour without changing the picture, plus a DESIGN number that gets bumped whenever the image's layout changes. There's no cache invalidation to write. When the business changes the wording, the colors or a number, the hash changes, so the next fetch draws a new image and deletes the old one. The first fetch takes about 300 ms, and every one after it about 15.

This is content-addressed caching: name things after what's in them, and a stale copy can't be served, because nothing will ever ask for its name again.

The takeaway

Choosing what a scene looks like when nothing is moving turned out to be the most useful decision in the player. Reduced motion, saved images and identical previews all came from that one rule, instead of three features to build and keep in sync.

GitHub activity