npm.io
1.3.2 • Published 2 months ago

p5.webm-capture

Licence
MIT
Version
1.3.2
Deps
1
Size
510 kB
Vulns
0
Weekly
0

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.3.1/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.3.1/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):

Name Default Description
element document.querySelector('canvas') the <canvas> tag p5.js is drawing to. If you're using https://editor.p5js.org, it's usually document.getElementById("defaultCanvas0"), but you can leave it to the default.
frameCount 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 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 false shows a HUD style timecode view
onComplete 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.

dependencies

  • ccapture.js (v2.0.0, MIT License) by Jaume Sanchez Elias, providing ESM, TypeScript types, and modern WebCodecs hardware-accelerated encoding with automatic fallback to WebM.

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]
$ npm publish

Changelog

1.3.1: Fixed race condition between CCapture start() and capture() in which the first frame could be captured before the library was ready to encode it, resulting in duplicate first frames and dropped last frames. Added e2e test covering this behavior.

1.3.0: Upgrade to ccapture.js 2.0.0 for more encodings out of the box and better cross-browser support.

1.2.1: package version update.

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.

Keywords