Skip to content

🎥 Make videos programmatically with React

update spring docs

Jonny Burger6b61adf40f55
diff --git a/packages/docs/components/demos/Spring.tsx b/packages/docs/components/demos/Spring.tsx
new file mode 100644
index 0000000000..e7d575e197
--- /dev/null
+++ b/packages/docs/components/demos/Spring.tsx
@@ -0,0 +1,65 @@
+import React from 'react';
+import {AbsoluteFill, spring, useCurrentFrame, useVideoConfig} from 'remotion';
+
+export const SpringDemo: React.FC<{
+ readonly damping?: number;
+ readonly mass?: number;
+ readonly stiffness?: number;
+ readonly overshootClamping?: boolean;
+ readonly reverse?: boolean;
+ readonly durationInFrames?: number;
+}> = ({
+ damping = 10,
+ mass = 1,
+ stiffness = 100,
+ overshootClamping = false,
+ reverse = false,
+ durationInFrames,
+}) => {
+ const frame = useCurrentFrame();
+ const {fps} = useVideoConfig();
+
+ const value = spring({
+ frame,
+ fps,
+ config: {
+ damping,
+ mass,
+ stiffness,
+ overshootClamping,
+ },
+ durationInFrames: durationInFrames ?? undefined,
+ reverse,
+ });
+
+ return (
+ <AbsoluteFill
+ style={{
+ padding: 20,
+ fontSize: 20,
+ fontFamily: 'GTPlanar',
+ }}
+ >
+ <div>
+ &quot;spring&quot;: {value.toFixed(3)}, &quot;frame&quot;: &quot;
+ {frame}&quot;
+ </div>
+ <AbsoluteFill
+ style={{
+ justifyContent: 'center',
+ alignItems: 'center',
+ }}
+ >
+ <div
+ style={{
+ height: 100,
+ width: 100,
+ backgroundColor: 'var(--ifm-color-primary)',
+ borderRadius: 10,
+ scale: value,
+ }}
+ />
+ </AbsoluteFill>
+ </AbsoluteFill>
+ );
+};
diff --git a/packages/docs/components/demos/index.tsx b/packages/docs/components/demos/index.tsx
index 3d26ea8a2d..c4487bf77b 100644
--- a/packages/docs/components/demos/index.tsx
+++ b/packages/docs/components/demos/index.tsx
@@ -29,6 +29,8 @@ import {
skewDemo,
slidePresentationDemo,
slidePresentationDemoLongThreshold,
+ springDampingDemo,
+ springDemo,
starDemo,
translateDemo,
triangleDemo,
@@ -71,6 +73,8 @@ const demos: DemoType[] = [
cubePresentationDemo,
animationMathDemo,
roundedTextBoxDemo,
+ springDemo,
+ springDampingDemo,
];
export const Demo: React.FC<{
diff --git a/packages/docs/components/demos/types.ts b/packages/docs/components/demos/types.ts
index cd55ac1561..baaed82d2a 100644
--- a/packages/docs/components/demos/types.ts
+++ b/packages/docs/components/demos/types.ts
@@ -19,6 +19,7 @@ import {PieDemo} from './Pie';
import {PolygonDemo} from './Polygon';
import {RectDemo} from './Rect';
import {RoundedTextBox} from './RoundedTextBox';
+import {SpringDemo} from './Spring';
import {StarDemo} from './Star';
import {AnimationMath} from './SubtractAnimations';
import {
@@ -823,3 +824,84 @@ export const animationMathDemo: DemoType = {
autoPlay: true,
options: [],
};
+
+export const springDemo: DemoType = {
+ comp: SpringDemo,
+ compHeight: 400,
+ compWidth: 1280,
+ durationInFrames: 150,
+ fps: 30,
+ id: 'spring',
+ autoPlay: true,
+ options: [
+ {
+ name: 'damping',
+ type: 'numeric',
+ default: 10,
+ min: 0,
+ max: 100,
+ step: 1,
+ optional: 'no',
+ },
+ {
+ name: 'mass',
+ type: 'numeric',
+ default: 1,
+ min: 0.1,
+ max: 10,
+ step: 0.1,
+ optional: 'no',
+ },
+ {
+ name: 'stiffness',
+ type: 'numeric',
+ default: 100,
+ min: 1,
+ max: 500,
+ step: 1,
+ optional: 'no',
+ },
+ {
+ name: 'durationInFrames',
+ type: 'numeric',
+ default: 30,
+ min: 1,
+ max: 120,
+ step: 1,
+ optional: 'default-disabled',
+ },
+ {
+ name: 'overshootClamping',
+ type: 'boolean',
+ default: false,
+ optional: 'no',
+ },
+ {
+ name: 'reverse',
+ type: 'boolean',
+ default: false,
+ optional: 'no',
+ },
+ ],
+};
+
+export const springDampingDemo: DemoType = {
+ comp: SpringDemo,
+ compHeight: 400,
+ compWidth: 1280,
+ durationInFrames: 60,
+ fps: 30,
+ id: 'spring-damping',
+ autoPlay: true,
+ options: [
+ {
+ name: 'damping',
+ type: 'numeric',
+ default: 100,
+ min: 1,
+ max: 200,
+ step: 1,
+ optional: 'no',
+ },
+ ],
+};
diff --git a/packages/docs/docs/spring.mdx b/packages/docs/docs/spring.mdx
index 4f92dcc347..80070ad491 100644
--- a/packages/docs/docs/spring.mdx
+++ b/packages/docs/docs/spring.mdx
@@ -7,7 +7,7 @@ crumb: 'API'
A physics-based animation primitive.
-Example:
+Example usage:
```tsx twoslash title="spring-example.ts"
import {spring, useCurrentFrame, useVideoConfig} from 'remotion';
@@ -24,9 +24,15 @@ const value = spring({
});
```
-## Timing Editor
+In the following example, the <code>value</code> controls the [`scale`](/docs/transforms#scale) property of the div:
-Visit [remotion.dev/timing-editor](https://www.remotion.dev/timing-editor) to play around with the spring animation parameters and see how they affect the animation.
+<Demo type="spring" />
+
+To disable the default bounce, increase the [`damping`](#damping) parameter.
+
+<Demo type="spring-damping" />
+
+For a more advanced playground, visit [remotion.dev/timing-editor](https://www.remotion.dev/timing-editor).
## Parameters
@@ -34,67 +40,65 @@ Visit [remotion.dev/timing-editor](https://www.remotion.dev/timing-editor) to pl
The current time value. Most of the time you want to pass in the return value of [`useCurrentFrame()`](/docs/use-current-frame). The spring animation starts at frame 0, so if you would like to delay the animation, you can pass a different value like `frame - 20`.
-### `from`
+### `fps`
+
+For how many frames per second the spring animation should be calculated. This should always be the `fps` property of the return value of [`useVideoConfig()`](/docs/use-video-config).
+
+### `from?`
_Default:_ `0`
The initial value of the animation.
-### `to`
+### `to?`
_Default:_ `1`
The end value of the animation. Note that depending on the parameters, spring animations may overshoot the target a bit, before they bounce back to their final target.
-### `fps`
-
-For how many frames per second the spring animation should be calculated. This should always be the `fps` property of the return value of [`useVideoConfig()`](/docs/use-video-config).
-
-### `reverse`<AvailableFrom v="3.3.92" />
+### `reverse?`<AvailableFrom v="3.3.92" />
_Default:_ `false`
Render the animation in reverse. See: [Order of operations](#order-of-operations)
-### `config`
+### `config?`
An optional object that allows you to customize the physical properties of the animation.
-#### mass
+#### `mass?`
_Default:_ `1`
The weight of the spring. If you reduce the mass, the animation becomes faster!
-#### damping
+#### `damping?`
_Default_: `10`
How hard the animation decelerates.
-#### stiffness
+#### `stiffness?`
_Default_: `100`
Spring stiffness coefficient. Play with this one and it will affect how bouncy your animation is.
-#### overshootClamping
+#### `overshootClamping?`
_Default_: `false`
Determines whether the animation can shoot over the `to` value. If set to true, if the animation goes over `to`, it will just return the value of `to`.
-### `durationInFrames`<AvailableFrom v="3.0.27" />
-
-_optional_
+### `durationInFrames?`<AvailableFrom v="3.0.27" />
Stretches the animation curve so it is exactly as long as you specify.
```tsx twoslash title="spring-example.ts"
-import { spring, useCurrentFrame, useVideoConfig } from "remotion";
+import {spring, useCurrentFrame, useVideoConfig} from 'remotion';
// ---cut---
const frame = useCurrentFrame();
-const { fps } = useVideoConfig();
+const {fps} = useVideoConfig();
const value = spring({
frame,
@@ -108,17 +112,13 @@ const value = spring({
See also: [Order of operations](#order-of-operations)
-### `durationRestThreshold`<AvailableFrom v="3.0.27" />
-
-_optional_
+### `durationRestThreshold?`<AvailableFrom v="3.0.27" />
How close the animation should be to the end in order to be considered finished for the calculation of the duration. Only has an effect if `durationInFrames` is also specified.
For example, if a `durationRestThreshold` of `0.001` is given, and the durationOfFrames is `30`, it means that after 30 frames, the spring has reached 99.9% (`1 - 0.001 = 0.999`) of it's distance to the end value.
-### `delay`<AvailableFrom v="3.3.90" />
-
-_optional_
+### `delay?`<AvailableFrom v="3.3.90" />
How many frames to delay the animation for.
@@ -140,5 +140,5 @@ This function was taken from [Reanimated 2](https://github.com/software-mansion/
- [Spring animation example](/docs/animating-properties#using-spring-animations)
- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/core/src/spring/index.ts)
-- [interpolate()](/docs/interpolate)
-- [measureSpring()](/docs/measure-spring)
+- [`interpolate()`](/docs/interpolate)
+- [`measureSpring()`](/docs/measure-spring)
diff --git a/packages/docs/docs/transforms.mdx b/packages/docs/docs/transforms.mdx
index ac7c912604..5fb8737c00 100644
--- a/packages/docs/docs/transforms.mdx
+++ b/packages/docs/docs/transforms.mdx
@@ -55,7 +55,7 @@ Values below `0` and above `1` are accepted, but have no further effect.
The scale determines how big an element is. `1` is the natural size, `2` will make the element twice as tall and wide.
Values below `1` will make the element smaller. `0` makes the element invisible. Values below `0` are accepted and will make the element bigger again, but mirrored.
-You can set the scale of an element using the `transform` property.
+You can set the scale of an element using the `scale` property.
```tsx twoslash {6} title="MyComponent.tsx"
<div
@@ -63,7 +63,7 @@ You can set the scale of an element using the `transform` property.
height: 100,
width: 100,
backgroundColor: 'red',
- transform: `scale(0.5)`,
+ scale: 0.5,
}}
/>
```