Visualize Your Ideas With Code
docs: add springs documentation (#388)
Jacobc155dcef2a31
diff --git a/packages/docs/docs/getting-started/tweening.mdx b/packages/docs/docs/getting-started/tweening.mdxindex 4266dddd..07fab720 100644--- a/packages/docs/docs/getting-started/tweening.mdx+++ b/packages/docs/docs/getting-started/tweening.mdx@@ -6,6 +6,7 @@ slug: /tweeningimport AnimationPlayer from '@site/src/components/AnimationPlayer';import CodeBlock from '@theme/CodeBlock';import linearSource from '!!raw-loader!@motion-canvas/examples/src/scenes/tweening-linear';+import springSource from '!!raw-loader!@motion-canvas/examples/src/scenes/tweening-spring';# Tweening@@ -32,7 +33,7 @@ case of our circle, we use the `map` function to map the `value` range from<AnimationPlayer small name="tweening-linear" />-## Timing functions+### Timing functionsAt the moment, our animation feels a bit unnatural. The speed with which the`value` parameter changes is constant, which in turn makes the circle move with@@ -81,7 +82,7 @@ map(-300, 300, easeInOutCubic(value));easeInOutCubic(value, -300, 300);```-## Interpolation functions+### Interpolation functionsSo far, we've only animated a single, numeric value. The[`map`](/api/core/tweening#map) function can be used to interpolate between two@@ -134,7 +135,7 @@ yield *<AnimationPlayer small name="tweening-vector" />-## Tweening properties+### Tweening propertiesThe [`tween`](/api/core/tweening#tween) function is useful when we need toorchestrate complex animations. However, there's a better way of tweening@@ -197,3 +198,65 @@ yield *Vector2.arcLerp,);```++## `spring` function++The [`spring`](/api/core/tweening#spring) function allows us to interpolate+between two values using Hooke's law. We need to provide it with the description+of our spring and the `from` and `to` values. You can think of it as having a+spring in resting position (the `to` value), stretching it all the way to the+starting position (the `from` value), and then letting it go. The movement of+the spring as it tries to reach the equilibrium is what we can use to dive our+animations.++In the example below, we use springs to animate a position of a circle, but this+method can be used for more things than just position.++<CodeBlock language="tsx">{springSource}</CodeBlock>++<AnimationPlayer small name="tweening-spring" />++### Spring description++The first argument of the [`spring`](/api/core/tweening#spring) function expects+an object that describes the physical properties of our spring. Motion Canvas+ships with a few useful presets that you can use, such as `PlopSpring` and+`SmoothSpring`. But it's possible to define your own spring:++```ts+const MySpring = {+ mass: 0.04,+ stiffness: 10.0,+ damping: 0.7,+ initialVelocity: 8.0,+};+```++- `mass` - Describes the inertia of the spring. How much force is required to+ accelerate and decelerate it.+- `stiffness` - The coefficient of the spring. Usually represented by `k` in+ Hooke's equation. It describes how stiff the spring is.+- `damping` - Over time, damping causes the spring to lose energy and eventually+ settle in equilibrium. You can set it to `0` to create a spring that+ oscillates indefinitely.+- `initialVelocity` - The initial velocity of the spring. You can set the `from`+ and `to` positions to the same value and give the spring some initial velocity+ to make it oscillate in place.++### Settle tolerance++Notice how in our spring example, we provided the first spring with an+additional value:++```ts+yield * spring(PlopSpring, -400, 400, 1 /*...*/);+// here ^+```++This optional argument is called `settleTolerance` and is used to define the+minimal distance from the `to` value the spring should reach to be considered+settled. The generator created by the [`spring`](/api/core/tweening#spring)+function finishes **only** when the spring settles. By adjusting the tolerance+we can make the animation finish faster, depending on our needs. In our example+we animate the position so a tolerance of `1` means that the spring needs to be+at most `1` pixel away from the `to` value.diff --git a/packages/examples/src/scenes/tweening-spring.meta b/packages/examples/src/scenes/tweening-spring.metanew file mode 100644index 00000000..efb61818--- /dev/null+++ b/packages/examples/src/scenes/tweening-spring.meta@@ -0,0 +1,4 @@+{+ "version": 1,+ "seed": 3566829446+}\ No newline at end of filediff --git a/packages/examples/src/scenes/tweening-spring.tsx b/packages/examples/src/scenes/tweening-spring.tsxnew file mode 100644index 00000000..a3dd9059--- /dev/null+++ b/packages/examples/src/scenes/tweening-spring.tsx@@ -0,0 +1,29 @@+import {makeScene2D} from '@motion-canvas/2d';+import {Circle} from '@motion-canvas/2d/lib/components';+import {createRef} from '@motion-canvas/core/lib/utils';+import {+ PlopSpring,+ SmoothSpring,+ spring,+} from '@motion-canvas/core/lib/tweening';++export default makeScene2D(function* (view) {+ const circle = createRef<Circle>();++ view.add(+ <Circle+ // highlight-start+ ref={circle}+ x={-400}+ size={240}+ fill={'#e13238'}+ />,+ );++ yield* spring(PlopSpring, -400, 400, 1, value => {+ circle().position.x(value);+ });+ yield* spring(SmoothSpring, 400, -400, value => {+ circle().position.x(value);+ });+});diff --git a/packages/examples/src/tweening-spring.meta b/packages/examples/src/tweening-spring.metanew file mode 100644index 00000000..0e33e3fe--- /dev/null+++ b/packages/examples/src/tweening-spring.meta@@ -0,0 +1,3 @@+{+ "version": 0+}\ No newline at end of filediff --git a/packages/examples/src/tweening-spring.ts b/packages/examples/src/tweening-spring.tsnew file mode 100644index 00000000..22fb6f69--- /dev/null+++ b/packages/examples/src/tweening-spring.ts@@ -0,0 +1,7 @@+import {makeProject} from '@motion-canvas/core';++import scene from './scenes/tweening-spring?scene';++export default makeProject({+ scenes: [scene],+});diff --git a/packages/examples/vite.config.ts b/packages/examples/vite.config.tsindex 50bb3de4..4f81fc75 100644--- a/packages/examples/vite.config.ts+++ b/packages/examples/vite.config.ts@@ -23,6 +23,7 @@ export default defineConfig({'./src/components.ts','./src/logging.ts','./src/transitions.ts',+ './src/tweening-spring.ts',],}),],