p5.record.js 0.1.1 → 0.1.2-beta.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "p5.record.js",
3
- "version": "0.1.1",
3
+ "version": "0.1.2-beta.0",
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",
@@ -24,6 +24,7 @@
24
24
  "exports": {
25
25
  ".": "./dist/p5.record.esm.js"
26
26
  },
27
+ "browser": "./dist/p5.record.min.js",
27
28
  "scripts": {
28
29
  "build": "rollup -c rollup.config.js",
29
30
  "dev": "rollup -c rollup.config.js --watch",
@@ -0,0 +1,67 @@
1
+ class SequenceRecorder extends EventTarget {
2
+ #counter = 0;
3
+ #source;
4
+ #images = [];
5
+ #animationFrame;
6
+ #timePerFrame;
7
+ #accumulator;
8
+ state = "inactive";
9
+
10
+ constructor(source, frameRate = 60) {
11
+ this.#source = source;
12
+ this.#timePerFrame = 1000 / frameRate;
13
+ }
14
+
15
+ start() {
16
+ state = "recording";
17
+ this.dispatchEvent(new Event("start"));
18
+ this.frame();
19
+ }
20
+
21
+ stop() {
22
+ state = "inactive";
23
+ if(this.#animationFrame){
24
+ cancelAnimationFrame(this.#animationFrame);
25
+ }
26
+
27
+ // NOTE: Compression Streams API creates raw compressed streams.
28
+ // Need some implementation to makie it zip file, probably headers
29
+ // const compressor = new CompressionStream("deflate");
30
+ // for(let image of this.#images){
31
+ // image.stream().pipeThrough(compressor);
32
+ // }
33
+
34
+ this.dispatchEvent(new Event("stop"));
35
+ }
36
+
37
+ pause() {
38
+ state = "paused";
39
+ if(this.#animationFrame){
40
+ cancelAnimationFrame(this.#animationFrame);
41
+ }
42
+ this.dispatchEvent(new Event("pause"));
43
+ }
44
+
45
+ resume() {
46
+ state = "recording";
47
+ this.dispatchEvent(new Event("resume"));
48
+ this.frame();
49
+ }
50
+
51
+ frame(deltaTime) {
52
+ let c = this.#counter;
53
+ this.#source.toBlob((blob) => {
54
+ this.#images[c] = blob;
55
+ });
56
+ this.#counter++;
57
+
58
+ if(state === "recording"){
59
+ this.#accumulator += deltaTime;
60
+ if(this.accumulator >= this.#timePerFrame){
61
+ this.#animationFrame = requestAnimationFrame(this.frame);
62
+ }else{
63
+ this.#animationFrame = requestAnimationFrame(() => {});
64
+ }
65
+ }
66
+ }
67
+ }