Visualize Your Ideas With Code
docs: add signals page (#134)
Jacobcc31a2d87013
diff --git a/packages/docs/docs/guides/getting-started/properties.md b/packages/docs/docs/guides/getting-started/properties.mddeleted file mode 100644index 89ecacc4..00000000--- a/packages/docs/docs/guides/getting-started/properties.md+++ /dev/null@@ -1,5 +0,0 @@-----sidebar_position: 4------# Signals and Propertiesdiff --git a/packages/docs/docs/guides/getting-started/signals.mdx b/packages/docs/docs/guides/getting-started/signals.mdxnew file mode 100644index 00000000..20eee6f8--- /dev/null+++ b/packages/docs/docs/guides/getting-started/signals.mdx@@ -0,0 +1,118 @@+---+sidebar_position: 3+---++import AnimationPlayer from '@site/src/components/AnimationPlayer';+import CodeBlock from '@theme/CodeBlock';+import signalSource from '!!raw-loader!@motion-canvas/examples/src/scenes/node-signal';++# Signals++Signals represent a value that may change over time.+They can be used to define dependencies between the state of the animation.+This way, when a value changes, all other values that depend on it get automatically updated.++## Overview++Signals for primitive types are created using the [`createSignal()`](/api/core/signals#createSignal) function, where the first argument specifies their initial value:++```ts+import {createSignal} from '@motion-canvas/core/lib/signals';++const signal = createSignal(0);+```++Additionally, each complex type has a static `createSignal()` method that can be used to create a signal for said type:++```ts+import {Vector2} from '@motion-canvas/core/lib/types';++const signal = Vector2.createSignal(Vector2.up);+```++Properties of every node are also represented by signals:++```tsx+const circle = <Circle />;++const signal = circle.fill;+```++Once created, signals can be invoked to perform one of the three possible actions+(The action is chosen based on the number of arguments):++1. retrieve the value:+ ```ts+ const value = signal();+ ```+2. update the value:+ ```ts+ signal(3);+ ```+3. create a [tween](/guides/getting-started/tweening) for the value:+ ```ts+ yield * signal(2, 0.3);+ ```++Instead of the actual value, a signal can be provided with a function that computes the value dynamically. Consider the following example:++```ts+const radius = createSignal(1);+const area = createSignal(() => Math.PI * radius() * radius());++console.log(area()); // 3.141592653589793+radius(2);+console.log(area()); // 12.566370614359172+```++Here, the `area` signal uses the `radius` signal to compute its value.++## Explanation++To better understand how signals work, let's modify the example from before to see when exactly the area is calculated:++```ts+const radius = createSignal(1);+const area = createSignal(() => {+ console.log('area recalculated!');+ return Math.PI * radius() * radius();+});++area(); // area recalculated!+area();+radius(2);+area(); // area recalculated!+radius(3);+radius(4);+area(); // area recalculated!+```++This demonstrates three important aspects of signals:++### Laziness++Signals are only calculated when their value is requested. The first `"area recalculated!"` message is logged to console only after `area()` is called.++### Caching++Once the signal is calculated, its value is saved and then returned during subsequent calls to `area()`. That's why nothing is logged to the console during the second call. This aspect of signals makes them perfect for caching computationally heavy operations. In fact, Motion Canvas uses signals internally to cache things such as matrices.++### Dependency tracking++The `area` signal keeps track of other signals it depends on. When we change the `radius` signal, the `area` signal is notified about that. But it doesn't get recalculated immediately - laziness is still at play. We can modify the radius however many times we want, but the `area` will be recalculated only once its value is requested again by calling `area()`.++## Complex example++We can use the fact that properties of nodes are represented by signals to construct scenes that automatically update when the data changes. Following the previous example, let's create a visualisation for the area of the circle:++<AnimationPlayer small name="node-signal" />++Below you'll find the code used to create this animation. We highlighted all the places where signals are used:++<CodeBlock language="tsx">{signalSource}</CodeBlock>++With this setup, all we need to do is animate the `radius` signal, and the rest of the scene will adjust accordingly:++```ts+yield * radius(4, 2).to(3, 2);+```diff --git a/packages/docs/docs/guides/getting-started/tweening.mdx b/packages/docs/docs/guides/getting-started/tweening.mdxindex 4044fd87..5964cf3b 100644--- a/packages/docs/docs/guides/getting-started/tweening.mdx+++ b/packages/docs/docs/guides/getting-started/tweening.mdx@@ -1,5 +1,5 @@----sidebar_position: 3+sidebar_position: 4---import AnimationPlayer from '@site/src/components/AnimationPlayer';@@ -152,12 +152,18 @@ Can be written as:yield* circle.value.color('#e13238', 2);```-Here, we use a [`SignalTween`](/api/core/utils/SignalTween) signature that+Here, we use a [`SignalTween`](/api/core/signals/SignalTween) signature thatlooks similar to a setter, except it accepts the transition duration as itssecond argument. Under the hood, this will also create a tween - one thatstarts with the current value and ends with the newly provided one.-By default, it will use `easeInOutCubic` as the timing function.+We can chain multiple tweens together by calling the+`to()` method on the returned object:+```ts+yield* circle.value.color('#e13238', 2).to('#e6a700', 2);+```++By default, property tweens use `easeInOutCubic` as the timing function.We can override that by providing a third argument:```tsdiff --git a/packages/docs/docusaurus.config.js b/packages/docs/docusaurus.config.jsindex a62918fc..725596ce 100644--- a/packages/docs/docusaurus.config.js+++ b/packages/docs/docusaurus.config.js@@ -39,8 +39,9 @@ const config = {{to: '/blog', label: 'Blog', position: 'left'},{href: 'https://github.com/motion-canvas',- label: 'GitHub',position: 'right',+ className: 'navbar-github-link',+ 'aria-label': 'GitHub repository',},],},diff --git a/packages/docs/src/components/AnimationPlayer/styles.module.css b/packages/docs/src/components/AnimationPlayer/styles.module.cssindex 3eeef9e2..ae846829 100644--- a/packages/docs/src/components/AnimationPlayer/styles.module.css+++ b/packages/docs/src/components/AnimationPlayer/styles.module.css@@ -2,7 +2,7 @@width: 100%;overflow: hidden;border-radius: var(--ifm-global-radius);- background-color: var(--ifm-background-color);+ background-color: var(--ifm-background-surface-color);position: relative;aspect-ratio: 16 / 9;border: 1px solid var(--ifm-color-emphasis-300);diff --git a/packages/docs/src/css/custom.css b/packages/docs/src/css/custom.cssindex 1c3b2c05..e2b83076 100644--- a/packages/docs/src/css/custom.css+++ b/packages/docs/src/css/custom.css@@ -115,6 +115,35 @@ a.button:hover {text-decoration: none;}+.navbar-github-link {+ transition: background var(--ifm-transition-fast);+ width: 2rem;+ height: 2rem;+ padding: 0;+ display: flex;+ align-items: center;+ justify-content: center;+ border-radius: 50%;+ margin-right: var(--ifm-button-padding-vertical);+}++.navbar-github-link:hover {+ background-color: var(--ifm-color-emphasis-200);+}++.navbar-github-link::before {+ display: flex;+ content: '';+ background-image: url('/img/github-icon.svg');+ width: 20px;+ height: 20px;+ color: red;+}++html[data-theme='dark'] .navbar-github-link::before {+ background-image: url('/img/github-icon-dark.svg');+}+@media screen and (max-width: 996px) {:root {--ifm-navbar-background-color: rgb(255, 255, 255);diff --git a/packages/docs/static/img/github-icon-dark.svg b/packages/docs/static/img/github-icon-dark.svgnew file mode 100644index 00000000..427b7504--- /dev/null+++ b/packages/docs/static/img/github-icon-dark.svg@@ -0,0 +1,8 @@+<svg viewBox="0 0 98 96" xmlns="http://www.w3.org/2000/svg">+ <path+ fill-rule="evenodd"+ clip-rule="evenodd"+ d="M48.854 0C21.839 0 0 22 0 49.217c0 21.756 13.993 40.172 33.405 46.69 2.427.49 3.316-1.059 3.316-2.362 0-1.141-.08-5.052-.08-9.127-13.59 2.934-16.42-5.867-16.42-5.867-2.184-5.704-5.42-7.17-5.42-7.17-4.448-3.015.324-3.015.324-3.015 4.934.326 7.523 5.052 7.523 5.052 4.367 7.496 11.404 5.378 14.235 4.074.404-3.178 1.699-5.378 3.074-6.6-10.839-1.141-22.243-5.378-22.243-24.283 0-5.378 1.94-9.778 5.014-13.2-.485-1.222-2.184-6.275.486-13.038 0 0 4.125-1.304 13.426 5.052a46.97 46.97 0 0 1 12.214-1.63c4.125 0 8.33.571 12.213 1.63 9.302-6.356 13.427-5.052 13.427-5.052 2.67 6.763.97 11.816.485 13.038 3.155 3.422 5.015 7.822 5.015 13.2 0 18.905-11.404 23.06-22.324 24.283 1.78 1.548 3.316 4.481 3.316 9.126 0 6.6-.08 11.897-.08 13.526 0 1.304.89 2.853 3.316 2.364 19.412-6.52 33.405-24.935 33.405-46.691C97.707 22 75.788 0 48.854 0z"+ fill="#e3e3e3"+ />+</svg>diff --git a/packages/docs/static/img/github-icon.svg b/packages/docs/static/img/github-icon.svgnew file mode 100644index 00000000..a07a6b66--- /dev/null+++ b/packages/docs/static/img/github-icon.svg@@ -0,0 +1,8 @@+<svg viewBox="0 0 98 96" xmlns="http://www.w3.org/2000/svg">+ <path+ fill-rule="evenodd"+ clip-rule="evenodd"+ d="M48.854 0C21.839 0 0 22 0 49.217c0 21.756 13.993 40.172 33.405 46.69 2.427.49 3.316-1.059 3.316-2.362 0-1.141-.08-5.052-.08-9.127-13.59 2.934-16.42-5.867-16.42-5.867-2.184-5.704-5.42-7.17-5.42-7.17-4.448-3.015.324-3.015.324-3.015 4.934.326 7.523 5.052 7.523 5.052 4.367 7.496 11.404 5.378 14.235 4.074.404-3.178 1.699-5.378 3.074-6.6-10.839-1.141-22.243-5.378-22.243-24.283 0-5.378 1.94-9.778 5.014-13.2-.485-1.222-2.184-6.275.486-13.038 0 0 4.125-1.304 13.426 5.052a46.97 46.97 0 0 1 12.214-1.63c4.125 0 8.33.571 12.213 1.63 9.302-6.356 13.427-5.052 13.427-5.052 2.67 6.763.97 11.816.485 13.038 3.155 3.422 5.015 7.822 5.015 13.2 0 18.905-11.404 23.06-22.324 24.283 1.78 1.548 3.316 4.481 3.316 9.126 0 6.6-.08 11.897-.08 13.526 0 1.304.89 2.853 3.316 2.364 19.412-6.52 33.405-24.935 33.405-46.691C97.707 22 75.788 0 48.854 0z"+ fill="#1c1c1c"+ />+</svg>diff --git a/packages/examples/src/node-signal.meta b/packages/examples/src/node-signal.metanew file mode 100644index 00000000..0e33e3fe--- /dev/null+++ b/packages/examples/src/node-signal.meta@@ -0,0 +1,3 @@+{+ "version": 0+}\ No newline at end of filediff --git a/packages/examples/src/node-signal.ts b/packages/examples/src/node-signal.tsnew file mode 100644index 00000000..f7697d9c--- /dev/null+++ b/packages/examples/src/node-signal.ts@@ -0,0 +1,7 @@+import {makeProject} from '@motion-canvas/core';++import scene from './scenes/node-signal?scene';++export default makeProject({+ scenes: [scene],+});diff --git a/packages/examples/src/random.ts b/packages/examples/src/random.tsindex e7ea939e..b8dc0427 100644--- a/packages/examples/src/random.ts+++ b/packages/examples/src/random.ts@@ -5,6 +5,5 @@ import {Vector2} from '@motion-canvas/core/lib/types';export default makeProject({scenes: [scene],- background: '#141414',size: new Vector2(960, 540),});diff --git a/packages/examples/src/scenes/node-signal.meta b/packages/examples/src/scenes/node-signal.metanew file mode 100644index 00000000..14f24862--- /dev/null+++ b/packages/examples/src/scenes/node-signal.meta@@ -0,0 +1,4 @@+{+ "version": 1,+ "seed": 1826074633+}\ No newline at end of filediff --git a/packages/examples/src/scenes/node-signal.tsx b/packages/examples/src/scenes/node-signal.tsxnew file mode 100644index 00000000..4ebb1f47--- /dev/null+++ b/packages/examples/src/scenes/node-signal.tsx@@ -0,0 +1,65 @@+import {makeScene2D} from '@motion-canvas/2d/lib/scenes';+import {Circle, Text, Line} from '@motion-canvas/2d/lib/components';+import {createSignal} from '@motion-canvas/core/lib/signals';+import {Vector2} from '@motion-canvas/core/lib/types';+import {waitFor} from '@motion-canvas/core/lib/flow';++export default makeScene2D(function* (view) {+ // highlight-start+ const radius = createSignal(3);+ const area = createSignal(() => Math.PI * radius() * radius());+ // highlight-end++ const scale = 100;+ const textStyle = {+ fontWeight: 700,+ fontSize: 56,+ offsetY: -1,+ padding: 20,+ cache: true,+ };++ view.add(+ <>+ <Circle+ // highlight-start+ width={() => radius() * scale * 2}+ height={() => radius() * scale * 2}+ // highlight-end+ fill={'#e13238'}+ />+ <Line+ points={[+ Vector2.zero,+ // highlight-next-line+ () => Vector2.right.scale(radius() * scale),+ ]}+ lineDash={[20, 20]}+ startArrow+ endArrow+ endOffset={8}+ lineWidth={8}+ stroke={'#242424'}+ />+ <Text+ // highlight-start+ text={() => `r = ${radius().toFixed(2)}`}+ x={() => (radius() * scale) / 2}+ // highlight-end+ fill={'#242424'}+ {...textStyle}+ />+ <Text+ // highlight-start+ text={() => `A = ${area().toFixed(2)}`}+ y={() => radius() * scale}+ // highlight-end+ fill={'#e13238'}+ {...textStyle}+ />+ </>,+ );++ yield* radius(4, 2).to(3, 2);+ yield* waitFor(1);+});diff --git a/packages/examples/src/scenes/random.tsx b/packages/examples/src/scenes/random.tsxindex af80d50b..ac9bae01 100644--- a/packages/examples/src/scenes/random.tsx+++ b/packages/examples/src/scenes/random.tsx@@ -13,7 +13,7 @@ export default makeScene2D(function* (view) {{range(40).map(i => (<Rectref={makeRef(rects, i)}- radius={20}+ radius={5}width={10}height={10}fill={'#e13238'}diff --git a/packages/examples/vite.config.ts b/packages/examples/vite.config.tsindex cbc3f9a1..6148b6d0 100644--- a/packages/examples/vite.config.ts+++ b/packages/examples/vite.config.ts@@ -10,6 +10,7 @@ export default defineConfig({'./src/tweening-cubic.ts','./src/tweening-color.ts','./src/tweening-vector.ts',+ './src/node-signal.ts','./src/random.ts',],}),