Skip to content

🎥 Make videos programmatically with React

`@remotion/transitions`: Add possibility to reverse a `springTransition()`

Jonny Burger72170255535d
diff --git a/packages/core/src/spring/index.ts b/packages/core/src/spring/index.ts
index a620bfc5e8..392bb275e1 100644
--- a/packages/core/src/spring/index.ts
+++ b/packages/core/src/spring/index.ts
@@ -1,3 +1,4 @@
+import {interpolate} from '../interpolate.js';
import {validateFrame} from '../validate-frame.js';
import {validateFps} from '../validation/validate-fps.js';
import {validateSpringDuration} from '../validation/validation-spring-duration.js';
@@ -95,19 +96,18 @@ export function spring({
fps,
frame: durationProcessed,
config,
- from,
- to,
});
- if (!config.overshootClamping) {
- return spr.current;
- }
+ const inner = config.overshootClamping
+ ? to >= from
+ ? Math.min(spr.current, to)
+ : Math.max(spr.current, to)
+ : spr.current;
- if (to >= from) {
- return Math.min(spr.current, to);
- }
+ const interpolated =
+ from === 0 && to === 1 ? inner : interpolate(inner, [0, 1], [from, to]);
- return Math.max(spr.current, to);
+ return interpolated;
}
export {measureSpring} from './measure-spring.js';
diff --git a/packages/core/src/spring/measure-spring.ts b/packages/core/src/spring/measure-spring.ts
index 8520778f78..27bc6d7af5 100644
--- a/packages/core/src/spring/measure-spring.ts
+++ b/packages/core/src/spring/measure-spring.ts
@@ -71,8 +71,6 @@ export function measureSpring({
fps,
frame,
config,
- from,
- to,
});
};
diff --git a/packages/core/src/spring/spring-utils.ts b/packages/core/src/spring/spring-utils.ts
index 6982148703..40a7c5d8ab 100644
--- a/packages/core/src/spring/spring-utils.ts
+++ b/packages/core/src/spring/spring-utils.ts
@@ -101,21 +101,17 @@ function advance({
const calculationCache: {[key: string]: AnimationNode} = {};
export function springCalculation({
- from = 0,
- to = 1,
frame,
fps,
config = {},
}: {
- from?: number;
- to?: number;
frame: number;
fps: number;
config?: Partial<SpringConfig>;
}): AnimationNode {
+ const from = 0;
+ const to = 1;
const cacheKey = [
- from,
- to,
frame,
fps,
config.damping,
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index d8ffefb71c..2985b07d3f 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -66,6 +66,7 @@ import {StillZoom} from './StillZoom';
import './style.css';
import {Tailwind} from './Tailwind';
import {TenFrameTester} from './TenFrameTester';
+import {TextStroke} from './TextStroke';
import ThreeBasic from './ThreeBasic';
import {VideoTextureDemo} from './ThreeScene/Scene';
import {Timeout} from './Timeout';
@@ -651,6 +652,14 @@ export const Index: React.FC = () => {
fps={30}
durationInFrames={100}
/>
+ <Composition
+ id="text-stroke"
+ component={TextStroke}
+ width={1280}
+ height={720}
+ fps={30}
+ durationInFrames={100}
+ />
<Composition
id="native-buffer-state"
component={NativeBufferState}
@@ -1093,7 +1102,7 @@ export const Index: React.FC = () => {
</Folder>
<Folder name="Transitions">
<Composition
- id="transition"
+ id="basic-transition"
component={BasicTransition}
fps={30}
height={1080}
diff --git a/packages/example/src/TextStroke/index.tsx b/packages/example/src/TextStroke/index.tsx
new file mode 100644
index 0000000000..36b53ce1b3
--- /dev/null
+++ b/packages/example/src/TextStroke/index.tsx
@@ -0,0 +1,20 @@
+import React from 'react';
+import {AbsoluteFill} from 'remotion';
+
+export const TextStroke: React.FC = () => {
+ return (
+ <AbsoluteFill
+ style={{
+ fontFamily: 'Arial',
+ fontSize: 100,
+ color: 'blue',
+ justifyContent: 'center',
+ alignItems: 'center',
+ WebkitTextStroke: '10px red',
+ paintOrder: 'stroke fill',
+ }}
+ >
+ TextStroke
+ </AbsoluteFill>
+ );
+};
diff --git a/packages/example/src/webpack-override.mjs b/packages/example/src/webpack-override.mjs
index 6980410e66..5945c4a84f 100644
--- a/packages/example/src/webpack-override.mjs
+++ b/packages/example/src/webpack-override.mjs
@@ -24,6 +24,14 @@ const aliases = {
'@remotion/shapes': resolveCwd('@remotion/shapes'),
'@remotion/skia': resolveCwd('@remotion/skia'),
'@remotion/three': resolveCwd('@remotion/three'),
+ '@remotion/transitions/fade': resolveCwd('@remotion/transitions/fade'),
+ '@remotion/transitions/slide': resolveCwd('@remotion/transitions/slide'),
+ '@remotion/transitions/flip': resolveCwd('@remotion/transitions/flip'),
+ '@remotion/transitions/clock-wipe': resolveCwd(
+ '@remotion/transitions/clock-wipe',
+ ),
+ '@remotion/transitions/wipe': resolveCwd('@remotion/transitions/wipe'),
+ '@remotion/transitions': resolveCwd('@remotion/transitions'),
'@remotion/zod-types': resolveCwd('@remotion/zod-types'),
};
diff --git a/packages/transitions/src/presentations/slide.tsx b/packages/transitions/src/presentations/slide.tsx
index dce21bc454..0d44c17dc7 100644
--- a/packages/transitions/src/presentations/slide.tsx
+++ b/packages/transitions/src/presentations/slide.tsx
@@ -34,6 +34,7 @@ const SlidePresentation: React.FC<
presentationProgress === 1
? presentationProgress * 100
: presentationProgress * 100 - epsilon;
+ console.log(presentationProgressWithEpsilonCorrection);
if (presentationDirection === 'exiting') {
switch (direction) {
@@ -43,7 +44,7 @@ const SlidePresentation: React.FC<
};
case 'from-right':
return {
- transform: `translateX(-${presentationProgress * 100}%)`,
+ transform: `translateX(${-presentationProgress * 100}%)`,
};
case 'from-top':
return {
@@ -51,7 +52,7 @@ const SlidePresentation: React.FC<
};
case 'from-bottom':
return {
- transform: `translateY(-${presentationProgress * 100}%)`,
+ transform: `translateY(${-presentationProgress * 100}%)`,
};
default:
throw new Error(`Invalid direction: ${direction}`);
diff --git a/packages/transitions/src/timings/spring-timing.ts b/packages/transitions/src/timings/spring-timing.ts
index 66a632d55e..747d6b2427 100644
--- a/packages/transitions/src/timings/spring-timing.ts
+++ b/packages/transitions/src/timings/spring-timing.ts
@@ -2,21 +2,12 @@ import type {SpringConfig} from 'remotion';
import {measureSpring, spring} from 'remotion';
import type {TransitionTiming} from '../types.js';
-const springWithInvalidArgumentRejection: typeof spring = (args) => {
- if (args.to || args.from) {
- throw new Error(
- 'to / from values are not supported by springWithRoundUpIfThreshold',
- );
- }
-
- return spring(args);
-};
-
export const springTiming = (
options: {
config?: Partial<SpringConfig>;
durationInFrames?: number;
durationRestThreshold?: number;
+ reverse?: boolean;
} = {},
): TransitionTiming => {
return {
@@ -32,12 +23,17 @@ export const springTiming = (
});
},
getProgress: ({fps, frame}) => {
- return springWithInvalidArgumentRejection({
+ const to = options.reverse ? 0 : 1;
+ const from = options.reverse ? 1 : 0;
+ return spring({
fps,
frame,
+ to,
+ from,
config: options.config,
durationInFrames: options.durationInFrames,
durationRestThreshold: options.durationRestThreshold,
+ reverse: options.reverse,
});
},
};