Skip to content

🎥 Make videos programmatically with React

Docs: Improved Animation Math article

Jonny Burgerb416306b4c88
diff --git a/packages/docs/components/demos/SubtractAnimations.tsx b/packages/docs/components/demos/SubtractAnimations.tsx
new file mode 100644
index 0000000000..d3f0db890d
--- /dev/null
+++ b/packages/docs/components/demos/SubtractAnimations.tsx
@@ -0,0 +1,59 @@
+import React from "react";
+import {
+ AbsoluteFill,
+ spring,
+ useCurrentFrame,
+ useVideoConfig,
+} from "remotion";
+
+export const AnimationMath: React.FC = () => {
+ const frame = useCurrentFrame();
+ const { fps, durationInFrames } = useVideoConfig();
+
+ const enter = spring({
+ fps,
+ frame,
+ config: {
+ damping: 200,
+ },
+ });
+
+ const exit = spring({
+ fps,
+ config: {
+ damping: 200,
+ },
+ durationInFrames: 20,
+ delay: durationInFrames - 20,
+ frame,
+ });
+
+ const scale = enter - exit;
+
+ return (
+ <AbsoluteFill
+ style={{
+ justifyContent: "center",
+ alignItems: "center",
+ backgroundColor: "white",
+ }}
+ >
+ <div
+ style={{
+ height: 100,
+ width: 100,
+ backgroundColor: "#4290f5",
+ borderRadius: 20,
+ transform: `scale(${scale})`,
+ justifyContent: "center",
+ alignItems: "center",
+ display: "flex",
+ fontSize: 50,
+ color: "white",
+ }}
+ >
+ {frame}
+ </div>
+ </AbsoluteFill>
+ );
+};
diff --git a/packages/docs/components/demos/index.tsx b/packages/docs/components/demos/index.tsx
index 6feeceb0df..6174a73b75 100644
--- a/packages/docs/components/demos/index.tsx
+++ b/packages/docs/components/demos/index.tsx
@@ -6,6 +6,7 @@ import { Control } from "./control";
import styles from "./styles.module.css";
import type { DemoType } from "./types";
import {
+ animationMathDemo,
circleDemo,
clockWipePresentationDemo,
cubePresentationDemo,
@@ -61,10 +62,11 @@ const demos: DemoType[] = [
customTimingDemo,
clockWipePresentationDemo,
cubePresentationDemo,
+ animationMathDemo,
];
export const Demo: React.FC<{
- type: string;
+ readonly type: string;
}> = ({ type }) => {
const demo = demos.find((d) => d.id === type);
const { colorMode } = useColorMode();
diff --git a/packages/docs/components/demos/types.ts b/packages/docs/components/demos/types.ts
index 3a16089dc2..4ddc248804 100644
--- a/packages/docs/components/demos/types.ts
+++ b/packages/docs/components/demos/types.ts
@@ -16,6 +16,7 @@ import { PieDemo } from "./Pie";
import { PolygonDemo } from "./Polygon";
import { RectDemo } from "./Rect";
import { StarDemo } from "./Star";
+import { AnimationMath } from "./SubtractAnimations";
import {
OpacityDemo,
RotateDemo,
@@ -667,3 +668,14 @@ export const customTimingDemo: DemoType = {
autoPlay: true,
options: [],
};
+
+export const animationMathDemo: DemoType = {
+ comp: AnimationMath,
+ compHeight: 280,
+ compWidth: 540,
+ durationInFrames: 120,
+ fps: 30,
+ id: "animation-math",
+ autoPlay: true,
+ options: [],
+};
diff --git a/packages/docs/docs/animation-math.mdx b/packages/docs/docs/animation-math.mdx
new file mode 100644
index 0000000000..99e828f2f8
--- /dev/null
+++ b/packages/docs/docs/animation-math.mdx
@@ -0,0 +1,103 @@
+---
+image: /generated/articles-docs-animation-math.png
+title: Animation math
+crumb: "Techniques"
+---
+
+You can add, subtract and multiply animation values to create more complex animations.
+Consider the following example:
+
+```tsx twoslash title="Enter and exit"
+import { spring, useCurrentFrame, useVideoConfig } from "remotion";
+
+const frame = useCurrentFrame();
+const { fps, durationInFrames } = useVideoConfig();
+
+const enter = spring({
+ fps,
+ frame,
+ config: {
+ damping: 200,
+ },
+});
+
+const exit = spring({
+ fps,
+ config: {
+ damping: 200,
+ },
+ durationInFrames: 20,
+ delay: durationInFrames - 20,
+ frame,
+});
+
+const scale = enter - exit;
+```
+
+- At the beginning of the animation, the value of `enter` is `0`, it goes to `1` over the course of the animation.
+- Before the sequence ends, we create an `exit` animation that goes from `0` to `1`.
+- Subtracting the `exit` animation from the `enter` animation gives us the overall state of the animation which we use to animate `scale`.
+
+<Demo type="animation-math" />
+
+```tsx twoslash title="Full snippet"
+import React from "react";
+import {
+ AbsoluteFill,
+ spring,
+ useCurrentFrame,
+ useVideoConfig,
+} from "remotion";
+
+export const AnimationMath: React.FC = () => {
+ const frame = useCurrentFrame();
+ const { fps, durationInFrames } = useVideoConfig();
+
+ const enter = spring({
+ fps,
+ frame,
+ config: {
+ damping: 200,
+ },
+ });
+
+ const exit = spring({
+ fps,
+ config: {
+ damping: 200,
+ },
+ durationInFrames: 20,
+ delay: durationInFrames - 20,
+ frame,
+ });
+
+ const scale = enter - exit;
+
+ return (
+ <AbsoluteFill
+ style={{
+ justifyContent: "center",
+ alignItems: "center",
+ backgroundColor: "white",
+ }}
+ >
+ <div
+ style={{
+ height: 100,
+ width: 100,
+ backgroundColor: "#4290f5",
+ borderRadius: 20,
+ transform: `scale(${scale})`,
+ justifyContent: "center",
+ alignItems: "center",
+ display: "flex",
+ fontSize: 50,
+ color: "white",
+ }}
+ >
+ {frame}
+ </div>
+ </AbsoluteFill>
+ );
+};
+```
diff --git a/packages/docs/docs/miscellaneous/snippets/adding-animations.md b/packages/docs/docs/miscellaneous/snippets/adding-animations.md
deleted file mode 100644
index 84e74b31d3..0000000000
--- a/packages/docs/docs/miscellaneous/snippets/adding-animations.md
+++ /dev/null
@@ -1,85 +0,0 @@
----
-image: /generated/articles-docs-miscellaneous-snippets-adding-animations.png
-title: Adding and subtracting animations
-crumb: "Snippets"
----
-
-<div style={{height: 200}}>
-<video style={{
- width: 200,
- height: 200,
- boxShadow: 'var(--box-shadow)',
- float: 'left',
- borderRadius: 4,
- marginRight: 10
-}} src="/img/pushes.mp4" autoPlay muted loop></video>
-
-<p>This example demonstrates that animation values can be added, subtracted, multiplied and divided with each other to create more complex animations. The standard JavaScript operators like <code>+</code> and <code>-</code> may be used.</p>
-</div>
-<br/>
-
-```tsx twoslash
-import {
- AbsoluteFill,
- interpolate,
- spring,
- useCurrentFrame,
- useVideoConfig,
-} from "remotion";
-
-export const TwoPushes: React.FC = () => {
- const frame = useCurrentFrame();
- const { fps } = useVideoConfig();
-
- const scalePush1 = spring({
- fps,
- frame,
- config: {
- damping: 200,
- },
- });
-
- const scalePush2 = spring({
- fps,
- frame: frame - 30,
- config: {
- damping: 200,
- },
- });
-
- const scalePush3 = spring({
- fps,
- frame: frame - 60,
- config: {
- damping: 200,
- },
- });
-
- const scale = scalePush1 + scalePush2 + scalePush3;
-
- const left =
- interpolate(scalePush1, [0, 1], [0, 80]) +
- interpolate(scalePush2, [0, 1], [0, 80]) +
- interpolate(scalePush3, [0, 1], [0, 80]);
-
- return (
- <AbsoluteFill
- style={{
- justifyContent: "center",
- padding: 50,
- backgroundColor: "white",
- }}
- >
- <div
- style={{
- height: 100,
- width: 100,
- backgroundColor: "#4290f5",
- borderRadius: 40,
- transform: `scale(${scale}) translateX(${left}px)`,
- }}
- />
- </AbsoluteFill>
- );
-};
-```
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index d1f2196cf7..b7fe8f53d2 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -582,6 +582,7 @@ module.exports = {
"audio-visualization",
"noise-visualization",
"video-manipulation",
+ "animation-math",
],
},
{
@@ -821,7 +822,6 @@ module.exports = {
"miscellaneous/snippets/player-in-iframe",
"miscellaneous/snippets/fps-converter",
"miscellaneous/snippets/use-delay-render",
- "miscellaneous/snippets/adding-animations",
"miscellaneous/snippets/offthread-video-while-rendering",
"miscellaneous/snippets/hls",
"miscellaneous/snippets/combine-compositions",
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index 79e7706ffd..493187e77f 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -62,6 +62,13 @@ export const articles = [
compId: "articles-docs-animating-properties",
crumb: "The basics",
},
+ {
+ id: "animation-math",
+ title: "Animation math",
+ relativePath: "docs/animation-math.mdx",
+ compId: "articles-docs-animation-math",
+ crumb: "Techniques",
+ },
{
id: "animation-utils/index",
title: "@remotion/animation-utils",
@@ -1735,13 +1742,6 @@ export const articles = [
compId: "articles-docs-miscellaneous-snippets-accelerated-video",
crumb: "Snippets",
},
- {
- id: "miscellaneous/snippets/adding-animations",
- title: "Adding and subtracting animations",
- relativePath: "docs/miscellaneous/snippets/adding-animations.md",
- compId: "articles-docs-miscellaneous-snippets-adding-animations",
- crumb: "Snippets",
- },
{
id: "miscellaneous/snippets/align-duration",
title: "How do I make the composition the same duration as my video?",
diff --git a/packages/docs/static/generated/articles-docs-animation-math.png b/packages/docs/static/generated/articles-docs-animation-math.png
new file mode 100644
index 0000000000..670b4e14f3
Binary files /dev/null and b/packages/docs/static/generated/articles-docs-animation-math.png differ
diff --git a/packages/docs/static/img/pushes.mp4 b/packages/docs/static/img/pushes.mp4
deleted file mode 100644
index 014baf5c2f..0000000000
Binary files a/packages/docs/static/img/pushes.mp4 and /dev/null differ
diff --git a/vercel.json b/vercel.json
index bd368deecb..df461470b6 100644
--- a/vercel.json
+++ b/vercel.json
@@ -321,6 +321,11 @@
"source": "/recorder",
"destination": "/docs/recorder",
"permanent": false
+ },
+ {
+ "source": "/docs/miscellaneous/snippets/adding-animations",
+ "destination": "/docs/animation-math",
+ "permanent": false
}
]
}