Visualize Your Ideas With Code
docs: document plugins (#724)
Jacob6e7e85823429
diff --git a/package-lock.json b/package-lock.jsonindex f7cee2fb..7a9feb0b 100644--- a/package-lock.json+++ b/package-lock.json@@ -6154,28 +6154,6 @@"integrity": "sha512-LG4opVs2ANWZ1TJoKc937iMmNstM/d0ae1vNbnBvBhqCSezgVUOzcLCqbI5elV8Vy6WKwKjaqR+zO9VKirBBCA==","dev": true},- "node_modules/@rollup/plugin-terser": {- "version": "0.4.3",- "resolved": "https://registry.npmjs.org/@rollup/plugin-terser/-/plugin-terser-0.4.3.tgz",- "integrity": "sha512-EF0oejTMtkyhrkwCdg0HJ0IpkcaVg1MMSf2olHb2Jp+1mnLM04OhjpJWGma4HobiDTF0WCyViWuvadyE9ch2XA==",- "dev": true,- "dependencies": {- "serialize-javascript": "^6.0.1",- "smob": "^1.0.0",- "terser": "^5.17.4"- },- "engines": {- "node": ">=14.0.0"- },- "peerDependencies": {- "rollup": "^2.x || ^3.x"- },- "peerDependenciesMeta": {- "rollup": {- "optional": true- }- }- },"node_modules/@rollup/plugin-typescript": {"version": "11.1.1","resolved": "https://registry.npmjs.org/@rollup/plugin-typescript/-/plugin-typescript-11.1.1.tgz",@@ -20307,12 +20285,6 @@"npm": ">= 3.0.0"}},- "node_modules/smob": {- "version": "1.1.1",- "resolved": "https://registry.npmjs.org/smob/-/smob-1.1.1.tgz",- "integrity": "sha512-i5aqEBPnDv9d77+NDxfjROtywxzNdAVNyaOr+RsLhM28Ts+Ar7luIp/Q+SBYa6wv/7BBcOpEkrhtDxsl2WA9Jg==",- "dev": true- },"node_modules/sockjs": {"version": "0.3.24","license": "MIT",@@ -20893,9 +20865,9 @@}},"node_modules/terser": {- "version": "5.17.5",- "resolved": "https://registry.npmjs.org/terser/-/terser-5.17.5.tgz",- "integrity": "sha512-NqFkzBX34WExkCbk3K5urmNCpEWqMPZnwGI1pMHwqvJ/zDlXC75u3NI7BrzoR8/pryy8Abx2e1i8ChrWkhH1Hg==",+ "version": "5.16.1",+ "resolved": "https://registry.npmjs.org/terser/-/terser-5.16.1.tgz",+ "integrity": "sha512-xvQfyfA1ayT0qdK47zskQgRZeWLoOQ8JQ6mIgRGVNwZKdQMU+5FkCBjmv4QjcrTzyZquRw2FVtlJSRUmMKQslw==","dependencies": {"@jridgewell/source-map": "^0.3.2","acorn": "^8.5.0",@@ -23082,13 +23054,41 @@"devDependencies": {"@rollup/plugin-commonjs": "^25.0.0","@rollup/plugin-node-resolve": "^15.0.2",- "@rollup/plugin-terser": "^0.4.3",+ "@rollup/plugin-terser": "0.4.2","@rollup/plugin-typescript": "^11.1.1","highlight.js": "^11.7.0","marked": "^4.2.12","ttypescript": "^1.5.15"}},+ "packages/internal/node_modules/@rollup/plugin-terser": {+ "version": "0.4.2",+ "resolved": "https://registry.npmjs.org/@rollup/plugin-terser/-/plugin-terser-0.4.2.tgz",+ "integrity": "sha512-jfUVQ4MxzIB0mz8QhDA1xiLT+pTF3WEWXeIqcwhoF84WhLWscPpxjJgjYMyAq0Po4UXqw2D9C64tD0gRDzJzfA==",+ "dev": true,+ "dependencies": {+ "serialize-javascript": "^6.0.0",+ "smob": "^0.0.6",+ "terser": "^5.15.1"+ },+ "engines": {+ "node": ">=14.0.0"+ },+ "peerDependencies": {+ "rollup": "^2.x || ^3.x"+ },+ "peerDependenciesMeta": {+ "rollup": {+ "optional": true+ }+ }+ },+ "packages/internal/node_modules/smob": {+ "version": "0.0.6",+ "resolved": "https://registry.npmjs.org/smob/-/smob-0.0.6.tgz",+ "integrity": "sha512-V21+XeNni+tTyiST1MHsa84AQhT1aFZipzPpOFAVB8DkHzwJyjjAmt9bgwnuZiZWnIbMo2duE29wybxv/7HWUw==",+ "dev": true+ },"packages/player": {"name": "@motion-canvas/player","version": "3.8.0",@@ -26838,11 +26838,30 @@"requires": {"@rollup/plugin-commonjs": "^25.0.0","@rollup/plugin-node-resolve": "^15.0.2",- "@rollup/plugin-terser": "^0.4.3",+ "@rollup/plugin-terser": "0.4.2","@rollup/plugin-typescript": "^11.1.1","highlight.js": "^11.7.0","marked": "^4.2.12","ttypescript": "^1.5.15"+ },+ "dependencies": {+ "@rollup/plugin-terser": {+ "version": "0.4.2",+ "resolved": "https://registry.npmjs.org/@rollup/plugin-terser/-/plugin-terser-0.4.2.tgz",+ "integrity": "sha512-jfUVQ4MxzIB0mz8QhDA1xiLT+pTF3WEWXeIqcwhoF84WhLWscPpxjJgjYMyAq0Po4UXqw2D9C64tD0gRDzJzfA==",+ "dev": true,+ "requires": {+ "serialize-javascript": "^6.0.0",+ "smob": "^0.0.6",+ "terser": "^5.15.1"+ }+ },+ "smob": {+ "version": "0.0.6",+ "resolved": "https://registry.npmjs.org/smob/-/smob-0.0.6.tgz",+ "integrity": "sha512-V21+XeNni+tTyiST1MHsa84AQhT1aFZipzPpOFAVB8DkHzwJyjjAmt9bgwnuZiZWnIbMo2duE29wybxv/7HWUw==",+ "dev": true+ }}},"@motion-canvas/player": {@@ -27542,17 +27561,6 @@}}},- "@rollup/plugin-terser": {- "version": "0.4.3",- "resolved": "https://registry.npmjs.org/@rollup/plugin-terser/-/plugin-terser-0.4.3.tgz",- "integrity": "sha512-EF0oejTMtkyhrkwCdg0HJ0IpkcaVg1MMSf2olHb2Jp+1mnLM04OhjpJWGma4HobiDTF0WCyViWuvadyE9ch2XA==",- "dev": true,- "requires": {- "serialize-javascript": "^6.0.1",- "smob": "^1.0.0",- "terser": "^5.17.4"- }- },"@rollup/plugin-typescript": {"version": "11.1.1","resolved": "https://registry.npmjs.org/@rollup/plugin-typescript/-/plugin-typescript-11.1.1.tgz",@@ -36927,12 +36935,6 @@"integrity": "sha512-94hK0Hh8rPqQl2xXc3HsaBoOXKV20MToPkcXvwbISWLEs+64sBq5kFgn2kJDHb1Pry9yrP0dxrCI9RRci7RXKg==","dev": true},- "smob": {- "version": "1.1.1",- "resolved": "https://registry.npmjs.org/smob/-/smob-1.1.1.tgz",- "integrity": "sha512-i5aqEBPnDv9d77+NDxfjROtywxzNdAVNyaOr+RsLhM28Ts+Ar7luIp/Q+SBYa6wv/7BBcOpEkrhtDxsl2WA9Jg==",- "dev": true- },"sockjs": {"version": "0.3.24","requires": {@@ -37334,9 +37336,9 @@"dev": true},"terser": {- "version": "5.17.5",- "resolved": "https://registry.npmjs.org/terser/-/terser-5.17.5.tgz",- "integrity": "sha512-NqFkzBX34WExkCbk3K5urmNCpEWqMPZnwGI1pMHwqvJ/zDlXC75u3NI7BrzoR8/pryy8Abx2e1i8ChrWkhH1Hg==",+ "version": "5.16.1",+ "resolved": "https://registry.npmjs.org/terser/-/terser-5.16.1.tgz",+ "integrity": "sha512-xvQfyfA1ayT0qdK47zskQgRZeWLoOQ8JQ6mIgRGVNwZKdQMU+5FkCBjmv4QjcrTzyZquRw2FVtlJSRUmMKQslw==","requires": {"@jridgewell/source-map": "^0.3.2","acorn": "^8.5.0",diff --git a/packages/core/src/app/ImageExporter.ts b/packages/core/src/app/ImageExporter.tsindex 2761084e..5d562826 100644--- a/packages/core/src/app/ImageExporter.ts+++ b/packages/core/src/app/ImageExporter.ts@@ -25,6 +25,8 @@ interface ServerResponse {/*** Image sequence exporter.+ *+ * @internal*/export class ImageExporter implements Exporter {public static readonly id = '@motion-canvas/core/image-sequence';diff --git a/packages/core/src/app/PlaybackManager.ts b/packages/core/src/app/PlaybackManager.tsindex db10a50f..b3519fad 100644--- a/packages/core/src/app/PlaybackManager.ts+++ b/packages/core/src/app/PlaybackManager.ts@@ -14,6 +14,8 @@ export enum PlaybackState {* @remarks* This class provides primitive operations that can be executed on a scene* sequence, such as {@link progress} or {@link seek}.+ *+ * @internal*/export class PlaybackManager {/**diff --git a/packages/core/src/app/PlaybackStatus.ts b/packages/core/src/app/PlaybackStatus.tsindex dd432ac2..299c5551 100644--- a/packages/core/src/app/PlaybackStatus.ts+++ b/packages/core/src/app/PlaybackStatus.ts@@ -18,7 +18,7 @@ export class PlaybackStatus {/*** Convert frames to seconds using the current framerate.*- * @param seconds - The frames to convert.+ * @param frames - The frames to convert.*/public framesToSeconds(frames: number) {return frames / this.playback.fps;diff --git a/packages/core/src/app/Player.ts b/packages/core/src/app/Player.tsindex 3327788f..9e32c5a8 100644--- a/packages/core/src/app/Player.ts+++ b/packages/core/src/app/Player.ts@@ -35,7 +35,7 @@ const MAX_AUDIO_DESYNC = 1 / 50;* The player logic used by the editor and embeddable player.** @remarks- * This class builds on top of the {@link PlaybackManager} to provide a simple+ * This class builds on top of the `PlaybackManager` to provide a simple* interface similar to other media players. It plays through the animation* using a real-time update loop and optionally synchronises it with audio.*/diff --git a/packages/core/src/app/Renderer.ts b/packages/core/src/app/Renderer.tsindex 10d3ebdb..0ea71287 100644--- a/packages/core/src/app/Renderer.ts+++ b/packages/core/src/app/Renderer.ts@@ -35,10 +35,9 @@ export enum RendererResult {* The rendering logic used by the editor to export animations.** @remarks- * This class uses the {@link PlaybackManager} to render animations.- * In contrast to a player, a renderer does not use an update loop.- * It plays through the animation as fast as it can, occasionally pausing- * to keep the UI responsive.+ * This class uses the `PlaybackManager` to render animations. In contrast to a+ * player, a renderer does not use an update loop. It plays through the+ * animation as fast as it can, occasionally pausing to keep the UI responsive.** The actual exporting is outsourced to an {@link Exporter}.*/@@ -129,7 +128,7 @@ export class Renderer {* Export an individual frame.** @remarks- * This method always uses the default {@link ImageExporter}.+ * This method always uses the default `ImageExporter`.** @param settings - The rendering settings.* @param time - The timestamp to export.diff --git a/packages/core/src/plugin/DefaultPlugin.ts b/packages/core/src/plugin/DefaultPlugin.tsindex 6c64ac9b..519d36b2 100644--- a/packages/core/src/plugin/DefaultPlugin.ts+++ b/packages/core/src/plugin/DefaultPlugin.ts@@ -3,6 +3,8 @@ import {ImageExporter} from '../app';/*** The default plugin included in every Motion Canvas project.+ *+ * @internal*/export default makePlugin({name: 'mc-default-plugin',diff --git a/packages/docs/docs/advanced/plugins.mdx b/packages/docs/docs/advanced/plugins.mdxnew file mode 100644index 00000000..bec06213--- /dev/null+++ b/packages/docs/docs/advanced/plugins.mdx@@ -0,0 +1,254 @@+---+sidebar_position: 5+slug: /plugins+---++# Authoring plugins++Plugins are a way to extend the functionality of Motion Canvas.++Motion Canvas consists of two separate parts: a [Vite](https://vitejs.dev/)+development server running on Node.js and an editor running in the browser+(runtime). Plugins can extend both of these.++## Writing a runtime plugin++<div className="row margin-bottom--md">+ <div className="col col--6">++Let's start by creating the entry point - a simple file that exports our plugin.+For now, we'll put that file in the `src` directory of our Motion Canvas+project. If you find yourself reusing the plugin across multiple projects, you+can put it in a separate package.++To define the plugin, we pass a configuration object to the+[`makePlugin`][make-plugin] helper function and export the result. `name` is the+only required property. `player` is an optional hook that will receive the+[`Player`](/api/core/app/Player) instance right after it's created. Take a look+at the [`Plugin`](/api/core/plugin/Plugin) interface for the full list of+available hooks.++ </div>+ <div className="col col--6">++```diff title="Project structure"+ project/+ ├── src/+ │ ├── scenes/+ │ │ └── example.tsx+// highlight-next-line++ │ ├── plugin.ts+ │ └── project.ts+ ├── package.json+ └── vite.config.ts+```++ </div>+</div>++```ts title="src/plugin.ts"+import {makePlugin} from '@motion-canvas/core';++export default makePlugin({+ name: 'motion-canvas-plugin-example',+ player(player) {+ player.onRecalculated.subscribe(() => {+ player.requestReset();+ player.togglePlayback(true);+ });+ },+});+```++In this example we subscribe to the+[`onRecalculated`](/api/core/app/Player#onRecalculated) event to play the+animation from the beginning whenever this event occurs. Recalculation happens+when we edit the animation either in the editor or in the code.++Now we can import the plugin in our project file. Note that+[`makePlugin`][make-plugin] returns a function that we need to call to create an+actual plugin object. We'll see [why that's useful](#passing-options-to-runtime)+in a moment:++```ts title="src/project.ts"+import {makeProject} from '@motion-canvas/core';+// highlight-next-line+import myPlugin from './plugin';+import example from './scenes/example?scene';++export default makeProject({+ scenes: [example],+ // highlight-start+ plugins: [myPlugin()],+ // ^^ we need to call the function+ // highlight-end+});+```++That's it! Now we can run the project and see our plugin in action.++## Writing a Node.js plugin++<div className="row margin-bottom--md">+ <div className="col col--6">++Motion Canvas builds on top of the Vite plugin system. To create a plugin that+runs on Node.js, you just create a [Vite plugin][vite-plugin] and import it in+your `vite.config.ts` file. On top of that, `@motion-canvas/vite-plugin`+provides a symbol that lets you define motion-canvas-specific options.++Let's start by creating a file for our plugin. This time we'll put it in the+root directory of our project because Vite plugins are not part of the runtime+source code.++We use the Vite [`configureServer`][vite-configure-server] hook to add a custom+endpoint. Meanwhile, the `entryPoint` option lets us specify the path to our+runtime plugin:++ </div>+ <div className="col col--6">++```diff title="Project structure"+ project/+ ├── src/+ │ ├── scenes/+ │ │ └── example.tsx+ │ ├── plugin.ts+ │ └── project.ts+ ├── package.json+// highlight-next-line++ ├── myVitePlugin.ts+ └── vite.config.ts+```++ </div>+</div>++```ts title="myVitePlugin.ts"+import {Plugin, PLUGIN_OPTIONS} from '@motion-canvas/vite-plugin';++export default function myVitePlugin(): Plugin {+ return {+ name: 'vite-plugin-motion-canvas-example',++ // extend the dev server using Vite plugin hooks:+ configureServer(server) {+ server.middlewares.use('/my-plugin', (req, res) => {+ res.end('Hello from my plugin!');+ });+ },++ // extend Motion Canvas:+ [PLUGIN_OPTIONS]: {+ entryPoint: './plugin.ts',+ },+ };+}+```++Here's how we would import such plugin in our `vite.config.ts` file:++```ts title="vite.config.ts"+import {defineConfig} from 'vite';+import motionCanvas from '@motion-canvas/vite-plugin';+// highlight-next-line+import myVitePlugin from './myVitePlugin';++export default defineConfig({+ plugins: [+ motionCanvas(),+ // highlight-next-line+ myVitePlugin(),+ ],+});+```++Also, since we defined the entry point in the Node.js plugin, we no longer need+to import the runtime plugin in our project file:++```diff title="src/project.ts"+ import {makeProject} from '@motion-canvas/core';+- import myPlugin from './plugin';+ import example from './scenes/example?scene';++ export default makeProject({+ scenes: [example],+- plugins: [myPlugin()],+ });+```++:::tip++Notice that we defined the entry point as `'./plugin.ts'`. Because it's a+relative path, it will be resolved relative to the project file. Once you turn+your plugin into a separate package, you can use a package name instead.++:::++You can verify that the plugin is working by running the project and visiting+[http://localhost:9000/my-plugin](http://localhost:9000/my-plugin).++### Passing options to Runtime++A Node.js plugin has the ability to pass options to the runtime plugin. We can+do that using the `runtimeConfig` property:++```ts title="myVitePlugin.ts"+import {Plugin, PLUGIN_OPTIONS} from '@motion-canvas/vite-plugin';++export default function myVitePlugin(): Plugin {+ return {+ name: 'vite-plugin-motion-canvas-example',+ // ...+ [PLUGIN_OPTIONS]: {+ entryPoint: './plugin.ts',+ // highlight-start+ runtimeConfig: () => ({+ foo: 'bar',+ }),+ // highlight-end+ },+ };+}+```++We can then update the runtime plugin to receive these options:++```ts title="src/plugin.ts"+import {makePlugin} from '@motion-canvas/core';++// highlight-start+interface MyPluginOptions {+ foo: string;+}+// highlight-end++export default makePlugin((options?: MyPluginOptions) => {+ // highlight-next-line+ console.log(options?.foo); // 'bar'++ return {+ name: 'motion-canvas-plugin-example',+ player(player) {+ player.onRecalculated.subscribe(() => {+ player.requestReset();+ player.togglePlayback(true);+ });+ },+ };+});+```++## Plugin examples++You can take a look at our [FFmpeg plugin][ffmpeg-plugin] for an example of a+fully-fledged plugin. It's runtime portion provides the editor with a Video+[exporter](/api/core/app/Exporter) that sends the rendered frames over to+Node.js. The server portion then uses FFmpeg to convert the frames into a video.++[vite-plugin]: https://vitejs.dev/guide/api-plugin.html#authoring-a-plugin+[vite-configure-server]:+ https://vitejs.dev/guide/api-plugin.html#configureserver+[make-plugin]: /api/core/plugin#makePlugin+[ffmpeg-plugin]:+ https://github.com/motion-canvas/exporters/tree/main/packages/ffmpegdiff --git a/packages/docs/typedoc.js b/packages/docs/typedoc.jsindex f1648c1a..19bcd0b5 100644--- a/packages/docs/typedoc.js+++ b/packages/docs/typedoc.js@@ -31,12 +31,13 @@ module.exports = () => ({excludeInternal: true,excludePrivate: true,entryPoints: [+ '../core/src/app','../core/src/decorators','../core/src/events','../core/src/flow',- '../core/src/helpers','../core/src/media',- '../core/src/player',+ '../core/src/meta',+ '../core/src/plugin','../core/src/scenes','../core/src/signals','../core/src/threading',diff --git a/packages/internal/package.json b/packages/internal/package.jsonindex 0e942a31..b6db8939 100644--- a/packages/internal/package.json+++ b/packages/internal/package.json@@ -5,7 +5,7 @@"devDependencies": {"@rollup/plugin-commonjs": "^25.0.0","@rollup/plugin-node-resolve": "^15.0.2",- "@rollup/plugin-terser": "^0.4.3",+ "@rollup/plugin-terser": "0.4.2","@rollup/plugin-typescript": "^11.1.1","highlight.js": "^11.7.0","marked": "^4.2.12",