Skip to content

🎥 Make videos programmatically with React

alow "duration" to be added to a spring

Jonny Burger57aecf875638
diff --git a/packages/core/src/spring/index.ts b/packages/core/src/spring/index.ts
index 85fff6d9e3..5c42f9c188 100644
--- a/packages/core/src/spring/index.ts
+++ b/packages/core/src/spring/index.ts
@@ -1,4 +1,5 @@
-import type { SpringConfig} from './spring-utils';
+import {measureSpring} from './measure-spring';
+import type {SpringConfig} from './spring-utils';
import {springCalculation} from './spring-utils';
/**
@@ -11,8 +12,8 @@ import {springCalculation} from './spring-utils';
* @param {number} [config.damping=10] How hard the animation decelerates.
* @param {number} [config.stiffness=100] Affects bounciness of the animation.
* @param {boolean} [config.overshootClamping=false] Whether to prevent the animation going beyond the target value.
- * @param {?number} from The initial value of the animation. Default `0`
- * @param {?number} to The end value of the animation. Default `1`
+ * @param {?number} [config.from] The initial value of the animation. Default `0`
+ * @param {?number} [config.to] The end value of the animation. Default `1`
*/
export function spring({
frame,
@@ -20,20 +21,30 @@ export function spring({
config = {},
from = 0,
to = 1,
+ duration,
+ durationThreshold,
}: {
frame: number;
fps: number;
config?: Partial<SpringConfig>;
from?: number;
to?: number;
+ duration?: number;
+ durationThreshold?: number;
}): number {
+ const durationRatio = duration
+ ? duration /
+ measureSpring({fps, config, from, to, threshold: durationThreshold})
+ : 1;
+
const spr = springCalculation({
fps,
- frame,
+ frame: frame / durationRatio,
config,
from,
to,
});
+
if (!config.overshootClamping) {
return spr.current;
}
diff --git a/packages/core/src/spring/measure-spring.ts b/packages/core/src/spring/measure-spring.ts
index 0f211418cd..08a058d48a 100644
--- a/packages/core/src/spring/measure-spring.ts
+++ b/packages/core/src/spring/measure-spring.ts
@@ -1,5 +1,5 @@
import {validateFps} from '../validation/validate-fps';
-import type { SpringConfig} from './spring-utils';
+import type {SpringConfig} from './spring-utils';
import {springCalculation} from './spring-utils';
export function measureSpring({
diff --git a/packages/core/src/test/spring.test.ts b/packages/core/src/test/spring.test.ts
index 2e19704407..0727d82a9f 100644
--- a/packages/core/src/test/spring.test.ts
+++ b/packages/core/src/test/spring.test.ts
@@ -44,3 +44,13 @@ describe('Spring should go to 1', () => {
).toBeCloseTo(1);
});
});
+
+describe('Should be able to set duration for spring', () => {
+ expect(
+ spring({
+ fps: 30,
+ frame: 5,
+ duration: 5,
+ })
+ ).toBe(1);
+});
diff --git a/packages/example/src/Spring/base-spring.tsx b/packages/example/src/Spring/base-spring.tsx
new file mode 100644
index 0000000000..452b947290
--- /dev/null
+++ b/packages/example/src/Spring/base-spring.tsx
@@ -0,0 +1,30 @@
+import React from 'react';
+import {AbsoluteFill, spring, useCurrentFrame, useVideoConfig} from 'remotion';
+
+export const BaseSpring: React.FC = () => {
+ const frame = useCurrentFrame();
+ const {fps} = useVideoConfig();
+ return (
+ <AbsoluteFill
+ style={{
+ justifyContent: 'center',
+ alignItems: 'center',
+ }}
+ >
+ <div
+ style={{
+ height: 200,
+ width: 200,
+ backgroundColor: 'red',
+ borderRadius: 100,
+ transform: `scale(${spring({
+ frame,
+ fps,
+ config: {},
+ duration: 90,
+ })})`,
+ }}
+ />
+ </AbsoluteFill>
+ );
+};
diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsx
index eee5992e5f..8c71de5dca 100644
--- a/packages/example/src/Video.tsx
+++ b/packages/example/src/Video.tsx
@@ -18,6 +18,7 @@ import InfinityVideo from './ReallyLongVideo';
import RemoteVideo from './RemoteVideo';
import {Scripts} from './Scripts';
import {SkipZeroFrame} from './SkipZeroFrame';
+import {BaseSpring} from './Spring/base-spring';
import {SeriesTesting} from './StaggerTesting';
import {StaticDemo} from './StaticServer';
import {TenFrameTester} from './TenFrameTester';
@@ -80,6 +81,16 @@ export const Index: React.FC = () => {
durationInFrames={300}
/>
</Folder>
+ <Folder name="spring">
+ <Composition
+ id="base-spring"
+ component={BaseSpring}
+ width={1080}
+ height={1080}
+ fps={30}
+ durationInFrames={100}
+ />
+ </Folder>
<Folder name="regression-testing">
<Composition
id="missing-img"