Skip to content

🎥 Make videos programmatically with React

useCurrentScale()

Jonny Burgerb17c9bf45387
diff --git a/packages/docs/docs/measuring.md b/packages/docs/docs/measuring.md
index c94d9f8992..a130a9237e 100644
--- a/packages/docs/docs/measuring.md
+++ b/packages/docs/docs/measuring.md
@@ -10,6 +10,47 @@ If you want to measure a DOM node, you can assign a [React Ref](https://react.de
In Remotion, it is not that easy because the `<div>` element in which the video is rendered into has a `scale()` transform applied to it, which affects the value that you get from `getBoundingClientRect()`.
+## Measure using the `useCurrentScale()` hook
+
+From v4.0.111 on, you can use the [`useCurrentScale()`](/docs/use-current-scale) hook to correct the dimensions of the element.
+
+```tsx twoslash title="MyComponent.tsx"
+import { useCallback, useEffect, useState, useRef } from "react";
+import { useCurrentScale } from "remotion";
+
+export const MyComponent = () => {
+ const ref = useRef<HTMLDivElement>(null);
+
+ const [dimensions, setDimensions] = useState<{
+ correctedHeight: number;
+ correctedWidth: number;
+ } | null>(null);
+
+ const scale = useCurrentScale();
+
+ useEffect(() => {
+ if (!ref.current) {
+ return;
+ }
+
+ const rect = ref.current.getBoundingClientRect();
+
+ setDimensions({
+ correctedHeight: rect.height / scale,
+ correctedWidth: rect.width / scale,
+ });
+ }, [scale]);
+
+ return (
+ <div>
+ <div ref={ref}>Hello World!</div>
+ </div>
+ );
+};
+```
+
+## Versions prior to v4.0.110
+
To get an accurate measurement, you can render an additional element with a fixed width (say `10px`) and measure it too. Then, you can divide the width of the element by `10` to get the scale factor.
```tsx twoslash title="MyComponent.tsx"
@@ -57,16 +98,17 @@ export const MyComponent = () => {
};
```
+### Example project
+
+- [Source code](https://github.com/remotion-dev/measure-item)
+- [Preview](https://measure-item.vercel.app)
+
## Versions prior to v4.0.103
In previous versions of Remotion, `getBoundingClientRect()` could return dimensions with all values being `0` in the first `useEffect()` due to mounting your component but not showing it.
Going forward, you can rely on the dimensions being non-zero.
-## Example project
-
-- [Source code](https://github.com/remotion-dev/measure-item)
-- [Preview](https://measure-item.vercel.app)
-
## See also
+
- [react-use-measure](https://github.com/pmndrs/react-use-measure) - Measure elements correctly inside a scroll container
diff --git a/packages/docs/docs/use-current-scale.md b/packages/docs/docs/use-current-scale.md
deleted file mode 100644
index 7907bb6cc4..0000000000
--- a/packages/docs/docs/use-current-scale.md
+++ /dev/null
@@ -1,54 +0,0 @@
----
-image: /generated/articles-docs-use-current-scale.png
-title: useCurrentScale()
-id: use-current-scale
-crumb: "API"
----
-
-With this hook, you can retrieve the scale factor of the canvas.
-In the Studio, it will correspond to the zoom level - the value is `1` if the zoom is at 100%.
-
-```tsx twoslash
-import { Sequence, useCurrentScale } from "remotion";
-
-const MyVideo = () => {
- const scale = useCurrentScale({1});
-
- return (
- <div>{scale}</div>
- );
-};
-```
-
-Using `<Sequence />`'s, you can compose multiple elements together and time-shift them independently from each other without changing their animation.
-
-### Getting the absolute frame of the timeline
-
-In rare circumstances, you want access to the absolute frame of the timeline inside a sequence, use `useCurrentFrame()` at the top-level component and then pass it down as a prop to the children of the `<Sequence />`.
-
-```tsx twoslash
-import { Sequence, useCurrentFrame } from "remotion";
-
-// ---cut---
-
-const Subtitle: React.FC<{ absoluteFrame: number }> = ({ absoluteFrame }) => {
- console.log(useCurrentFrame()); // 15
- console.log(absoluteFrame); // 25
-
- return null;
-};
-
-const MyVideo = () => {
- const frame = useCurrentFrame(); // 25
-
- return (
- <Sequence from={10}>
- <Subtitle absoluteFrame={frame} />
- </Sequence>
- );
-};
-```
-
-## See also
-
-- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/core/src/use-current-scale.ts)
diff --git a/packages/docs/docs/use-current-scale.mdx b/packages/docs/docs/use-current-scale.mdx
new file mode 100644
index 0000000000..bfe0434d1b
--- /dev/null
+++ b/packages/docs/docs/use-current-scale.mdx
@@ -0,0 +1,43 @@
+---
+image: /generated/articles-docs-use-current-scale.png
+title: useCurrentScale()
+id: use-current-scale
+crumb: "API"
+---
+
+# useCurrentScale()<AvailableFrom v="4.0.111" />
+
+With this hook, you can retrieve the scale factor of the canvas.
+Useful for if you want to [measure DOM nodes](/docs/measuring).
+
+In the [Studio](/docs/terminology/studio), it will correspond to the zoom level - the value is `1` if the zoom is at 100%.
+In the [Player](/docs/terminology/player), it will correspond to the scale factor of the video.
+
+```tsx twoslash title="MyComp.tsx"
+import { Sequence, useCurrentScale } from "remotion";
+
+const MyVideo = () => {
+ const scale = useCurrentScale();
+
+ return <div>The current scale is {scale}</div>;
+};
+```
+
+If you are outside of a Remotion context, the hook will throw an error.
+If you want to ignore the fact that you are outside of a Remotion context, you can pass `true` as the first argument to the hook.
+The hook will always return `1` in this case.
+
+```tsx twoslash title="MyComp.tsx"
+import { useCurrentScale } from "remotion";
+
+const MyRegularReactComponent = () => {
+ const scale = useCurrentScale({ dontThrowIfOutsideOfRemotion: true });
+
+ return <div>The current scale is {scale}</div>;
+};
+```
+
+## See also
+
+- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/core/src/use-current-scale.ts)
+- [Measuring DOM nodes](/docs/measuring)