Skip to content

🎥 Make videos programmatically with React

`remotion`: Fix float imprecisions for <Sequence> + add test

Jonny Burgerff97bf815f23
diff --git a/packages/core/src/Sequence.tsx b/packages/core/src/Sequence.tsx
index 66a04c1e4b..615cc2643d 100644
--- a/packages/core/src/Sequence.tsx
+++ b/packages/core/src/Sequence.tsx
@@ -171,7 +171,9 @@ const SequenceRefForwardingFunction: React.ForwardRefRenderFunction<
loopDisplay,
]);
- const endThreshold = cumulatedFrom + from + durationInFrames - 1;
+ // Ceil to support floats
+ // https://github.com/remotion-dev/remotion/issues/2958
+ const endThreshold = Math.ceil(cumulatedFrom + from + durationInFrames - 1);
const content =
absoluteFrame < cumulatedFrom + from
? null
diff --git a/packages/core/src/test/nested-sequences.test.tsx b/packages/core/src/test/nested-sequences.test.tsx
index 6105f76f13..97cddf74ee 100644
--- a/packages/core/src/test/nested-sequences.test.tsx
+++ b/packages/core/src/test/nested-sequences.test.tsx
@@ -2,14 +2,16 @@
* @vitest-environment jsdom
*/
/* eslint-disable react/jsx-no-constructed-context-values */
-import {render} from '@testing-library/react';
-import {expect, test} from 'vitest';
-import {CanUseRemotionHooksProvider} from '../CanUseRemotionHooks.js';
+import {cleanup, render} from '@testing-library/react';
+import {afterEach, expect, test} from 'vitest';
+import {AbsoluteFill} from '../AbsoluteFill.js';
import {Sequence} from '../Sequence.js';
import {TimelineContext} from '../timeline-position-state.js';
import {useCurrentFrame} from '../use-current-frame.js';
import {WrapSequenceContext} from './wrap-sequence-context.js';
+afterEach(cleanup);
+
test('It should calculate the correct offset in nested sequences', () => {
const NestedChild = () => {
const frame = useCurrentFrame();
@@ -32,34 +34,14 @@ test('It should calculate the correct offset in nested sequences', () => {
);
};
- const {queryByText} = render(
- <WrapSequenceContext>
- <TimelineContext.Provider
- value={{
- rootId: 'hi',
- frame: {
- 'my-comp': 40,
- },
- playing: false,
- imperativePlaying: {
- current: false,
- },
- playbackRate: 1,
- setPlaybackRate: () => {
- throw new Error('playback rate');
- },
- audioAndVideoTags: {
- current: [],
- },
- }}
- >
- <Sequence from={20} durationInFrames={100}>
- <Child />
- </Sequence>
- </TimelineContext.Provider>
- </WrapSequenceContext>,
+ const content = (
+ <Sequence from={20} durationInFrames={100}>
+ <Child />
+ </Sequence>
);
- expect(queryByText(/^frame9$/i)).not.toBe(null);
+ const result = getForFrame(40, content);
+
+ expect(result(/^frame9$/i)).not.toBe(null);
});
test('Negative offset test', () => {
@@ -68,12 +50,27 @@ test('Negative offset test', () => {
return <div>{'frame' + frame}</div>;
};
+ const content = (
+ <Sequence from={-200} durationInFrames={300}>
+ <Sequence from={10} durationInFrames={300}>
+ <Sequence from={10} durationInFrames={300}>
+ <NestedChild />
+ </Sequence>
+ </Sequence>
+ </Sequence>
+ );
+
+ const result = getForFrame(40, content);
+ expect(result(/^frame220/i)).not.toBe(null);
+});
+
+const getForFrame = (frame: number, content: React.ReactNode) => {
const {queryByText} = render(
<WrapSequenceContext>
<TimelineContext.Provider
value={{
frame: {
- 'my-comp': 40,
+ 'my-comp': frame,
},
playing: false,
rootId: 'hi',
@@ -89,19 +86,12 @@ test('Negative offset test', () => {
},
}}
>
- <Sequence from={-200} durationInFrames={300}>
- <Sequence from={10} durationInFrames={300}>
- <Sequence from={10} durationInFrames={300}>
- <NestedChild />
- </Sequence>
- </Sequence>
- </Sequence>
+ {content}
</TimelineContext.Provider>
</WrapSequenceContext>,
);
- const result = queryByText(/^frame220/i);
- expect(result).not.toBe(null);
-});
+ return queryByText;
+};
test('Nested negative offset test', () => {
const NestedChild = () => {
@@ -113,51 +103,20 @@ test('Nested negative offset test', () => {
const endAt = 90;
const content = (
- <WrapSequenceContext>
- <Sequence from={0 - startFrom} durationInFrames={endAt}>
- <NestedChild />
- </Sequence>
- </WrapSequenceContext>
+ <Sequence from={0 - startFrom} durationInFrames={endAt}>
+ <NestedChild />
+ </Sequence>
);
- const getForFrame = (frame: number) => {
- const {queryByText} = render(
- <WrapSequenceContext>
- <TimelineContext.Provider
- value={{
- frame: {
- 'my-comp': frame,
- },
- playing: false,
- rootId: 'hi',
- imperativePlaying: {
- current: false,
- },
- playbackRate: 1,
- setPlaybackRate: () => {
- throw new Error('playback rate');
- },
- audioAndVideoTags: {
- current: [],
- },
- }}
- >
- {content}
- </TimelineContext.Provider>
- </WrapSequenceContext>,
- );
- return queryByText;
- };
-
- const frame0 = getForFrame(0);
+ const frame0 = getForFrame(0, content);
expect(frame0(/^frame40$/i)).not.toBe(null);
- const frame39 = getForFrame(39);
+ const frame39 = getForFrame(39, content);
expect(frame39(/^frame79$/i)).not.toBe(null);
- const frame50 = getForFrame(50);
+ const frame50 = getForFrame(50, content);
expect(frame50(/^frame90$/i)).toBe(null);
});
-test.skip('Negative offset edge case', () => {
+test('Negative offset edge case', () => {
const NestedChild = () => {
const frame = useCurrentFrame();
return <div>{'frame' + frame}</div>;
@@ -167,49 +126,38 @@ test.skip('Negative offset edge case', () => {
const endAt = 90;
const content = (
- <WrapSequenceContext>
- <Sequence from={40}>
- <Sequence from={0 - startFrom} durationInFrames={endAt}>
- <NestedChild />
- </Sequence>
+ <Sequence from={40}>
+ <Sequence from={0 - startFrom} durationInFrames={endAt}>
+ <NestedChild />
</Sequence>
- </WrapSequenceContext>
+ </Sequence>
);
- const getForFrame = (frame: number) => {
- const {queryByText} = render(
- <CanUseRemotionHooksProvider>
- <TimelineContext.Provider
- value={{
- frame: {
- 'my-comp': frame,
- },
- playing: false,
- rootId: 'hi',
- imperativePlaying: {
- current: false,
- },
- playbackRate: 1,
- setPlaybackRate: () => {
- throw new Error('playback rate');
- },
- audioAndVideoTags: {
- current: [],
- },
- }}
- >
- {content}
- </TimelineContext.Provider>
- </CanUseRemotionHooksProvider>,
- );
- return queryByText;
- };
-
- expect(getForFrame(0)(/^frame0/i)).toBe(null);
- expect(getForFrame(10)(/^frame10/i)).toBe(null);
- expect(getForFrame(40)(/^frame40$/i)).not.toBe(null);
- const atFrame80 = getForFrame(80)(/^frame80$/i);
+ expect(getForFrame(40, content)(/^frame40$/i)).not.toBe(null);
+ expect(getForFrame(0, content)(/^frame0/i)).toBe(null);
+ expect(getForFrame(10, content)(/^frame10/i)).toBe(null);
+ const atFrame80 = getForFrame(80, content)(/^frame80$/i);
expect(atFrame80).not.toBe(null);
- const atFrame90 = getForFrame(90)(/^frame90$/i);
+ const atFrame90 = getForFrame(90, content)(/^frame90$/i);
expect(atFrame90).toBe(null);
});
+
+test('Floats', () => {
+ const content = (
+ <AbsoluteFill style={{backgroundColor: 'white'}}>
+ <Sequence from={5.1000000000000005} durationInFrames={128.4}>
+ <h1>One</h1>
+ </Sequence>
+ <Sequence from={133.5} durationInFrames={96.72}>
+ <h1>Two</h1>
+ </Sequence>
+ </AbsoluteFill>
+ );
+ expect(getForFrame(132, content)(/^One$/i)).not.toBe(null);
+ cleanup();
+ expect(getForFrame(133, content)(/^One$/i)).not.toBe(null);
+ cleanup();
+ expect(getForFrame(133, content)(/^Two$/i)).toBe(null);
+ cleanup();
+ expect(getForFrame(134, content)(/^Two$/i)).not.toBe(null);
+});