p5.record.js 0.1.0 → 0.1.1

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.
@@ -0,0 +1,137 @@
1
+ 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
+ }
77
+
78
+ function p5Record(p5, fn, lifecycles){
79
+ let recorder, options;
80
+
81
+ const p5VersionSemver = p5.VERSION.split(".")
82
+ .map((n) => parseInt(n));
83
+ if(!(
84
+ p5VersionSemver[0] > 2 ||
85
+ (p5VersionSemver[0] > 2 && p5VersionSemver[1] > 0) ||
86
+ (p5VersionSemver[0] === 2 && p5VersionSemver[1] === 0 && p5VersionSemver[2] >= 3)
87
+ )){
88
+ console.error(`p5.record.js requires p5.js >= 2.0.3`);
89
+ return;
90
+ }
91
+
92
+ p5.Recorder = Recorder;
93
+
94
+ lifecycles.postdraw = function() {
95
+ if(recorder && recorder.state === "recording" && options?.frameRate === "manual"){
96
+ recorder.frame();
97
+ }
98
+ };
99
+
100
+ fn.setRecording = function(opt) {
101
+ if(opt.frameRate === "manual" && !("CanvasCaptureMediaStreamTrack" in window)){
102
+ console.error("Your browser does not support directly specifying frame capture timing with { frameRate: 'manual' }.");
103
+ return;
104
+ }
105
+ options = opt;
106
+ };
107
+
108
+ fn.startRecording = function() {
109
+ options = Object.assign({
110
+ source: this.canvas,
111
+ frameRate: this.getTargetFrameRate()
112
+ }, options);
113
+ recorder = new Recorder(options);
114
+ recorder.start();
115
+ };
116
+
117
+ fn.stopRecording = function() {
118
+ recorder.stop();
119
+ };
120
+
121
+ fn.pauseRecording = function() {
122
+ recorder.pause();
123
+ };
124
+
125
+ fn.resumeRecording = function() {
126
+ recorder.resume();
127
+ };
128
+
129
+ fn.createRecording = function(options) {
130
+ return new Recorder(options);
131
+ };
132
+ }
133
+ if(typeof p5 !== "undefined"){
134
+ p5.registerAddon(p5Record);
135
+ }
136
+
137
+ export { Recorder, p5Record };
@@ -0,0 +1 @@
1
+ !function(e){"use strict";class r{#e;#r;constructor(e){const r="manual"===e.frameRate?0:e.frameRate,t=[],a=(e.source instanceof HTMLCanvasElement?e.source:e.source.canvas).captureStream(r),s=new MediaRecorder(a,{mimeType:e.mimeType??"video/webm;codecs=vp8"});s.addEventListener("start",e=>{console.log("recording started")}),s.addEventListener("stop",r=>{const a=new Blob(t);if("function"!=typeof e?.stopCallback||e?.stopCallback(a)){const e=URL.createObjectURL(a),r=document.createElement("a");r.href=e,r.download="recording.webm",r.click()}}),s.addEventListener("dataavailable",e=>{t.push(e.data)}),s.addEventListener("pause",e=>{console.log("recording paused")}),s.addEventListener("resume",e=>{console.log("recording resumed")}),this.#r=s,this.#e=a}get state(){return this.#r.state}start(){this.#r.start()}stop(){this.#r.stop()}pause(){this.#r.pause()}resume(){this.#r.resume()}frame(){this.#e.getVideoTracks()[0].requestFrame()}}function t(e,t,a){let s,o;const n=e.VERSION.split(".").map(e=>parseInt(e));n[0]>2||n[0]>2&&n[1]>0||2===n[0]&&0===n[1]&&n[2]>=3?(e.Recorder=r,a.postdraw=function(){s&&"recording"===s.state&&"manual"===o?.frameRate&&s.frame()},t.setRecording=function(e){"manual"!==e.frameRate||"CanvasCaptureMediaStreamTrack"in window?o=e:console.error("Your browser does not support directly specifying frame capture timing with { frameRate: 'manual' }.")},t.startRecording=function(){o=Object.assign({source:this.canvas,frameRate:this.getTargetFrameRate()},o),s=new r(o),s.start()},t.stopRecording=function(){s.stop()},t.pauseRecording=function(){s.pause()},t.resumeRecording=function(){s.resume()},t.createRecording=function(e){return new r(e)}):console.error("p5.record.js requires p5.js >= 2.0.3")}"undefined"!=typeof p5&&p5.registerAddon(t),e.Recorder=r,e.p5Record=t}({});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "p5.record.js",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "p5.js addon providing functions to record HTML canvas based sketches",
5
5
  "homepage": "https://github.com/limzykenneth/p5.record.js",
6
6
  "bugs": "https://github.com/limzykenneth/p5.record.js/issues",
@@ -16,6 +16,10 @@
16
16
  "p5.js",
17
17
  "captureStream"
18
18
  ],
19
+ "files": [
20
+ "dist/*",
21
+ "src/*"
22
+ ],
19
23
  "type": "module",
20
24
  "exports": {
21
25
  ".": "./dist/p5.record.esm.js"
@@ -1,24 +0,0 @@
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>
@@ -1,32 +0,0 @@
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
- }
@@ -1,24 +0,0 @@
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>
@@ -1,37 +0,0 @@
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
- }
@@ -1,24 +0,0 @@
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>
@@ -1,47 +0,0 @@
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
- }
@@ -1,24 +0,0 @@
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>
@@ -1,84 +0,0 @@
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/rollup.config.js DELETED
@@ -1,21 +0,0 @@
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
- ];