LLMs Writing Code for Animation: Programmatic Visual Demos in the Browser

    Large language models are generating viral demos not as video frames but as HTML, CSS, Canvas, WebGL, and Web Audio that the browser renders. This article explains the shift from pixel synthesis to executable rendering programs, covering pipeline choices, timeline-driven cameras, performance, and how this differs from template-based AI slide tools.

    Overseas AI communities have been flooded with unusual work: a presentation with more than seven million views and nearly thirty thousand likes, plus a sixty-second hand-drawn-style animation and a finely rendered 3D mini-game. What matters is not the pixels themselves but how they are produced. The models do not emit video frames. They write code; the browser renders it at runtime. Characters, scenes, camera moves, and even background music come from HTML, CSS, Canvas, WebGL, and Web Audio. That shift moves “AI-generated presentation content” from filling templates to programmatic visual generation, and it is worth unpacking for developers.

    1. From generating pixels to generating a renderer

    Traditional video generation uses diffusion models to synthesize frames. It is expensive, hard to control, and limited in length. The code path is different: the model outputs structured scene descriptions and animation logic, then a deterministic engine executes them. Three contrasts stand out.

    • Controllability: changing a parameter reflows the whole piece instead of resampling.
    • Size: a multi-scene deck can stay in the low-megabyte range because you ship code, not a frame sequence.
    • Interactivity: the scene can respond to clicks, scroll, and viewport size—something a static video cannot do.

    The trade-offs are equally clear. Visual quality is capped by the engine, and generated code needs validation and fault tolerance or you get empty scenes, broken timelines, and performance collapse.

    2. Choosing a rendering stack

    Different visual goals map to different stacks; in practice they are mixed.

    Goal Approach Notes
    Layout and transitions DOM + CSS transform/transition Sharp text, accessibility, easy debugging
    2D drawing and particles Canvas 2D Per-frame drawing; brush feel and many primitives
    3D scenes and cameras WebGL (and wrappers) Camera, lighting, materials, depth
    Vector and stroke animation SVG + stroke-dashoffset Line-growth effects
    Sound and score Web Audio API Procedural audio without external files

    3. Timeline and camera: the skeleton of the piece

    For demo work, rhythm matters more than a single pretty frame. Treat time as the single source of truth; every property is a pure function of time:

    1. // Single timeline: pause, seek, and record stay simple
    2. const tl = { t: 0, duration: 65 };
    3. function easeInOutCubic(x) {
    4. return x < 0.5 ? 4 * x * x * x : 1 - Math.pow(-2 * x + 2, 3) / 2;
    5. }
    6. function sceneAt(t) {
    7. // Scene cuts: current index and local progress
    8. const idx = Math.min(SCENES.length - 1, Math.floor(t / 5));
    9. const local = (t - idx * 5) / 5;
    10. return { idx, p: easeInOutCubic(Math.min(1, Math.max(0, local))) };
    11. }
    12. function frame(now) {
    13. tl.t = (now - start) / 1000;
    14. const { idx, p } = sceneAt(tl.t);
    15. camera.position.z = lerp(6, 2, p); // dolly in
    16. renderer.render(scene, camera);
    17. requestAnimationFrame(frame);
    18. }

    In 3D, cinematic “page turns” are camera interpolation along a path, plus approximations of depth of field and motion blur. In 2D, masks, parallax, and easing create the illusion of space.

    4. Performance and experience

    • Frame budget: target 16.7 ms; limit draw calls and materials; avoid allocating objects every frame.
    • Assets: reuse geometry and textures; lazy-load heavy resources; first paint loads only scene one.
    • Degradation: detect device capability and drop post-processing and shadows on weaker hardware.
    • Accessibility: keep semantic DOM for text, captions, pause controls, and respect reduced-motion preferences.
    • Stability: lint generated code, catch exceptions, and time out so a bad run does not white-screen the page.

    5. Versus conventional AI presentation tools

    Most AI slide tools still generate an outline and drop it into templates—static pages. The programmatic path produces a runnable web app, with launch-style motion, that embeds in a page, versions like code, and debugs with a normal frontend toolchain. The bar for users therefore moves from “write a prompt” to “read and tweak the generated code.”

    6. Engineering it into a product

    A layered design works: a content layer emits structured scene scripts (JSON); a render layer interprets them; a validation layer applies static checks and budget constraints; a publish layer hosts artifacts on object storage, accelerates them with a CDN, and uses a container platform for stateless render services plus logs, metrics, and alerts on failure rate and time-to-first-frame. Baidu AI Cloud can supply these generic building blocks rather than a proprietary demo API.

    Stay clear-eyed: visual code from models is stochastic. The same prompt can diverge across runs, so generate, validate, and hand-tune remains the reliable loop. What these pieces prove is not that models “learned animation,” but that the compile path from natural language to an executable renderer is now smooth enough—that is the engineering takeaway.