Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

p5.capture

tapioca24139MIT1.6.0TypeScript support: included

super easy recording for p5.js animations 📸

sketch, p5js, p5, p5.js, processing, art, creative, creative-coding

readme

p5.capture

AboutWhy p5.capture?Getting startedAPI
OptionsBrowser compatibilityLimitationsDonation

About

Assuming you would like to record p5.js animations super easily, this package is the right choice for you.

👇 Check out the demo:

Why p5.capture?

🎩 Stable recording

Recording p5.js animations with screen recording tools can cause jerky recordings. Complex animations can slow down the framerate and make recording unstable. p5.capture hooks into the p5.js draw function and records the rendered frame, so it works like magic.

✨ Keep your sketch clean

Adding recording functionality to a sketch can be very tedious. p5.capture provides a minimal GUI and is designed to add recording functionality without adding any code to your sketch. Let's focus on your creative coding. Of course, you can also use the API to integrate it into your code.

🤹 Any format • One API

Tired of having to use different libraries for different formats? p5.capture supports many export formats including WebM, GIF, MP4, PNG, JPG, and WebP. There is sure to be something you want.

Getting started

Installation

Insert a link to the p5.capture after p5.js in your html file:

<script src="https://cdn.jsdelivr.net/npm/p5/lib/p5.min.js"></script>
<!-- insert after p5.js -->
<script src="https://cdn.jsdelivr.net/npm/p5.capture@1.6.0/dist/p5.capture.umd.min.js"></script>

You can find all versions in the jsDelivr.

Usage

Basically, the GUI provided by p5.capture starts and stops recording.

usage

That's all 🎉

Export formats

Supported formats include:

  • WebM (default): export WebM video using webm-writer-js
  • GIF: export animated GIF using gif.js
  • MP4: export MP4 video using h264-mp4-encoder
  • PNG: export PNG images in a ZIP file
  • JPG: export JPG images in a ZIP file
  • WebP: export WebP images in a ZIP file

API

The P5Capture class can be used to programmatically control recording.

Static methods

P5Capture.getInstance()

Returns a P5Capture instance.
Used to access the P5Capture instance automatically created when p5.capture is initialized.

P5Capture.setDefaultOptions(options)

Change default options. These options affect both GUI and API recording.
Must be used before p5.js initialization.

P5Capture.setDefaultOptions({
  format: "gif",
  framerate: 10,
  quality: 0.5,
  width: 320,
});

function setup() {
  // do something...
}

Instance methods

start(options?)

Start recording with the specified options.
options can be omitted.

stop()

Stop recording and start encoding.
Download files after encoding is complete.

Instance properties

state (Read only)

Returns the current recording state ("idle", "capturing", or "encoding").

Examples

The following example shows how to record the first 100 frames and create a GIF video:

function draw() {
  if (frameCount === 1) {
    const capture = P5Capture.getInstance();
    capture.start({
      format: "gif",
      duration: 100,
    });
  }

  // do something...
}

The following example shows how to add an event handler that starts and stops recording with a keystroke:

function keyPressed() {
  if (key === "c") {
    const capture = P5Capture.getInstance();
    if (capture.state === "idle") {
      capture.start();
    } else {
      capture.stop();
    }
  }
}

Options

Name Default Description
format "webm" export format. "webm", "gif", "mp4", "png", "jpg", and "webp"
framerate 30 recording framerate
bitrate 5000 recording bitrate in kbps (only available for MP4)
quality see Quality option recording quality from 0 (worst) to 1 (best). (only available for WebM/GIF/JPG/WebP)
width canvas width output image width
height canvas height output image height
duration null maximum recording duration in number of frames
autoSaveDuration null automatically downloads every n frames. convenient for long captures (only available for PNG/JPG/WebP)
baseFilename see Base filename option function to customize the filename of a video or zip file. see Base filename option for details
imageFilename see Image filename option function to customize the filename of a image file. see Image filename option for details
beforeDownload undefined function called before file download. see Before download option for details
verbose false dumps info on the console
disableUi false (only P5Capture.setDefaultOptions()) hides the UI
disableScaling false (only P5Capture.setDefaultOptions()) disables pixel scaling for high pixel density displays

Quality option

The default value of the quality option is different for each format.

Format Default
WebM 0.95
GIF 0.7
JPG 0.92
WebP 0.8

Base filename option

You can customize the filename of a video or zip file by specifying a function that returns a filename string. A Date object is passed as the first argument. This object indicates the time the encoding was completed and is useful for making the filename unique.

By default, the following function is used to determine the filename.

function baseFilename(date) {
  const zeroPadding = (n) => n.toString().padStart(2, "0");
  const years = date.getFullYear();
  const months = zeroPadding(date.getMonth() + 1);
  const days = zeroPadding(date.getDate());
  const hours = zeroPadding(date.getHours());
  const minutes = zeroPadding(date.getMinutes());
  const seconds = zeroPadding(date.getSeconds());
  return `${years}${months}${days}-${hours}${minutes}${seconds}`;
}

Note that the extension is automatically assigned and is not included in the return value of the function.

Image filename option

You can customize the filename of a image file by specifying a function that returns a filename string. The index of the recording frame is passed as the first argument. This is useful for making the filename unique.

By default, the following function is used to determine the filename.

function imageFilename(index) {
  return index.toString().padStart(7, "0");
}

Note that the extension is automatically assigned and is not included in the return value of the function.

Before download option

You can interrupt and add your own code before the file download.

P5Capture.setDefaultOptions({
  beforeDownload(blob, context, next) {
    // call your own code to do before file download.
    console.log(blob.size, context);

    // calling `next` callback will start the file download.
    // this can be omitted if not needed.
    next();
  },
});

The following arguments are passed:

  • blob: the generated Blob object
  • context: provides the context object
    • filename: the filename expected when downloading
    • format: output format
  • next: callback function to start the download

Browser compatibility

It may not work depending on your environment.
Tested in the following environments:

| | Chrome | Edge | Firefox | Safari | | ---- | ------ | ---- | ------- | ------ | | WebM | ✅ | ✅ | ✅ | ❌ | | GIF | ✅ | ✅ | ✅ | ✅ | | MP4 | ✅ | ✅ | ✅ | ✅ | | PNG | ✅ | ✅ | ✅ | ✅ | | JPG | ✅ | ✅ | ✅ | ✅ | | WebP | ✅ | ✅ | ✅ | ✅ |

The browser versions used for testing are

  • Chrome 98.0.4758.109
  • Edge 98.0.1108.62
  • Firefox 97.0.2
  • Safari 15.3

Limitations

  • Multiple instances is not supported
  • Module bundlers is not supported

Donation

This project is open source and always will be, even if I don't get donations. That said, I know there are people out there that may still want to donate just to show their appreciation so this is for you guys. All donations will be used for sustainable software development. Thanks in advance!

ko-fi
Tezos Profile

changelog

1.6.0 (April 26, 2025)

  • Support for p5.js v2

1.5.0 (April 13, 2025)

  • Support for WebGL2
  • Update dependencies

1.4.1 (January 15, 2023)

1.4.0 (November 19, 2022)

1.3.0 (August 11, 2022)

1.2.0 (April 30, 2022)

1.1.1 (April 12, 2022)

1.1.0 (March 27, 2022)

1.0.0 (March 25, 2022)

  • First stable release 🎉

0.5.1 (March 24, 2022)

  • Update documentation

0.5.0 (March 23, 2022)

  • (BREAKING!!) Revamp API
  • Make the recording button focusable
  • Change the unit test runner to Vitest
  • Update documentation

0.4.2 (March 20, 2022)

  • Update documentation

0.4.1 (March 19, 2022)

  • Remove debug code

0.4.0 (March 19, 2022)

  • (BREAKING!!) Change how to change default options
  • Use h264-mp4-encoder for mp4 format

0.3.2 (March 15, 2022)

  • Add type definition

0.3.1 (March 10, 2022)

  • Change the style of the UI

0.3.0 (March 8, 2022)

  • Add UI for format and framerate

0.2.0 (March 6, 2022)

  • (BREAKING!!) Change options
  • Change the UI
  • Add width and height options for resize

0.1.2 (February 27, 2022)

  • (BREAKING!!) Fix package defalut file

0.1.1 (February 27, 2022)

  • (BREAKING!!) Fix mp4-wasm to import from URL

0.1.0 (February 27, 2022)

  • (BREAKING!!) Change the webm recording implementation from MediaRecorder to webm-writer-js.
  • Add mp4 support (Experimental)

0.0.2 (February 23, 2022)

  • Change the default value of videoBitsPerSecond in WebM to 20Mbps

0.0.1 (February 23, 2022)

  • Initial public release