@thi.ng/dl-asset 2.2.1 → 2.3.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.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2021-11-19T07:59:50Z
3
+ - **Last updated**: 2022-03-11T12:13:49Z
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/README.md CHANGED
@@ -23,7 +23,7 @@ This project is part of the
23
23
 
24
24
  ## About
25
25
 
26
- Local asset download for web apps, with automatic MIME type detection.
26
+ Canvas, video recording & file asset download helpers for web apps.
27
27
 
28
28
  ### Status
29
29
 
@@ -58,7 +58,7 @@ node --experimental-repl-await
58
58
  > const dlAsset = await import("@thi.ng/dl-asset");
59
59
  ```
60
60
 
61
- Package sizes (gzipped, pre-treeshake): ESM: 516 bytes
61
+ Package sizes (gzipped, pre-treeshake): ESM: 648 bytes
62
62
 
63
63
  ## Dependencies
64
64
 
@@ -158,4 +158,4 @@ If this project contributes to an academic publication, please cite it as:
158
158
 
159
159
  ## License
160
160
 
161
- © 2020 - 2021 Karsten Schmidt // Apache Software License 2.0
161
+ © 2020 - 2022 Karsten Schmidt // Apache Software License 2.0
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,12 +1,28 @@
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
4
5
  * `png`. Default quality is 0.95 (only used for JPEG/WebP).
5
6
  *
6
- * @param canvas
7
- * @param baseName
8
- * @param type
9
- * @param quality
7
+ * @param canvas -
8
+ * @param baseName -
9
+ * @param type -
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
@@ -4,10 +4,10 @@ import { downloadWithMime } from "./raw.js";
4
4
  * then triggers download via {@link downloadWithMime}. Default file type is
5
5
  * `png`. Default quality is 0.95 (only used for JPEG/WebP).
6
6
  *
7
- * @param canvas
8
- * @param baseName
9
- * @param type
10
- * @param quality
7
+ * @param canvas -
8
+ * @param baseName -
9
+ * @param type -
10
+ * @param quality -
11
11
  */
12
12
  export const downloadCanvas = (canvas, baseName, type = "png", quality = 0.95) => {
13
13
  const mime = `image/${type}`;
@@ -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/download.d.ts CHANGED
@@ -6,9 +6,9 @@ import type { DownloadOpts } from "./api.js";
6
6
  * derive it from the given filename's extension (e.g. `.svg`) and if
7
7
  * that fails, falls back to default value.
8
8
  *
9
- * @param name
10
- * @param src
11
- * @param opts
9
+ * @param name -
10
+ * @param src -
11
+ * @param opts -
12
12
  */
13
13
  export declare const download: (name: string, src: string | TypedArray | ArrayBuffer | Blob, opts?: Partial<DownloadOpts>) => void;
14
14
  //# sourceMappingURL=download.d.ts.map
package/download.js CHANGED
@@ -6,9 +6,9 @@ import { downloadWithMime } from "./raw.js";
6
6
  * derive it from the given filename's extension (e.g. `.svg`) and if
7
7
  * that fails, falls back to default value.
8
8
  *
9
- * @param name
10
- * @param src
11
- * @param opts
9
+ * @param name -
10
+ * @param src -
11
+ * @param opts -
12
12
  */
13
13
  export const download = (name, src, opts = {}) => {
14
14
  if (opts.mime === undefined) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@thi.ng/dl-asset",
3
- "version": "2.2.1",
4
- "description": "Local asset download for web apps, with automatic MIME type detection",
3
+ "version": "2.3.1",
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.1",
38
- "@thi.ng/checks": "^3.1.1",
39
- "@thi.ng/mime": "^2.1.1"
37
+ "@thi.ng/api": "^8.3.4",
38
+ "@thi.ng/checks": "^3.1.4",
39
+ "@thi.ng/mime": "^2.1.4"
40
40
  },
41
41
  "devDependencies": {
42
- "@microsoft/api-extractor": "^7.18.19",
43
- "@thi.ng/testament": "^0.2.1",
42
+ "@microsoft/api-extractor": "^7.19.4",
43
+ "@thi.ng/testament": "^0.2.4",
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.13",
47
+ "typescript": "^4.6.2"
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": "8bd27c8bde0b770e7c001943f11c951cd345d668\n"
92
+ "gitHead": "0fc692a3225c068aacafdc4cb6140cf603c67ad8\n"
91
93
  }
package/raw.d.ts CHANGED
@@ -18,9 +18,9 @@ import type { DownloadOpts } from "./api.js";
18
18
  * again after `expire` millseconds (default: 10000) to free up memory.
19
19
  * The URL won't be expired if `expire <= 0`.
20
20
  *
21
- * @param name
22
- * @param src
23
- * @param opts
21
+ * @param name -
22
+ * @param src -
23
+ * @param opts -
24
24
  */
25
25
  export declare const downloadWithMime: (name: string, src: string | TypedArray | ArrayBuffer | Blob, opts: Partial<DownloadOpts> & {
26
26
  mime: string;
package/raw.js CHANGED
@@ -17,9 +17,9 @@ import { isString } from "@thi.ng/checks/is-string";
17
17
  * again after `expire` millseconds (default: 10000) to free up memory.
18
18
  * The URL won't be expired if `expire <= 0`.
19
19
  *
20
- * @param name
21
- * @param src
22
- * @param opts
20
+ * @param name -
21
+ * @param src -
22
+ * @param opts -
23
23
  */
24
24
  export const downloadWithMime = (name, src, opts) => {
25
25
  const _opts = {