p5.record.js 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +27 -0
- package/README.md +51 -0
- package/examples/basic-sketch-recording/index.html +24 -0
- package/examples/basic-sketch-recording/src/sketch.js +32 -0
- package/examples/frame-by-frame-recording/index.html +24 -0
- package/examples/frame-by-frame-recording/src/sketch.js +37 -0
- package/examples/graphics-recording/index.html +24 -0
- package/examples/graphics-recording/src/sketch.js +47 -0
- package/examples/multiple-parallel-recording/index.html +24 -0
- package/examples/multiple-parallel-recording/src/sketch.js +84 -0
- package/package.json +32 -0
- package/rollup.config.js +21 -0
- package/src/Recorder.js +76 -0
- package/src/main.js +63 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
Copyright (c) 2025, Kenneth Lim
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
|
4
|
+
modification, are permitted provided that the following conditions are met:
|
|
5
|
+
|
|
6
|
+
1. Redistributions of source code must retain the above copyright notice,
|
|
7
|
+
this list of conditions and the following disclaimer.
|
|
8
|
+
|
|
9
|
+
2. Redistributions in binary form must reproduce the above copyright
|
|
10
|
+
notice, this list of conditions and the following disclaimer in the
|
|
11
|
+
documentation and/or other materials provided with the distribution.
|
|
12
|
+
|
|
13
|
+
3. Neither the name of the copyright holder nor the names of
|
|
14
|
+
its contributors may be used to endorse or promote products derived from
|
|
15
|
+
this software without specific prior written permission.
|
|
16
|
+
|
|
17
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
18
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
19
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
20
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
21
|
+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
22
|
+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
23
|
+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
24
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
25
|
+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
26
|
+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
27
|
+
POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# p5.record.js
|
|
2
|
+
|
|
3
|
+
Record your p5.js sketch and export it as a video.
|
|
4
|
+
|
|
5
|
+
This p5.js addon library provides a simple interface to record your p5.js sketch into a video by using the [`captureStream()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/captureStream) API of `HTMLCanvasElement`. No external dependencies is included with this library.
|
|
6
|
+
|
|
7
|
+
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:
|
|
8
|
+
|
|
9
|
+
* Recording at specific defined frame rate, and in supported browsers, full control over [when individual frame capture should happen](https://developer.mozilla.org/en-US/docs/Web/API/CanvasCaptureMediaStreamTrack/requestFrame).
|
|
10
|
+
* Record canvas backed objects, such as `p5.Graphics`, and HTML canvas directly.
|
|
11
|
+
* Create multiple recordings at the same time.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
For different use cases, please check out the `examples/` folder. The available p5.js addon functions are as follow:
|
|
15
|
+
|
|
16
|
+
* `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 call `setRecording`) is as follow:
|
|
17
|
+
```js
|
|
18
|
+
options = {
|
|
19
|
+
frameRate: getTargetFrameRate(),
|
|
20
|
+
source: canvas.elt,
|
|
21
|
+
mimeType: "video/webm;codecs=vp8"
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
* `frameRate` - (`Number|"manual"`) If `options.frameRate` is set to a string with value `"manual"`, sketch recording will automatically be tied to each call of `draw()`. This is useful when you have set the sketch to `noLoop()` and is manually calling `redraw()`, only available in supported browsers.
|
|
25
|
+
* `source` - An object containing a property of `canvas` of type `HTMLCanvasElement` (eg. `p5.Graphics` instance) or an `HTMLCanvasElement`.
|
|
26
|
+
* `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.
|
|
27
|
+
* `startRecording()` - Start recording at specified frame rate.
|
|
28
|
+
* `stopRecording()` - Stop recording and download the recording file.
|
|
29
|
+
* `pauseRecording()` - Pause recording to be resumed later.
|
|
30
|
+
* `resumeRecording()` - Resume previously paused recording.
|
|
31
|
+
* `createRecording(options)` - Create an independent recorder instance. See the `Recorder` class below for usage of its return value. Accepts the same `options` object as `setRecording(options)` as argument but `frameRate` and `source` are required.
|
|
32
|
+
|
|
33
|
+
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.
|
|
34
|
+
|
|
35
|
+
### `Recorder` class
|
|
36
|
+
* Constructor - `new Recorder(options)` accepts the same options object as `createRecording(options)`, ie. `frameRate` and `source` are required.
|
|
37
|
+
* `state` - Recorder state, can be one of `"inactive"`, `"recording"`, or `"paused"`.
|
|
38
|
+
* `start()` - Start the recording.
|
|
39
|
+
* `stop()` - Stop the recording.
|
|
40
|
+
* `pause()` - Pause the recording.
|
|
41
|
+
* `resume()` - Resume the recording.
|
|
42
|
+
* `frame()` - Record a single frame, to be used in conjunction with `options.frameRate = "manual"`
|
|
43
|
+
|
|
44
|
+
## Limitations
|
|
45
|
+
This library is only designed for canvas backed sketches and objects, recording of other inputs (eg. SVG) is not supported.
|
|
46
|
+
|
|
47
|
+
Currently the 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.
|
|
48
|
+
|
|
49
|
+
## Future features
|
|
50
|
+
* Custom output video formats
|
|
51
|
+
* Record as gif and image sequence
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title>p5.record.js Basic Sketch Recording</title>
|
|
5
|
+
|
|
6
|
+
<script src="https://cdn.jsdelivr.net/npm/p5@2/lib/p5.min.js"></script>
|
|
7
|
+
<script src="../../dist/p5.record.min.js"></script>
|
|
8
|
+
|
|
9
|
+
<style>
|
|
10
|
+
html, body {
|
|
11
|
+
margin: 0;
|
|
12
|
+
}
|
|
13
|
+
</style>
|
|
14
|
+
</head>
|
|
15
|
+
<body>
|
|
16
|
+
<section style="margin: 10px;">
|
|
17
|
+
<button id="start-recording">Start Recording</button>
|
|
18
|
+
<button id="stop-recording">Stop Recording</button>
|
|
19
|
+
<button id="pause-recording">Pause Recording</button>
|
|
20
|
+
<button id="resume-recording">Resume Recording</button>
|
|
21
|
+
</section>
|
|
22
|
+
<script src="./src/sketch.js"></script>
|
|
23
|
+
</body>
|
|
24
|
+
</html>
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
let startBtn, stopBtn, pauseBtn, resumeBtn;
|
|
2
|
+
|
|
3
|
+
function setup() {
|
|
4
|
+
createCanvas(400, 400);
|
|
5
|
+
background(200);
|
|
6
|
+
|
|
7
|
+
startBtn = select("#start-recording");
|
|
8
|
+
stopBtn = select("#stop-recording");
|
|
9
|
+
pauseBtn = select("#pause-recording");
|
|
10
|
+
resumeBtn = select("#resume-recording");
|
|
11
|
+
|
|
12
|
+
startBtn.mouseClicked(startRecording);
|
|
13
|
+
stopBtn.mouseClicked(stopRecording);
|
|
14
|
+
pauseBtn.mouseClicked(pauseRecording);
|
|
15
|
+
resumeBtn.mouseClicked(resumeRecording);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function draw() {
|
|
19
|
+
line(mouseX, mouseY, pmouseX, pmouseY);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function keyTyped() {
|
|
23
|
+
if(key === "a"){
|
|
24
|
+
startRecording();
|
|
25
|
+
}else if(key === "s"){
|
|
26
|
+
stopRecording();
|
|
27
|
+
}else if(key === "d"){
|
|
28
|
+
pauseRecording();
|
|
29
|
+
}else if(key === "f"){
|
|
30
|
+
resumeRecording();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title>p5.record.js Frame By Frame Recording</title>
|
|
5
|
+
|
|
6
|
+
<script src="https://cdn.jsdelivr.net/npm/p5@2/lib/p5.min.js"></script>
|
|
7
|
+
<script src="../../dist/p5.record.min.js"></script>
|
|
8
|
+
|
|
9
|
+
<style>
|
|
10
|
+
html, body {
|
|
11
|
+
margin: 0;
|
|
12
|
+
}
|
|
13
|
+
</style>
|
|
14
|
+
</head>
|
|
15
|
+
<body>
|
|
16
|
+
<section style="margin: 10px;">
|
|
17
|
+
<button id="start-recording">Start Recording</button>
|
|
18
|
+
<button id="stop-recording">Stop Recording</button>
|
|
19
|
+
<button id="pause-recording">Pause Recording</button>
|
|
20
|
+
<button id="resume-recording">Resume Recording</button>
|
|
21
|
+
</section>
|
|
22
|
+
<script src="./src/sketch.js"></script>
|
|
23
|
+
</body>
|
|
24
|
+
</html>
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
let data;
|
|
2
|
+
let startBtn, stopBtn, pauseBtn, resumeBtn;
|
|
3
|
+
|
|
4
|
+
function setup() {
|
|
5
|
+
createCanvas(400, 400);
|
|
6
|
+
background(200);
|
|
7
|
+
|
|
8
|
+
startBtn = select("#start-recording");
|
|
9
|
+
stopBtn = select("#stop-recording");
|
|
10
|
+
pauseBtn = select("#pause-recording");
|
|
11
|
+
resumeBtn = select("#resume-recording");
|
|
12
|
+
|
|
13
|
+
startBtn.mouseClicked(startRecording);
|
|
14
|
+
stopBtn.mouseClicked(stopRecording);
|
|
15
|
+
pauseBtn.mouseClicked(pauseRecording);
|
|
16
|
+
resumeBtn.mouseClicked(resumeRecording);
|
|
17
|
+
|
|
18
|
+
setRecording({
|
|
19
|
+
frameRate: "manual"
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function draw() {
|
|
24
|
+
line(mouseX, mouseY, pmouseX, pmouseY);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function keyTyped() {
|
|
28
|
+
if(key === "a"){
|
|
29
|
+
startRecording();
|
|
30
|
+
}else if(key === "s"){
|
|
31
|
+
stopRecording();
|
|
32
|
+
}else if(key === "d"){
|
|
33
|
+
pauseRecording();
|
|
34
|
+
}else if(key === "f"){
|
|
35
|
+
resumeRecording();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title>p5.record.js Basic Sketch Recording</title>
|
|
5
|
+
|
|
6
|
+
<script src="https://cdn.jsdelivr.net/npm/p5@2/lib/p5.min.js"></script>
|
|
7
|
+
<script src="../../dist/p5.record.min.js"></script>
|
|
8
|
+
|
|
9
|
+
<style>
|
|
10
|
+
html, body {
|
|
11
|
+
margin: 0;
|
|
12
|
+
}
|
|
13
|
+
</style>
|
|
14
|
+
</head>
|
|
15
|
+
<body>
|
|
16
|
+
<section style="margin: 10px;">
|
|
17
|
+
<button id="start-recording">Start Recording</button>
|
|
18
|
+
<button id="stop-recording">Stop Recording</button>
|
|
19
|
+
<button id="pause-recording">Pause Recording</button>
|
|
20
|
+
<button id="resume-recording">Resume Recording</button>
|
|
21
|
+
</section>
|
|
22
|
+
<script src="./src/sketch.js"></script>
|
|
23
|
+
</body>
|
|
24
|
+
</html>
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
let graphic;
|
|
2
|
+
let startBtn, stopBtn, pauseBtn, resumeBtn;
|
|
3
|
+
|
|
4
|
+
function setup() {
|
|
5
|
+
createCanvas(400, 400);
|
|
6
|
+
background(200);
|
|
7
|
+
|
|
8
|
+
startBtn = select("#start-recording");
|
|
9
|
+
stopBtn = select("#stop-recording");
|
|
10
|
+
pauseBtn = select("#pause-recording");
|
|
11
|
+
resumeBtn = select("#resume-recording");
|
|
12
|
+
|
|
13
|
+
startBtn.mouseClicked(startRecording);
|
|
14
|
+
stopBtn.mouseClicked(stopRecording);
|
|
15
|
+
pauseBtn.mouseClicked(pauseRecording);
|
|
16
|
+
resumeBtn.mouseClicked(resumeRecording);
|
|
17
|
+
|
|
18
|
+
graphic = createGraphics(400, 400);
|
|
19
|
+
|
|
20
|
+
setRecording({
|
|
21
|
+
source: graphic
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function draw() {
|
|
26
|
+
line(mouseX, mouseY, pmouseX, pmouseY);
|
|
27
|
+
graphic.background(255);
|
|
28
|
+
graphic.fill(255, 0, 0);
|
|
29
|
+
graphic.push();
|
|
30
|
+
graphic.translate(200, 200);
|
|
31
|
+
graphic.rotate(frameCount/10);
|
|
32
|
+
graphic.rect(-100, -100, 200, 200);
|
|
33
|
+
graphic.pop();
|
|
34
|
+
image(graphic, 100, 100, 200, 200);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function keyTyped() {
|
|
38
|
+
if(key === "a"){
|
|
39
|
+
startRecording();
|
|
40
|
+
}else if(key === "s"){
|
|
41
|
+
stopRecording();
|
|
42
|
+
}else if(key === "d"){
|
|
43
|
+
pauseRecording();
|
|
44
|
+
}else if(key === "f"){
|
|
45
|
+
resumeRecording();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title>p5.record.js Basic Sketch Recording</title>
|
|
5
|
+
|
|
6
|
+
<script src="https://cdn.jsdelivr.net/npm/p5@2/lib/p5.min.js"></script>
|
|
7
|
+
<script src="../../dist/p5.record.min.js"></script>
|
|
8
|
+
|
|
9
|
+
<style>
|
|
10
|
+
html, body {
|
|
11
|
+
margin: 0;
|
|
12
|
+
}
|
|
13
|
+
</style>
|
|
14
|
+
</head>
|
|
15
|
+
<body>
|
|
16
|
+
<section style="margin: 10px;">
|
|
17
|
+
<button id="start-recording">Start Recording</button>
|
|
18
|
+
<button id="stop-recording">Stop Recording</button>
|
|
19
|
+
<button id="pause-recording">Pause Recording</button>
|
|
20
|
+
<button id="resume-recording">Resume Recording</button>
|
|
21
|
+
</section>
|
|
22
|
+
<script src="./src/sketch.js"></script>
|
|
23
|
+
</body>
|
|
24
|
+
</html>
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
let graphics = [];
|
|
2
|
+
let colors = [];
|
|
3
|
+
let recorders = [];
|
|
4
|
+
let startBtn, stopBtn, pauseBtn, resumeBtn;
|
|
5
|
+
|
|
6
|
+
function setup() {
|
|
7
|
+
createCanvas(400, 400);
|
|
8
|
+
background(200);
|
|
9
|
+
|
|
10
|
+
for(let i=0; i<4; i++){
|
|
11
|
+
graphics.push(createGraphics(400, 400));
|
|
12
|
+
colors.push(color(random(255), random(255), random(255)));
|
|
13
|
+
|
|
14
|
+
const options = {
|
|
15
|
+
source: graphics[i]
|
|
16
|
+
}
|
|
17
|
+
recorders.push(createRecording(options));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
startBtn = select("#start-recording");
|
|
21
|
+
stopBtn = select("#stop-recording");
|
|
22
|
+
pauseBtn = select("#pause-recording");
|
|
23
|
+
resumeBtn = select("#resume-recording");
|
|
24
|
+
|
|
25
|
+
startBtn.mouseClicked(() => {
|
|
26
|
+
recorders.forEach((recorder) => {
|
|
27
|
+
recorder.start();
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
stopBtn.mouseClicked(() => {
|
|
31
|
+
recorders.forEach((recorder) => {
|
|
32
|
+
recorder.stop();
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
pauseBtn.mouseClicked(() => {
|
|
36
|
+
recorders.forEach((recorder) => {
|
|
37
|
+
recorder.pause();
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
resumeBtn.mouseClicked(() => {
|
|
41
|
+
recorders.forEach((recorder) => {
|
|
42
|
+
recorder.resume();
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function draw() {
|
|
48
|
+
line(mouseX, mouseY, pmouseX, pmouseY);
|
|
49
|
+
|
|
50
|
+
for(let j=0; j<2; j++){
|
|
51
|
+
for(let i=0; i<2; i++){
|
|
52
|
+
const graphic = graphics[j*2+i];
|
|
53
|
+
graphic.background(0);
|
|
54
|
+
graphic.fill(colors[j*2+i]);
|
|
55
|
+
graphic.push();
|
|
56
|
+
graphic.translate(200, 200);
|
|
57
|
+
graphic.rotate(frameCount/10);
|
|
58
|
+
graphic.rect(-100, -100, 200, 200);
|
|
59
|
+
graphic.pop();
|
|
60
|
+
|
|
61
|
+
image(graphic, i * 200, j * 200, 200, 200);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function keyTyped() {
|
|
67
|
+
if(key === "a"){
|
|
68
|
+
recorders.forEach((recorder) => {
|
|
69
|
+
recorder.start();
|
|
70
|
+
});
|
|
71
|
+
}else if(key === "s"){
|
|
72
|
+
recorders.forEach((recorder) => {
|
|
73
|
+
recorder.stop();
|
|
74
|
+
});
|
|
75
|
+
}else if(key === "d"){
|
|
76
|
+
recorders.forEach((recorder) => {
|
|
77
|
+
recorder.pause();
|
|
78
|
+
});
|
|
79
|
+
}else if(key === "f"){
|
|
80
|
+
recorders.forEach((recorder) => {
|
|
81
|
+
recorder.resume();
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "p5.record.js",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "p5.js addon providing functions to record HTML canvas based sketches",
|
|
5
|
+
"homepage": "https://github.com/limzykenneth/p5.record.js",
|
|
6
|
+
"bugs": "https://github.com/limzykenneth/p5.record.js/issues",
|
|
7
|
+
"repository": "github:limzykenneth/p5.record.js",
|
|
8
|
+
"author": "Kenneth Lim <hello@limzykenneth.com> (https://limzykenneth.com)",
|
|
9
|
+
"license": "BSD-3-Clause",
|
|
10
|
+
"keywords": [
|
|
11
|
+
"record",
|
|
12
|
+
"recorder",
|
|
13
|
+
"capture",
|
|
14
|
+
"canvas",
|
|
15
|
+
"p5",
|
|
16
|
+
"p5.js",
|
|
17
|
+
"captureStream"
|
|
18
|
+
],
|
|
19
|
+
"type": "module",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": "./dist/p5.record.esm.js"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "rollup -c rollup.config.js",
|
|
25
|
+
"dev": "rollup -c rollup.config.js --watch",
|
|
26
|
+
"prepublishOnly": "npm run build"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
30
|
+
"rollup": "^4.41.1"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/rollup.config.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import terser from "@rollup/plugin-terser";
|
|
2
|
+
|
|
3
|
+
export default [
|
|
4
|
+
{
|
|
5
|
+
input: "src/main.js",
|
|
6
|
+
output: {
|
|
7
|
+
file: "dist/p5.record.min.js",
|
|
8
|
+
format: "iife",
|
|
9
|
+
plugins: [
|
|
10
|
+
process.env.ROLLUP_WATCH ? null : terser()
|
|
11
|
+
]
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
input: "src/main.js",
|
|
16
|
+
output: {
|
|
17
|
+
file: "dist/p5.record.esm.js",
|
|
18
|
+
format: "esm"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
];
|
package/src/Recorder.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
export class Recorder {
|
|
2
|
+
#stream;
|
|
3
|
+
#recorder;
|
|
4
|
+
|
|
5
|
+
constructor(options) {
|
|
6
|
+
const frameRate = options.frameRate === "manual" ? 0 : options.frameRate;
|
|
7
|
+
const chunks = [];
|
|
8
|
+
const stream = (
|
|
9
|
+
options.source instanceof HTMLCanvasElement ?
|
|
10
|
+
options.source :
|
|
11
|
+
options.source.canvas
|
|
12
|
+
).captureStream(frameRate);
|
|
13
|
+
const recorder = new MediaRecorder(stream, {
|
|
14
|
+
mimeType: options.mimeType ?? "video/webm;codecs=vp8"
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
recorder.addEventListener("start", (e) => {
|
|
18
|
+
console.log("recording started");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
recorder.addEventListener("stop", (e) => {
|
|
22
|
+
const blob = new Blob(chunks);
|
|
23
|
+
|
|
24
|
+
const executeDefault = typeof options?.stopCallback === "function" ?
|
|
25
|
+
options?.stopCallback(blob) :
|
|
26
|
+
true;
|
|
27
|
+
|
|
28
|
+
if(executeDefault){
|
|
29
|
+
const blobUrl = URL.createObjectURL(blob);
|
|
30
|
+
const link = document.createElement("a");
|
|
31
|
+
link.href = blobUrl;
|
|
32
|
+
link.download = "recording.webm";
|
|
33
|
+
link.click();
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
recorder.addEventListener("dataavailable", (e) => {
|
|
38
|
+
chunks.push(e.data);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
recorder.addEventListener("pause", (e) => {
|
|
42
|
+
console.log("recording paused");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
recorder.addEventListener("resume", (e) => {
|
|
46
|
+
console.log("recording resumed");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
this.#recorder = recorder;
|
|
50
|
+
this.#stream = stream;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
get state() {
|
|
54
|
+
return this.#recorder.state;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
start() {
|
|
58
|
+
this.#recorder.start();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
stop() {
|
|
62
|
+
this.#recorder.stop();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
pause() {
|
|
66
|
+
this.#recorder.pause();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
resume() {
|
|
70
|
+
this.#recorder.resume();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
frame() {
|
|
74
|
+
this.#stream.getVideoTracks()[0].requestFrame();
|
|
75
|
+
}
|
|
76
|
+
}
|
package/src/main.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Recorder } from "./Recorder.js";
|
|
2
|
+
|
|
3
|
+
export function p5Record(p5, fn, lifecycles){
|
|
4
|
+
let recorder, options;
|
|
5
|
+
|
|
6
|
+
const p5VersionSemver = p5.VERSION.split(".")
|
|
7
|
+
.map((n) => parseInt(n));
|
|
8
|
+
if(!(
|
|
9
|
+
p5VersionSemver[0] > 2 ||
|
|
10
|
+
(p5VersionSemver[0] > 2 && p5VersionSemver[1] > 0) ||
|
|
11
|
+
(p5VersionSemver[0] === 2 && p5VersionSemver[1] === 0 && p5VersionSemver[2] >= 3)
|
|
12
|
+
)){
|
|
13
|
+
console.error(`p5.record.js requires p5.js >= 2.0.3`);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
p5.Recorder = Recorder;
|
|
18
|
+
|
|
19
|
+
lifecycles.postdraw = function() {
|
|
20
|
+
if(recorder && recorder.state === "recording" && options?.frameRate === "manual"){
|
|
21
|
+
recorder.frame();
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
fn.setRecording = function(opt) {
|
|
26
|
+
if(opt.frameRate === "manual" && !("CanvasCaptureMediaStreamTrack" in window)){
|
|
27
|
+
console.error("Your browser does not support directly specifying frame capture timing with { frameRate: 'manual' }.");
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
options = opt;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
fn.startRecording = function() {
|
|
34
|
+
options = Object.assign({
|
|
35
|
+
source: this.canvas,
|
|
36
|
+
frameRate: this.getTargetFrameRate()
|
|
37
|
+
}, options);
|
|
38
|
+
recorder = new Recorder(options);
|
|
39
|
+
recorder.start();
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
fn.stopRecording = function() {
|
|
43
|
+
recorder.stop();
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
fn.pauseRecording = function() {
|
|
47
|
+
recorder.pause();
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
fn.resumeRecording = function() {
|
|
51
|
+
recorder.resume();
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
fn.createRecording = function(options) {
|
|
55
|
+
return new Recorder(options);
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
if(typeof p5 !== "undefined"){
|
|
60
|
+
p5.registerAddon(p5Record);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export { Recorder };
|