@thi.ng/dl-asset 2.2.2 → 2.3.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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2021-11-21T17:09:28Z
3
+ - **Last updated**: 2021-12-13T10:26:00Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,12 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [2.3.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/dl-asset@2.3.0) (2021-12-13)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add canvasVideoRecorder() ([6736463](https://github.com/thi-ng/umbrella/commit/6736463))
17
+
12
18
  ## [2.2.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/dl-asset@2.2.0) (2021-11-17)
13
19
 
14
20
  #### 🚀 Features
package/api.d.ts CHANGED
@@ -21,4 +21,15 @@ export interface DownloadOpts {
21
21
  */
22
22
  expire: number;
23
23
  }
24
+ /**
25
+ * User options for {@link canvasRecorder}.
26
+ */
27
+ export interface CanvasRecorderOpts extends Pick<MediaRecorderOptions, "mimeType" | "bitsPerSecond" | "videoBitsPerSecond"> {
28
+ /**
29
+ * Recording frame rate (fps)
30
+ *
31
+ * @defaultValue 60
32
+ */
33
+ fps: number;
34
+ }
24
35
  //# sourceMappingURL=api.d.ts.map
package/canvas.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { CanvasRecorderOpts } from "./api.js";
1
2
  /**
2
3
  * Triggers canvas-to-blob conversion for given file type (and opt. `quality`),
3
4
  * then triggers download via {@link downloadWithMime}. Default file type is
@@ -9,4 +10,19 @@
9
10
  * @param quality
10
11
  */
11
12
  export declare const downloadCanvas: (canvas: HTMLCanvasElement, baseName: string, type?: "png" | "jpeg" | "webp", quality?: number) => void;
13
+ /**
14
+ * Constructs, initializes and returns a `MediaRecorder` instance for the given
15
+ * canvas. The recording MUST be started & ended manually by the caller (via
16
+ * `.start()` and `.stop()`). The downloading starts only once sufficient frames
17
+ * were recorded, or latest when `.stop()` was called.
18
+ *
19
+ * @remarks
20
+ * The default recording format is WebM (VP9 codec) @ 60 fps, using the browser
21
+ * defined default bit rate.
22
+ *
23
+ * @param canvas
24
+ * @param fileName
25
+ * @param opts
26
+ */
27
+ export declare const canvasRecorder: (canvas: HTMLCanvasElement, fileName: string, opts?: Partial<CanvasRecorderOpts> | undefined) => MediaRecorder;
12
28
  //# sourceMappingURL=canvas.d.ts.map
package/canvas.js CHANGED
@@ -15,3 +15,35 @@ export const downloadCanvas = (canvas, baseName, type = "png", quality = 0.95) =
15
15
  ? downloadWithMime(`${baseName}.${type}`, blob, { mime })
16
16
  : console.warn("can't download canvas"), mime, quality);
17
17
  };
18
+ /**
19
+ * Constructs, initializes and returns a `MediaRecorder` instance for the given
20
+ * canvas. The recording MUST be started & ended manually by the caller (via
21
+ * `.start()` and `.stop()`). The downloading starts only once sufficient frames
22
+ * were recorded, or latest when `.stop()` was called.
23
+ *
24
+ * @remarks
25
+ * The default recording format is WebM (VP9 codec) @ 60 fps, using the browser
26
+ * defined default bit rate.
27
+ *
28
+ * @param canvas
29
+ * @param fileName
30
+ * @param opts
31
+ */
32
+ export const canvasRecorder = (canvas, fileName, opts) => {
33
+ opts = { fps: 60, mimeType: "video/webm; codecs=vp9", ...opts };
34
+ const stream = canvas.captureStream(opts.fps);
35
+ const recorder = new MediaRecorder(stream, {
36
+ mimeType: opts.mimeType,
37
+ });
38
+ let blobs = [];
39
+ recorder.ondataavailable = (e) => {
40
+ if (e.data.size > 0) {
41
+ blobs.push(e.data);
42
+ downloadWithMime(fileName, new Blob(blobs, { type: opts.mimeType }), {
43
+ mime: opts.mimeType,
44
+ });
45
+ blobs = [];
46
+ }
47
+ };
48
+ return recorder;
49
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@thi.ng/dl-asset",
3
- "version": "2.2.2",
4
- "description": "Local asset download for web apps, with automatic MIME type detection",
3
+ "version": "2.3.0",
4
+ "description": "Canvas, video recording & file asset download helpers for web apps",
5
5
  "type": "module",
6
6
  "module": "./index.js",
7
7
  "typings": "./index.d.ts",
@@ -34,17 +34,17 @@
34
34
  "test": "testament test"
35
35
  },
36
36
  "dependencies": {
37
- "@thi.ng/api": "^8.3.2",
38
- "@thi.ng/checks": "^3.1.2",
39
- "@thi.ng/mime": "^2.1.2"
37
+ "@thi.ng/api": "^8.3.3",
38
+ "@thi.ng/checks": "^3.1.3",
39
+ "@thi.ng/mime": "^2.1.3"
40
40
  },
41
41
  "devDependencies": {
42
- "@microsoft/api-extractor": "^7.18.19",
43
- "@thi.ng/testament": "^0.2.2",
42
+ "@microsoft/api-extractor": "^7.19.2",
43
+ "@thi.ng/testament": "^0.2.3",
44
44
  "rimraf": "^3.0.2",
45
45
  "tools": "^0.0.1",
46
- "typedoc": "^0.22.9",
47
- "typescript": "^4.5.2"
46
+ "typedoc": "^0.22.10",
47
+ "typescript": "^4.5.3"
48
48
  },
49
49
  "keywords": [
50
50
  "browser",
@@ -52,7 +52,9 @@
52
52
  "download",
53
53
  "file",
54
54
  "mime",
55
- "typescript"
55
+ "mediarecorder",
56
+ "typescript",
57
+ "video"
56
58
  ],
57
59
  "publishConfig": {
58
60
  "access": "public"
@@ -87,5 +89,5 @@
87
89
  ],
88
90
  "year": 2020
89
91
  },
90
- "gitHead": "e8a7c2a40191b391cef182c2978e5a6c85987a87\n"
92
+ "gitHead": "2db9dd34c0c2c60cbfde3dad0bca352b20292f5c\n"
91
93
  }