p5js-render

Render p5.js sketches to MP4, WebM or GIF in headless Chromium. Every frame is drawn once, in order, on a virtual clock with a seeded random generator, so the same sketch always makes the same video.

View on GitHub

Quick start

# install, then fetch the browser once
pnpm install && pnpm exec playwright install chromium

# render an example, with the debug HUD burned in
pnpm render cube-wave --debug

# render your own sketch to a looping GIF
pnpm render ./sketch.js -o sketch.gif --duration 4

How it works

  1. Your sketch

    • sketch.js
    • + assets
  2. Headless Chromium

    • seeded random()
    • virtual clock
    • frames 1, 2, 3… in order
  3. Encoder workers

    • PNG, in parallel
    • next frame keeps drawing
  4. ffmpeg

    • MP4
    • WebM
    • GIF

Questions

Still stuck? Read the README or open an issue.

How do I render a p5.js sketch to an MP4 video?

Install p5js-render with pnpm, run pnpm exec playwright install chromium once, then run pnpm render ./sketch.js -o sketch.mp4 --duration 4. The sketch runs in headless Chromium and each frame streams into ffmpeg.

How do I make a p5.js animation loop seamlessly?

Drive every motion from window.p5Render.progress, which runs from 0 up to (not including) 1 across the video. Use whole periods such as sin(TWO_PI * progress), so the frame after the last one matches the first.

Why do screen recordings of p5.js sketches drop frames or differ between runs?

Live recording depends on the wall clock and on Math.random. p5js-render replaces both: millis(), deltaTime and Date follow the frame number, and random() and noise() are seeded, so every render of the same sketch and seed is identical.

Can I export a p5.js sketch as a GIF or WebM?

Yes. The output extension picks the format: .mp4 for H.264, .webm for VP9 and .gif for a looping GIF with an optimized palette.

Does it support WebGL, p5.js 2.x and async setup?

Yes. It runs the installed p5.js 2.x by default, awaits async setup(), supports instance mode and WEBGL canvases, and can load any p5 version with --p5-version.

How is p5js-render different from p5.capture or CCapture?

p5.capture and CCapture record inside your browser tab. p5js-render runs from the command line, a Node API or an HTTP server, so it suits batch renders, CI and servers, and it keeps stateful sketches exact by drawing every frame in order.