p5.record.js
v0.3.0
Published
p5.js addon providing functions to record HTML canvas based sketches
Downloads
141
Maintainers
Readme
p5.record.js
Record your p5.js sketch and export it as a video or image sequence.
This p5.js addon library provides a simple interface to record your p5.js sketch into a video or image sequence by using the captureStream() or toBlob APIs of HTMLCanvasElement. No external dependencies is included or required with this library.
p5.record.js starts with a set of default configuration that covers the simplest and likely most common use, while also providing additional customization options to achieve additional functionalities:
- Recording at specific defined frame rate, and in supported browsers, full control over when individual frame capture should happen.
- Record canvas backed objects, such as
p5.Graphics, and HTML canvas directly. - Create multiple recordings at the same time.
- Record image sequence.
Usage
To add p5.record.js to your sketch, include the following script tag in your HTML file:
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/p5.record.min.js"></script>ESM builds are also available either via CDN at https://cdn.jsdelivr.net/npm/[email protected]/dist/p5.record.esm.js or via NPM:
npm install p5.record.jsFor different use cases, please check out the examples/ folder. The available p5.js addon functions are as follow:
setRecording(options)- Set a configuration for recording, accepts an options object as argument. All options are optional. The default configuration (already set without needing to callsetRecording) is as follow:options = { frameRate: getTargetFrameRate(), source: canvas.elt, mimeType: "video/webm;codecs=vp8" }frameRate- (Number|"manual") Ifoptions.frameRateis set to a string with value"manual", sketch recording will automatically be tied to each call ofdraw(). This is useful when you have set the sketch tonoLoop()and is manually callingredraw(), only available in supported browsers.source- An object containing a property ofcanvasof typeHTMLCanvasElement(eg.p5.Graphicsinstance) or anHTMLCanvasElement.mimeType- The container and codec to be used for recording. Currently only a very small set of containers and codecs are supported natively, it is recommended to leave the default and reencode the video after downloading the result. Set this toimage/png,image/jpeg, orimage/webpto capture image sequence instead of video, will download a zip file instead.
startRecording()- Start recording at specified frame rate.stopRecording()- Stop recording and download the recording file.pauseRecording()- Pause recording to be resumed later.resumeRecording()- Resume previously paused recording.createRecording(options)- Create an independent recorder instance. See theRecorderclass below for usage of its return value. Accepts the sameoptionsobject assetRecording(options)as argument butframeRateandsourceare required.
The library also provides a p5.Recorder class for multiple simultaneous recording usage. In p5.js sketches, it is recommended to use createRecording(options) factory function to create instance of this class.
Recorder class
- Constructor -
new Recorder(options)accepts the same options object ascreateRecording(options), ie.frameRateandsourceare required. state- Recorder state, can be one of"inactive","recording", or"paused".start()- Start the recording.stop()- Stop the recording.pause()- Pause the recording.resume()- Resume the recording.frame()- Record a single frame, to be used in conjunction withoptions.frameRate = "manual"
Limitations
This library is only designed for canvas backed sketches and objects, recording of other inputs (eg. SVG) is not supported.
Currently the video recording output format is limited to VP8 encoded WebM video file, depending on use case you may need to reencode the video to a different format.
For image sequence recording, PNG, JPEG, and WebP formats are supported (except for Safari which does not support WebP). The image sequence will be downloaded as a Zip file with no compression applied. When capturing image sequence, you are more likely to encounter physical limits such as RAM limits or Zip file limits (archive size is currently limited to 4GB and 65,535 entries, I may implement ZIP64 later but it is a little bit less commonly supported, you may run out of RAM first anyway).
Future features
- Custom output video formats
- Record as gif
