p5.webm-capture

1.2.1 • Public • Published

p5js webm capture

npm version

record frame-perfect p5.js sketches to .webm format video files in the Chrome browser. handy for turning slow-developing simulation sketches into smooth animations or creating perfect-loop videos.

combination of cc-capture and webm-writer, handy for capturing frames of sketches and producing webm video output. different from other p5.recording libraries because it doesn't capture a video stream of the canvas, it captures frames individually and rebuilds them into a webm video file, so you can produce a smooth final product even if your sketch is running on a slow computer.

designed to work with p5.js and Chrome. I wrote this so I could include it on https://editor.p5js.org sketches via CDN.

it is currently available at: https://unpkg.com/p5.webm-capture@1.2.0/dist/p5.webm-capture.js

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
    <script src="https://unpkg.com/p5.webm-capture@1.2.0/dist/p5.webm-capture.js"></script>
    <meta charset="utf-8" />
  </head>
  <body>
    <script>
      let c = 0;

      function setup() {
        createCanvas(400, 400);
        colorMode(HSB);
        enableCapture({
          frameCount: 360,
        });
      }

      function draw() {
        background(c, 100, 100);
        c = (c + 1) % 360;
      }
    </script>
  </body>
</html>

Usage

Include p5.webm-capture.js in your index.html page right anytime after your p5.js script tag. p5.js must be loaded before p5.webm-writer.

p5.webm-writer only has one function you need to worry about: enableCapture. It should be called from your setup() function after createCanvas.

enableCapture

Should be called in sketch setup(), after createCanvas. Prepares the webm-capture library to start grabbing frames.

Options (defaults shown in square brackets):

  • element {HTMLCanvasElement} [querySelector('canvas')] the <canvas> tag p5.js is drawing to. If you're using https://editor.p5js.org, it's usually document.getElementById("defaultCanvas0").
  • frameCount {Integer} [600] the total number of frames to capture. If you give a value less than 1, the library will run in "manual stop" mode and you'll have to call stopCapture to finish.
  • frameRate {Integer} [60] sets the frames-per-second of the final video. NOTE: this doesn't have to be the same as the frameRate of the p5.js sketch.
  • display {boolean} [false] shows a HUD style timecode view
  • onComplete {Function} [function () {}] a function that is called when capture is complete. You could use this to stop the sketch or send a message to an external process.

So a call with every option given would look like:

enableCapture({
  element: document.querySelector("canvas"),
  frameCount: 120,
  frameRate: 30,
  display: true,
  onComplete: function () {
    alert("boop!");
  },
});

By default, given no arguments, a 10 second video at 60fps will be captured and your sketch will continue running.

stopCapture

This is an optional method that can be called anytime during recording to stop capturing frames, download the recorded video, and call onComplete.

If you give a value less than 1 for frameCount, calling stopCapture() is required to stop recording and download. This is "manual stop" mode.

included libraries

I have heartily abused the following libraries to make them play nicely with esbuild:

I stripped away everything not needed for running webm exports from Chrome. You can find their source in the src/vendor folder.

hacking

Make changes, then:

$ npm run build
$ cd example
$ ./serve
$ open http://localhost:8000

Releasing a new version of the library:

# commit all changes, then
$ npm version [major|minor|patch]

Changelog

1.2.0: expose the stopCapture method and allow passing a value less than 1 to frameCount to signal the intent to stop recording manually.

1.1.0: uses p5.js library hooks so that captureFrame() is called after the draw() function automatically.

1.0.1: functioning release (like 30 minutes after 1.0.0)

1.0.0: initial release.

Package Sidebar

Install

npm i p5.webm-capture

Weekly Downloads

3

Version

1.2.1

License

MIT

Unpacked Size

117 kB

Total Files

4

Last publish

Collaborators

  • abachman