Skip to content

🎥 Make videos programmatically with React

cool

Jonny Burger6218222ae82d
diff --git a/packages/docs/src/remotion/HomepageVideo/Card.tsx b/packages/docs/src/remotion/HomepageVideo/Card.tsx
index b0de43001e..e0c62105ab 100644
--- a/packages/docs/src/remotion/HomepageVideo/Card.tsx
+++ b/packages/docs/src/remotion/HomepageVideo/Card.tsx
@@ -1,4 +1,5 @@
import React, { useCallback } from "react";
+import { spring } from "remotion";
export const paddingAndMargin = 20;
const height = 360;
@@ -14,35 +15,160 @@ const getPositionForIndex = (index: number) => {
return { x, y };
};
+const getInitialPositions = () => {
+ return new Array(4).fill(true).map((_, i) => getPositionForIndex(i));
+};
+
+const positions = getInitialPositions();
+let shouldBePositions = getInitialPositions();
+
+const getIndexFromPosition = (clientX: number, clientY: number) => {
+ const left = clientX < cardWidth / 2 + paddingAndMargin;
+ const top = clientY < cardHeight / 2 + paddingAndMargin;
+
+ if (left && top) {
+ return 0;
+ }
+
+ if (!left && top) {
+ return 1;
+ }
+
+ if (left && !top) {
+ return 2;
+ }
+
+ if (!left && !top) {
+ return 3;
+ }
+};
+
+const arePositionsEqual = (a: typeof positions, b: typeof positions) => {
+ return a.every((pos, i) => {
+ return pos.x === b[i].x && pos.y === b[i].y;
+ });
+};
+
export const Card: React.FC<{
index: number;
- refToUse: React.MutableRefObject<HTMLDivElement>;
-}> = ({ index, refToUse }) => {
+ refsToUse: React.MutableRefObject<HTMLDivElement>[];
+}> = ({ index, refsToUse }) => {
+ const refToUse = refsToUse[index];
+
+ const applyPositions = useCallback(
+ (except: number | null) => {
+ positions.forEach((_, i) => {
+ const shouldBe = shouldBePositions[i];
+ const ref = refsToUse[i].current;
+ if (!ref) {
+ return;
+ }
+
+ if (i === except) {
+ ref.style.left = getPositionForIndex(i).x + "px";
+ ref.style.top = getPositionForIndex(i).y + "px";
+ return;
+ }
+
+ ref.style.zIndex = "1";
+ let animationI = 0;
+ const duration = 60;
+
+ const releasePositionX = positions[i].x;
+ const releasePositionY = positions[i].y;
+
+ const update = () => {
+ const progress = spring({
+ fps: 30,
+ frame: animationI,
+ config: {
+ damping: 200,
+ },
+ });
+ const newX =
+ progress * (shouldBe.x - releasePositionX) + releasePositionX;
+ const newY =
+ progress * (shouldBe.y - releasePositionY) + releasePositionY;
+ ref.style.left = `${newX}px`;
+ ref.style.top = `${newY}px`;
+ positions[i] = {
+ x: newX,
+ y: newY,
+ };
+ animationI++;
+ if (animationI === duration) {
+ return;
+ }
+
+ requestAnimationFrame(update);
+ };
+
+ update();
+ });
+ },
+ [refsToUse],
+ );
+
const onPointerDown = useCallback(
(e: React.PointerEvent<HTMLDivElement>) => {
e.currentTarget.setPointerCapture(e.pointerId);
+ const cardLeft = refToUse.current.offsetLeft;
+ const cardTop = refToUse.current.offsetTop;
const startX = e.clientX;
const startY = e.clientY;
+ let translateX = 0;
+ let translateY = 0;
const onMove = (evt: PointerEvent) => {
- refToUse.current.style.transform = `translateX(${
- evt.clientX - startX
- }px) translateY(${evt.clientY - startY}px)`;
- refToUse.current.style.zIndex = "2";
+ const scale =
+ refToUse.current.getBoundingClientRect().width / cardWidth;
+
+ translateX = (evt.clientX - startX) * (1 / scale);
+ translateY = (evt.clientY - startY) * (1 / scale);
- refToUse.current.addEventListener(
- "pointerup",
- () => {
- refToUse.current.style.transform = `translateX(0px) translateY(0px)`;
- refToUse.current.removeEventListener("pointermove", onMove);
- },
- { once: true },
+ const position = getIndexFromPosition(
+ cardLeft + translateX,
+ cardTop + translateY,
);
+
+ if (position === index) {
+ if (!arePositionsEqual(getInitialPositions(), shouldBePositions)) {
+ shouldBePositions = getInitialPositions();
+ applyPositions(index);
+ }
+ } else {
+ const newPos = getInitialPositions();
+ newPos[position] = getPositionForIndex(index);
+ newPos[index] = getPositionForIndex(position);
+ if (!arePositionsEqual(newPos, shouldBePositions)) {
+ shouldBePositions = newPos;
+ applyPositions(index);
+ }
+ }
+
+ refToUse.current.style.transform = `translateX(${translateX}px) translateY(${translateY}px)`;
+ refToUse.current.style.zIndex = "2";
};
+ refToUse.current.addEventListener(
+ "pointerup",
+ () => {
+ positions[index] = {
+ x: cardLeft + translateX,
+ y: cardTop + translateY,
+ };
+ refToUse.current.style.left = `${cardLeft + translateX}px`;
+ refToUse.current.style.top = `${cardTop + translateY}px`;
+ refToUse.current.style.transform = `translateX(0px) translateY(0px)`;
+ refToUse.current.removeEventListener("pointermove", onMove);
+ applyPositions(null);
+ },
+ { once: true },
+ );
+
refToUse.current.addEventListener("pointermove", onMove);
},
- [refToUse],
+ [applyPositions, index, refToUse],
);
const onPointerUp = useCallback((e: React.PointerEvent<HTMLDivElement>) => {
diff --git a/packages/docs/src/remotion/HomepageVideo/Comp.tsx b/packages/docs/src/remotion/HomepageVideo/Comp.tsx
index fd5d0dcd73..0a0d5be7c1 100644
--- a/packages/docs/src/remotion/HomepageVideo/Comp.tsx
+++ b/packages/docs/src/remotion/HomepageVideo/Comp.tsx
@@ -16,10 +16,10 @@ export const Comp: React.FC = () => {
backgroundColor: "white",
}}
>
- <Card refToUse={refs[0]} index={0} />
- <Card refToUse={refs[1]} index={1} />
- <Card refToUse={refs[2]} index={2} />
- <Card refToUse={refs[3]} index={3} />
+ <Card refsToUse={refs} index={0} />
+ <Card refsToUse={refs} index={1} />
+ <Card refsToUse={refs} index={2} />
+ <Card refsToUse={refs} index={3} />
</AbsoluteFill>
);
};