Skip to content

🎥 Make videos programmatically with React

persist frame

Jonny Burgerf8e62a7ec79e
diff --git a/packages/cli/src/editor/components/Editor.tsx b/packages/cli/src/editor/components/Editor.tsx
index e84f5b6766..5cfc1baed7 100644
--- a/packages/cli/src/editor/components/Editor.tsx
+++ b/packages/cli/src/editor/components/Editor.tsx
@@ -15,6 +15,7 @@ import {
loadRichTimelineOption,
RichTimelineContext,
} from '../state/rich-timeline';
+import {FramePersistor} from './FramePersistor';
import {SplitterContainer} from './Splitter/SplitterContainer';
import {SplitterElement} from './Splitter/SplitterElement';
import {SplitterHandle} from './Splitter/SplitterHandle';
@@ -98,6 +99,7 @@ export const Editor: React.FC = () => {
<Background>
<Root />
<UpdateCheck />
+ <FramePersistor />
<SplitterContainer
orientation="horizontal"
id="top-to-bottom"
diff --git a/packages/cli/src/editor/components/FramePersistor.tsx b/packages/cli/src/editor/components/FramePersistor.tsx
new file mode 100644
index 0000000000..c64d9bed31
--- /dev/null
+++ b/packages/cli/src/editor/components/FramePersistor.tsx
@@ -0,0 +1,31 @@
+import React, {useEffect} from 'react';
+import {Internals} from 'remotion';
+
+export const getCurrentCompositionFromUrl = () => {
+ return window.location.pathname.substr(1);
+};
+
+export const persistCurrentFrame = (frame: number) => {
+ const currentComposition = getCurrentCompositionFromUrl();
+ if (!currentComposition) {
+ return;
+ }
+
+ localStorage.setItem(
+ `remotion.time.${getCurrentCompositionFromUrl()}`,
+ String(frame)
+ );
+};
+
+export const FramePersistor: React.FC = () => {
+ const [playing] = Internals.Timeline.usePlayingState();
+ const frame = Internals.Timeline.useTimelinePosition();
+
+ useEffect(() => {
+ if (!playing) {
+ persistCurrentFrame(frame);
+ }
+ }, [frame, playing]);
+
+ return null;
+};
diff --git a/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx b/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx
index 814662a062..439f5b7306 100644
--- a/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx
+++ b/packages/cli/src/editor/components/Timeline/TimelineDragHandler.tsx
@@ -3,6 +3,7 @@ import React, {useCallback, useEffect, useState} from 'react';
import {Internals, interpolate} from 'remotion';
import styled from 'styled-components';
import {TIMELINE_PADDING} from '../../helpers/timeline-layout';
+import {persistCurrentFrame} from '../FramePersistor';
import {sliderAreaRef} from './timeline-refs';
const Container = styled.div`
@@ -97,18 +98,33 @@ export const TimelineDragHandler: React.FC = ({children}) => {
[dragging.dragging, seek, left, videoConfig, width]
);
- const onPointerUp = useCallback(() => {
- setDragging({
- dragging: false,
- });
- if (!dragging.dragging) {
- return;
- }
+ const onPointerUp = useCallback(
+ (e: React.PointerEvent<HTMLDivElement>) => {
+ setDragging({
+ dragging: false,
+ });
+ if (!dragging.dragging) {
+ return;
+ }
- if (dragging.wasPlaying) {
- play();
- }
- }, [dragging, play]);
+ if (!videoConfig) {
+ return;
+ }
+
+ const frame = getFrameFromX(
+ e.clientX - left,
+ videoConfig.durationInFrames,
+ width
+ );
+
+ persistCurrentFrame(frame);
+
+ if (dragging.wasPlaying) {
+ play();
+ }
+ },
+ [dragging, left, play, videoConfig, width]
+ );
useEffect(() => {
if (!dragging.dragging) {