Frame-Driven Refactoring: Convert "Timeline" Animations to "Frame" Animations
When to Use
- You have animation code from GSAP / anime.js / framer-motion / Web Animations API / native rAF and want to render it in Remotion
- You've already ported it, but you see symptoms like: static visuals, export mismatching Studio preview, misalignment on skipped frames, intermittent ghosting in concurrent renders, or animations running faster/slower in the final video than in preview
- You're considering "screen recording" to work around this—read
references/recording-vs-frames.mdfirst; you usually shouldn't record
The One Rule
Visual = f(frame). Given a frame number, produce exactly one set of pixels, independent of rendering order, machine, or concurrency level.
All external time sources must be replaced with frame numbers: requestAnimationFrame, setTimeout, Date.now, performance.now, CSS transition/animation, gsap.ticker, or any animation library's internal Ticker.
The reason is that Remotion renders frames out of order and concurrently: frame 900 may render before frame 300. Any code that "depends on the previous frame's state" (accumulation, eased approximation, spring integration) will produce ghost results.
One-sentence self-check:
Is frame 200 rendered in isolation pixel-identical to frame 200 when rendered as part of a continuous sequence from 0 to 200?
Conversion Base
frame = seconds × fps
Get fps from useVideoConfig(), don't hardcode 30. Same for duration.
Quick Reference (full version in references/gsap-mapping.md)
| Original Code | Frame-Driven Equivalent |
|---|---|
gsap.to(el, {x: 100, duration: 1}) | interpolate(frame, [0, fps], [0, 100]) |
ease: "power2.out" | Easing.out(Easing.cubic) |
tl.to(a).to(b, "-=0.3") | Frame-point constants: const aEnd = aStart + 30; const bStart = aEnd - 9; |
stagger: 0.05 | start + i * Math.round(0.05 * fps) |
repeat: -1 | frame % period |
yoyo: true | Triangle wave (pingPong from assets/keyframes.ts) |
tl.progress()-driven scrub | Use the incoming progress value directly as frame |
DOM modifications in onUpdate | Don't touch DOM; render from derived values |
SplitText character-by-character entry | Use charIndex to calculate each character's delay in frames |
useSpring / motion.div | spring({frame, fps, config}) |
Draggable / InertiaPlugin | Closed-form v0*t + ½at², or precomputed curve sampling |
canvas rAF loop | Replace t with frame; keep the drawing function side-effect-free |
Seven-Step Workflow
- Audit time sources:
bash assets/audit-time-sources.sh <source-dir>– lists all rAF / timers / animation libraries / CSS animations. - Eliminate external state: mouse position, window size, network,
Math.random(). If randomness is needed, userandom(seed)with an explicit seed; no parameterless random during rendering. - Flatten the timeline: Convert chained timelines into a keyframe table
[[frame, value], ...], write frame numbers as constants, and centralize in one timeline module shared by animation, recording script, and export script (don't write it twice). - Derive values at the top of the component: One pure
constblock for computations, then only rendering below. - Map easings: GSAP ease names →
Easing.*(see reference table; note: power1=quad, power2=cubic, don't swap them). - Turn side effects into rendering:
el.style.transform = ...→style={{transform: ...}}; imperative canvas draw → parameterized draw function. - Verify (see next section).
Case Study: Pitfalls in a Vertical Promo Video
Symptoms / Root Cause / Fix – applies beyond this specific case.
-
End freeze frame showed the video's opening Root cause:
<Freeze frame>'s frame number was relative to the parent<Sequence>start; using an absolute frame caused an offset. Fix:<Freeze frame={0}>only means "freeze at frame 0 of this segment"; to freeze at absolute frame N, useframe={N - sequence.from}, or avoid Freeze altogether. -
Subtitle focus index stuck at 0 Root cause:
useCurrentFrame()inside a<Sequence>returns a frame relative to the parent's start; direct comparison against absolute frame never matched. Fix: Unify the absolute frame perspective – subtractfrombefore entering the Sequence (or convert before passing props). -
Audio volume curve not working / timing off Root cause: Same relative-frame issue – the callback for
<Audio volume={(f) => ...}>receives sequence-local frames. Fix: In thevolumecallback, computef + sequenceFromthen look up the curve. -
Frame-by-frame accumulation of position jitters on export Root cause:
pos += v * dtdepends on the previous frame's result, but frames render out of order. Fix: Always use a closed-form solution –pos(frame)computes from starting value + frame number; don't read the previous frame. -
CSS
transition/@keyframesdon't animate in the final video Root cause: Remotion usesseekto capture frames; it doesn't advance the CSS clock, so transitions never complete. Fix: Replace all with inline styles +interpolate; disable or convert transition effects in third-party components via props. -
Measured positions offset in preview but correct on export Root cause: Studio preview scales the canvas;
getBoundingClientRect()returns scaled values. Fix: Normalize by dividing by containeroffsetWidth/offsetHeight; use ratios, not pixels. -
Async resources (images, fonts, resize measurements) cause blank first frame Root cause: Screenshot captured before data is ready. Fix: Call
delayRender()at start,continueRender(handle)at end; for images, additionallyawait img.decode(). -
three.js / Lottie / Pixi have their own clocks Root cause: These libraries have built-in tickers or
useFrameloops that don't respect Remotion's frame. Fix: Take over the clock – for@remotion/three, useframeloop="never"+advance()each frame; for Lottie, usegoToAndStop(frame, true); for Pixi, manually callapp.ticker.update()or render frame by frame withrender().
Verification
bash assets/verify-determinism.sh <entry-file> <CompositionId> [frame-number] does three things:
- Single-frame render (
still) vs same frame from a continuous sequence (--sequence) – catches hidden state that "only works when played from frame 0" - Concurrency 1 vs concurrency 8 rendering the same range – catches "depends on render order"
- Compares PNG MD5 checksums (don't compare MP4; H.264 is lossy and can't show differences)
# Manual version (what the script does internally)
npx remotion render <entry> <Id> out/seq-a --sequence --image-format=png --frames=196-204 --concurrency=1
npx remotion render <entry> <Id> out/seq-b --sequence --image-format=png --frames=196-204 --concurrency=8
npx remotion still <entry> <Id> out/still.png --frame=200
# md5 out/seq-a/element-200.png out/seq-b/element-200.png out/still.png All three must match
Note: --sequence defaults to JPEG output; you must explicitly use --image-format=png; files are named element-[frame].png by default.
Numeric metrics only prove "consistent," not "visually correct" – a human must confirm visual correctness.
When Is Screen Recording Allowed?
See references/recording-vs-frames.md for detail. One-sentence version:
- Allowed: Real software UI / terminal output (faking it is unprofessional), third-party WebGL scenes you can't modify the source of, complex shaders with prohibitive per-frame rendering budgets
- Not allowed: Text animations, charts, transitions, particles, anything where "changing one word would require re-recording"
- Compromise: Recorded footage used as a texture (image) while animation and text are still done frame-driven
Environment Notes
These conclusions were validated on Remotion 4.0.438 + React 19 + three 0.187. Easing actually exports only linear/ease/quad/cubic/poly(n)/sin/circle/exp/elastic(n)/back(s)/bounce/bezier/step0/step1 – no quart/quint, use poly(4)/poly(5) instead.
Attached Assets
| File | Purpose |
|---|---|
assets/keyframes.ts | Keyframe interpolation, loop, yoyo, stagger, progress helpers |
assets/audit-time-sources.sh | Scans project for all external clocks (categorized as "high confidence" and "needs manual review") |
assets/verify-determinism.sh | Single-frame vs sequence, concurrency 1 vs 8, compares PNG MD5 |
references/gsap-mapping.md | Line-by-line mapping and code examples from GSAP/Anime/Framer to Remotion |
references/recording-vs-frames.md | Decision document for recording vs frame-driven (includes real incident postmortems) |