captions.js 0.4.0 → 0.4.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/README.md CHANGED
@@ -1,92 +1,39 @@
1
- # 📖 captions.js Monorepo Scripts Cheat Sheet
1
+ > [!WARNING]
2
+ > 🚧 **Captions.js is nearing its first public release**
3
+ > The library is under active development and a full-featured version is expected around **November 2025**.
2
4
 
3
- This cheat sheet summarizes all the key scripts for working with the `captions.js` monorepo. Use `pnpm` to run scripts in individual packages.
5
+ 💌 Want to know when Captions.js is released?
6
+ Star or watch the repo on GitHub to get updates!
4
7
 
5
- ---
8
+ # Captions.js
6
9
 
7
- ## 🌟 General `pnpm` Commands
10
+ Render styled animated captions on canvas — works both on client and server side using Node.js and FFmpeg.
8
11
 
9
- | Command | Description |
10
- | ------------------------------------------- | ------------------------------------------------------------ |
11
- | `pnpm install` | Installs all dependencies for all packages in the monorepo. |
12
- | `pnpm --filter <package> run <script>` | Runs a script `<script>` for a specific package `<package>`. |
13
- | `pnpm build` | Builds all packages that have a `build` script. |
14
- | `pnpm clean` | Cleans build artifacts if a `clean` script is defined. |
12
+ [**Live Demo**](https://maskin25.github.io/captions.js/)
15
13
 
16
- > **Note:** `<package>` refers to the package name in `packages/`, e.g., `@captions/core`, `captions-demo`, or `captions-storybook`.
14
+ [**Docs & API Reference**](https://maskin25.github.io/captions.js/docs/)
17
15
 
18
- ---
16
+ <!-- [![Storybook](https://raw.githubusercontent.com/storybookjs/brand/refs/heads/main/badge/badge-storybook.svg)](https://main--68e681805917843931c33a87.chromatic.com/) -->
19
17
 
20
- ## 📦 Package: `@captions/core`
18
+ ## Features
21
19
 
22
- | Script | Command | Description |
23
- | ------- | ----------------------------------------- | ------------------------------------------------------------------------ |
24
- | `build` | `pnpm --filter @captions/core run build` | Builds the library using `tsup` (CJS, ESM, and TypeScript declarations). |
25
- | `clean` | `pnpm --filter @captions/core run clean` | Deletes the build folder (`dist`). |
20
+ - **Easy Integration**: Simple API to add captions to your videos.
21
+ - **Customizable Styles**: Fully customizable caption styles via configurations.
22
+ - **Multiple Formats**: Supports various caption formats including WebVTT and SRT.
23
+ - **Responsive Design**: Captions adapt to different screen sizes and orientations.
26
24
 
27
- **Example:**
28
- ```bash
29
- pnpm --filter @captions/core run build
30
- ```
31
-
32
- ---
25
+ ## Installation
33
26
 
34
- ## 🎨 Demo App: `captions-demo`
27
+ To install Captions.js, use npm or yarn:
35
28
 
36
- | Script | Command | Description |
37
- | --------- | --------------------------------------- | ------------------------------------------------------------------ |
38
- | `dev` | `pnpm --filter captions-demo run dev` | Runs the demo locally with Vite (default: `http://localhost:5173`). |
39
- | `build` | `pnpm --filter captions-demo run build` | Builds the demo for production into the `dist` folder. |
40
- | `preview` | `pnpm --filter captions-demo run preview` | Serves the built demo locally for preview. |
41
-
42
- **Example:**
43
29
  ```bash
44
- pnpm --filter captions-demo run dev
30
+ npm install captions.js
45
31
  ```
46
32
 
47
- ---
48
-
49
- ## 📚 Storybook: `captions-storybook`
50
-
51
- | Script | Command | Description |
52
- | ----------------- | ------------------------------------------------- | ---------------------------------------------------------- |
53
- | `storybook` | `pnpm --filter captions-storybook run storybook` | Runs Storybook locally with Vite (`http://localhost:6006`). |
54
- | `build-storybook` | `pnpm --filter captions-storybook run build-storybook` | Builds a static Storybook site into `storybook-static`. |
55
- | `deploy` | `pnpm --filter captions-storybook run deploy` | Builds and deploys Storybook to GitHub Pages. |
33
+ or
56
34
 
57
- **Example:**
58
35
  ```bash
59
- pnpm --filter captions-storybook run storybook
36
+ yarn add captions.js
60
37
  ```
61
38
 
62
- ---
63
-
64
- ## ⚡ Tips & Recommendations
65
-
66
- 1. **Targeted Scripts:** Always run scripts using `pnpm --filter` to ensure you're using the correct local binaries for that package.
67
- ```bash
68
- pnpm --filter <package> run <script>
69
- ```
70
-
71
- 2. **Troubleshooting:** If you encounter issues after updating packages, run this sequence to perform a clean reinstall:
72
- ```bash
73
- # Clean up all node_modules and lockfile
74
- rm -rf node_modules packages/*/node_modules
75
- rm pnpm-lock.yaml
76
-
77
- # Prune the pnpm store and reinstall
78
- pnpm store prune
79
- pnpm install
80
- ```
81
-
82
- 3. **Global Build Script (Optional):** For convenience, you can add a global `build` script to the root `package.json` to build the entire monorepo with one command.
83
-
84
- ```json
85
- "scripts": {
86
- "build": "pnpm --filter @captions/core run build && pnpm --filter captions-demo run build && pnpm --filter captions-storybook run build-storybook"
87
- }
88
- ```
89
- Then, you can simply run:
90
- ```bash
91
- pnpm run build
92
- ```
39
+ For development see [DEVELOPMENT.md](./DEVELOPMENT.md).
package/dist/index.d.mts CHANGED
@@ -1,3 +1,236 @@
1
+ /**
2
+ * Whitelisted Google Fonts that the renderer knows how to lazy-load.
3
+ *
4
+ * @remarks
5
+ * Restricting the list keeps bundle size predictable across consumers.
6
+ *
7
+ * @public
8
+ */
9
+ declare const googleFontsList: readonly ["Roboto", "Open Sans", "Lato", "Montserrat", "Nunito", "Poppins", "Ubuntu", "PT Sans", "Merriweather Sans", "Lobster", "Amatic SC", "Pacifico", "Raleway", "Cinzel", "Quicksand", "Zilla Slab", "Caveat", "Crimson Pro", "Bebas Neue", "Comfortaa", "Satisfy", "Permanent Marker", "Oswald", "Onset", "Bangers", "Kanit", "Work Sans", "Fira Sans", "Anton", "Playfair Display", "Rubik", "Alumni Sans", "Righteous", "Comico", "Excon", "Kalam", "Tanker", "Arsenal", "Balsamiq Sans", "Bona Nova SC"];
10
+
11
+ /**
12
+ * Shared schema describing how a preset styles captions plus rough layout hints.
13
+ *
14
+ * @public
15
+ */
16
+ interface StylePreset {
17
+ id: number;
18
+ captionsSettings: {
19
+ style: {
20
+ font: {
21
+ italic: boolean;
22
+ fontSize: number;
23
+ fontColor: string;
24
+ underline: boolean;
25
+ fontFamily: string;
26
+ fontWeight: string;
27
+ fontCapitalize: boolean;
28
+ fontStrokeColor: string;
29
+ fontStrokeWidth: number;
30
+ shadow?: {
31
+ fontShadowBlur: number;
32
+ fontShadowColor: string;
33
+ fontShadowOffsetX: number;
34
+ fontShadowOffsetY: number;
35
+ };
36
+ };
37
+ name: string;
38
+ backgroundColor: string;
39
+ verticalCoverImg: string;
40
+ aplifiedWordColor: string;
41
+ };
42
+ position: string;
43
+ animation: string;
44
+ linesPerPage: number;
45
+ positionTopOffset?: number;
46
+ };
47
+ layoutSettings: {
48
+ aspectRatio: string;
49
+ aIAutoLayout: string[];
50
+ fitLayoutAspectRatio: string;
51
+ };
52
+ }
53
+ /**
54
+ * Curated set of presets that ship with captions.js out of the box.
55
+ *
56
+ * @remarks
57
+ * Downstream apps can reference them directly or clone/extend as needed.
58
+ *
59
+ * @public
60
+ */
61
+ declare const stylePresets: StylePreset[];
62
+
63
+ /**
64
+ * Server/worker-friendly helper that paints a text string onto a provided canvas.
65
+ *
66
+ * @remarks
67
+ * Uses the same Konva pipeline as the video overlay renderer so the results
68
+ * match what users see in the browser.
69
+ *
70
+ * @param canvas - Destination canvas that should receive the rendered text.
71
+ * @param text - Arbitrary content that needs to be painted.
72
+ * @param options - Rendering options that include the style preset.
73
+ * @returns Resolves once the frame has been painted (always resolves to `true`).
74
+ */
75
+ declare function renderString(canvas: HTMLCanvasElement, text: string, options: {
76
+ preset: StylePreset;
77
+ }): Promise<boolean>;
78
+
79
+ /**
80
+ * Full styling + animation configuration for a single captions track.
81
+ *
82
+ * @public
83
+ */
84
+ interface CaptionsSettings {
85
+ style: {
86
+ name: string;
87
+ font: {
88
+ fontFamily: string;
89
+ fontSize: number;
90
+ fontWeight: "thin" | "light" | "regular" | "medium" | "bold" | "black";
91
+ fontColor: string;
92
+ fontCapitalize: boolean;
93
+ italic: boolean;
94
+ underline: boolean;
95
+ fontStrokeColor: string;
96
+ fontStrokeWidth: number;
97
+ shadow: {
98
+ fontShadowColor: string;
99
+ fontShadowBlur: number;
100
+ fontShadowOffsetX: number;
101
+ fontShadowOffsetY: number;
102
+ };
103
+ };
104
+ aplifiedWordColor: string;
105
+ backgroundColor: string;
106
+ };
107
+ linesPerPage: number;
108
+ lineSpacing?: number | null;
109
+ position: "auto" | "top" | "middle" | "bottom";
110
+ positionTopOffset: number;
111
+ animation: "none" | "bounce" | "underline" | "box" | "pop" | "scale" | "slide-left" | "slide-up" | "slide-down" | "box-word";
112
+ }
113
+ /**
114
+ * Single timed word/segment that will be highlighted as audio plays.
115
+ *
116
+ * @public
117
+ */
118
+ interface Caption {
119
+ word: string;
120
+ startTime: number;
121
+ endTime: number;
122
+ highlightColor?: string;
123
+ }
124
+
125
+ /**
126
+ * Configuration passed to the captions runtime when binding to a video element.
127
+ *
128
+ * @public
129
+ */
130
+ type CaptionsOptions = {
131
+ /** Video element that should receive overlays. */
132
+ video: HTMLVideoElement;
133
+ /** Optional custom container to host the Konva stage. */
134
+ container?: HTMLDivElement;
135
+ /** Initial preset controlling font, colors, animations. */
136
+ preset: StylePreset;
137
+ /** Initial caption track. */
138
+ captions?: Caption[] | null;
139
+ /** When false, caller must invoke {@link Captions.enable} manually. */
140
+ autoEnable?: boolean;
141
+ /** Show debug bounding boxes around lines/blocks. */
142
+ debug?: boolean;
143
+ };
144
+ /**
145
+ * Imperative controller that owns the Konva stage lifecycle for a single video element.
146
+ *
147
+ * @public
148
+ */
149
+ declare class Captions {
150
+ private enabled;
151
+ private readonly video;
152
+ private readonly providedContainer?;
153
+ private presetState;
154
+ private captionsState;
155
+ private containerElement?;
156
+ private ownsContainer;
157
+ private stage;
158
+ private layer;
159
+ private resizeObserver?;
160
+ private animationFrameId;
161
+ private videoWidth;
162
+ private videoHeight;
163
+ private readonly debug;
164
+ private readonly handleResize;
165
+ private readonly handleMetadata;
166
+ private readonly animationLoop;
167
+ /**
168
+ * Create a controller bound to the provided video element and preset.
169
+ *
170
+ * @param options - Complete configuration for the controller.
171
+ */
172
+ constructor(options: CaptionsOptions);
173
+ /**
174
+ * Mount caption overlays onto the configured video if they are not active yet.
175
+ */
176
+ enable(): void;
177
+ /**
178
+ * Tear down overlays, observers and animation loops to free resources.
179
+ */
180
+ disable(): void;
181
+ /**
182
+ * Alias for {@link Captions.disable | disable()} to match typical imperative controller APIs.
183
+ */
184
+ destroy(): void;
185
+ /**
186
+ * Swap the active preset and re-render with updated typography/colors.
187
+ *
188
+ * @param nextPreset - Preset that becomes the new render baseline.
189
+ */
190
+ preset(nextPreset: StylePreset): void;
191
+ /**
192
+ * Replace the current caption track and repaint without reloading fonts.
193
+ *
194
+ * @param nextCaptions - Timed words that should drive the overlay.
195
+ */
196
+ captions(nextCaptions: Caption[] | null): void;
197
+ /**
198
+ * Whether the Konva overlay is currently attached to the video element.
199
+ *
200
+ * @returns `true` when the overlay is mounted on top of the video.
201
+ */
202
+ isEnabled(): boolean;
203
+ private refreshFrame;
204
+ private loadFontForCurrentPreset;
205
+ private updateFrame;
206
+ private syncStageDimensions;
207
+ private createOverlay;
208
+ }
209
+ /**
210
+ * Convenience alias for the concrete controller class.
211
+ *
212
+ * @public
213
+ */
214
+ type CaptionsInstance = Captions;
215
+ /**
216
+ * Factory mirroring the legacy default export for ergonomic imports.
217
+ *
218
+ * @param options - Same options accepted by the {@link Captions} constructor.
219
+ * @returns New controller instance.
220
+ */
221
+ declare function captionsjs(options: CaptionsOptions): Captions;
222
+
223
+ /**
224
+ * Simple canvas demo renderer used only for the docs playground.
225
+ *
226
+ * @remarks
227
+ * Keeps a reference implementation of drawing raw text to a canvas so we can
228
+ * showcase caption styling without a video element.
229
+ *
230
+ * @param ctx - Target 2D context to draw on.
231
+ * @param text - Arbitrary string that should be painted on the canvas.
232
+ * @returns Always returns `true` to match the historical API surface.
233
+ */
1
234
  declare function renderCaptions(ctx: CanvasRenderingContext2D, text: string): boolean;
2
235
 
3
- export { renderCaptions };
236
+ export { type Caption, Captions, type CaptionsInstance, type CaptionsOptions, type CaptionsSettings, type StylePreset, captionsjs, captionsjs as default, googleFontsList, renderCaptions, renderString, stylePresets };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,236 @@
1
+ /**
2
+ * Whitelisted Google Fonts that the renderer knows how to lazy-load.
3
+ *
4
+ * @remarks
5
+ * Restricting the list keeps bundle size predictable across consumers.
6
+ *
7
+ * @public
8
+ */
9
+ declare const googleFontsList: readonly ["Roboto", "Open Sans", "Lato", "Montserrat", "Nunito", "Poppins", "Ubuntu", "PT Sans", "Merriweather Sans", "Lobster", "Amatic SC", "Pacifico", "Raleway", "Cinzel", "Quicksand", "Zilla Slab", "Caveat", "Crimson Pro", "Bebas Neue", "Comfortaa", "Satisfy", "Permanent Marker", "Oswald", "Onset", "Bangers", "Kanit", "Work Sans", "Fira Sans", "Anton", "Playfair Display", "Rubik", "Alumni Sans", "Righteous", "Comico", "Excon", "Kalam", "Tanker", "Arsenal", "Balsamiq Sans", "Bona Nova SC"];
10
+
11
+ /**
12
+ * Shared schema describing how a preset styles captions plus rough layout hints.
13
+ *
14
+ * @public
15
+ */
16
+ interface StylePreset {
17
+ id: number;
18
+ captionsSettings: {
19
+ style: {
20
+ font: {
21
+ italic: boolean;
22
+ fontSize: number;
23
+ fontColor: string;
24
+ underline: boolean;
25
+ fontFamily: string;
26
+ fontWeight: string;
27
+ fontCapitalize: boolean;
28
+ fontStrokeColor: string;
29
+ fontStrokeWidth: number;
30
+ shadow?: {
31
+ fontShadowBlur: number;
32
+ fontShadowColor: string;
33
+ fontShadowOffsetX: number;
34
+ fontShadowOffsetY: number;
35
+ };
36
+ };
37
+ name: string;
38
+ backgroundColor: string;
39
+ verticalCoverImg: string;
40
+ aplifiedWordColor: string;
41
+ };
42
+ position: string;
43
+ animation: string;
44
+ linesPerPage: number;
45
+ positionTopOffset?: number;
46
+ };
47
+ layoutSettings: {
48
+ aspectRatio: string;
49
+ aIAutoLayout: string[];
50
+ fitLayoutAspectRatio: string;
51
+ };
52
+ }
53
+ /**
54
+ * Curated set of presets that ship with captions.js out of the box.
55
+ *
56
+ * @remarks
57
+ * Downstream apps can reference them directly or clone/extend as needed.
58
+ *
59
+ * @public
60
+ */
61
+ declare const stylePresets: StylePreset[];
62
+
63
+ /**
64
+ * Server/worker-friendly helper that paints a text string onto a provided canvas.
65
+ *
66
+ * @remarks
67
+ * Uses the same Konva pipeline as the video overlay renderer so the results
68
+ * match what users see in the browser.
69
+ *
70
+ * @param canvas - Destination canvas that should receive the rendered text.
71
+ * @param text - Arbitrary content that needs to be painted.
72
+ * @param options - Rendering options that include the style preset.
73
+ * @returns Resolves once the frame has been painted (always resolves to `true`).
74
+ */
75
+ declare function renderString(canvas: HTMLCanvasElement, text: string, options: {
76
+ preset: StylePreset;
77
+ }): Promise<boolean>;
78
+
79
+ /**
80
+ * Full styling + animation configuration for a single captions track.
81
+ *
82
+ * @public
83
+ */
84
+ interface CaptionsSettings {
85
+ style: {
86
+ name: string;
87
+ font: {
88
+ fontFamily: string;
89
+ fontSize: number;
90
+ fontWeight: "thin" | "light" | "regular" | "medium" | "bold" | "black";
91
+ fontColor: string;
92
+ fontCapitalize: boolean;
93
+ italic: boolean;
94
+ underline: boolean;
95
+ fontStrokeColor: string;
96
+ fontStrokeWidth: number;
97
+ shadow: {
98
+ fontShadowColor: string;
99
+ fontShadowBlur: number;
100
+ fontShadowOffsetX: number;
101
+ fontShadowOffsetY: number;
102
+ };
103
+ };
104
+ aplifiedWordColor: string;
105
+ backgroundColor: string;
106
+ };
107
+ linesPerPage: number;
108
+ lineSpacing?: number | null;
109
+ position: "auto" | "top" | "middle" | "bottom";
110
+ positionTopOffset: number;
111
+ animation: "none" | "bounce" | "underline" | "box" | "pop" | "scale" | "slide-left" | "slide-up" | "slide-down" | "box-word";
112
+ }
113
+ /**
114
+ * Single timed word/segment that will be highlighted as audio plays.
115
+ *
116
+ * @public
117
+ */
118
+ interface Caption {
119
+ word: string;
120
+ startTime: number;
121
+ endTime: number;
122
+ highlightColor?: string;
123
+ }
124
+
125
+ /**
126
+ * Configuration passed to the captions runtime when binding to a video element.
127
+ *
128
+ * @public
129
+ */
130
+ type CaptionsOptions = {
131
+ /** Video element that should receive overlays. */
132
+ video: HTMLVideoElement;
133
+ /** Optional custom container to host the Konva stage. */
134
+ container?: HTMLDivElement;
135
+ /** Initial preset controlling font, colors, animations. */
136
+ preset: StylePreset;
137
+ /** Initial caption track. */
138
+ captions?: Caption[] | null;
139
+ /** When false, caller must invoke {@link Captions.enable} manually. */
140
+ autoEnable?: boolean;
141
+ /** Show debug bounding boxes around lines/blocks. */
142
+ debug?: boolean;
143
+ };
144
+ /**
145
+ * Imperative controller that owns the Konva stage lifecycle for a single video element.
146
+ *
147
+ * @public
148
+ */
149
+ declare class Captions {
150
+ private enabled;
151
+ private readonly video;
152
+ private readonly providedContainer?;
153
+ private presetState;
154
+ private captionsState;
155
+ private containerElement?;
156
+ private ownsContainer;
157
+ private stage;
158
+ private layer;
159
+ private resizeObserver?;
160
+ private animationFrameId;
161
+ private videoWidth;
162
+ private videoHeight;
163
+ private readonly debug;
164
+ private readonly handleResize;
165
+ private readonly handleMetadata;
166
+ private readonly animationLoop;
167
+ /**
168
+ * Create a controller bound to the provided video element and preset.
169
+ *
170
+ * @param options - Complete configuration for the controller.
171
+ */
172
+ constructor(options: CaptionsOptions);
173
+ /**
174
+ * Mount caption overlays onto the configured video if they are not active yet.
175
+ */
176
+ enable(): void;
177
+ /**
178
+ * Tear down overlays, observers and animation loops to free resources.
179
+ */
180
+ disable(): void;
181
+ /**
182
+ * Alias for {@link Captions.disable | disable()} to match typical imperative controller APIs.
183
+ */
184
+ destroy(): void;
185
+ /**
186
+ * Swap the active preset and re-render with updated typography/colors.
187
+ *
188
+ * @param nextPreset - Preset that becomes the new render baseline.
189
+ */
190
+ preset(nextPreset: StylePreset): void;
191
+ /**
192
+ * Replace the current caption track and repaint without reloading fonts.
193
+ *
194
+ * @param nextCaptions - Timed words that should drive the overlay.
195
+ */
196
+ captions(nextCaptions: Caption[] | null): void;
197
+ /**
198
+ * Whether the Konva overlay is currently attached to the video element.
199
+ *
200
+ * @returns `true` when the overlay is mounted on top of the video.
201
+ */
202
+ isEnabled(): boolean;
203
+ private refreshFrame;
204
+ private loadFontForCurrentPreset;
205
+ private updateFrame;
206
+ private syncStageDimensions;
207
+ private createOverlay;
208
+ }
209
+ /**
210
+ * Convenience alias for the concrete controller class.
211
+ *
212
+ * @public
213
+ */
214
+ type CaptionsInstance = Captions;
215
+ /**
216
+ * Factory mirroring the legacy default export for ergonomic imports.
217
+ *
218
+ * @param options - Same options accepted by the {@link Captions} constructor.
219
+ * @returns New controller instance.
220
+ */
221
+ declare function captionsjs(options: CaptionsOptions): Captions;
222
+
223
+ /**
224
+ * Simple canvas demo renderer used only for the docs playground.
225
+ *
226
+ * @remarks
227
+ * Keeps a reference implementation of drawing raw text to a canvas so we can
228
+ * showcase caption styling without a video element.
229
+ *
230
+ * @param ctx - Target 2D context to draw on.
231
+ * @param text - Arbitrary string that should be painted on the canvas.
232
+ * @returns Always returns `true` to match the historical API surface.
233
+ */
1
234
  declare function renderCaptions(ctx: CanvasRenderingContext2D, text: string): boolean;
2
235
 
3
- export { renderCaptions };
236
+ export { type Caption, Captions, type CaptionsInstance, type CaptionsOptions, type CaptionsSettings, type StylePreset, captionsjs, captionsjs as default, googleFontsList, renderCaptions, renderString, stylePresets };