🎥 Make videos programmatically with React
Docs: How to measure a DOM Node
Jonny Burger5f9bbf8c81b4
diff --git a/packages/docs/docs/measuring.md b/packages/docs/docs/measuring.mdnew file mode 100644index 0000000000..ed5ab9c73c--- /dev/null+++ b/packages/docs/docs/measuring.md@@ -0,0 +1,69 @@+---+image: /generated/articles-docs-measuring.png+id: measuring+title: Measuring DOM nodes+sidebar_label: Measuring items+crumb: "How To"+---++If you want to measure a DOM node, you can assign a [React Ref](https://react.dev/learn/manipulating-the-dom-with-refs) to it and then use the [`getBoundingClientRect()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect?retiredLocale=de) browser API to get the position and size of the node.++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()`.++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"+import { useCallback, useEffect, useState, useRef } from "react";++const MEASURER_SIZE = 10;++export const MyComponent = () => {+ const ref = useRef<HTMLDivElement>(null);+ const measurer = useRef<HTMLDivElement>(null);++ const [dimensions, setDimensions] = useState<{+ correctedHeight: number;+ correctedWidth: number;+ } | null>(null);++ useEffect(() => {+ if (!ref.current || !measurer.current) {+ return;+ }++ const rect = ref.current.getBoundingClientRect();+ const measurerRect = measurer.current.getBoundingClientRect();+ const scale = MEASURER_SIZE / measurerRect.width;++ setDimensions({+ correctedHeight: rect.height * scale,+ correctedWidth: rect.width * scale,+ });+ }, []);++ return (+ <div>+ <div ref={ref}>Hello World!</div>+ <div+ ref={measurer}+ style={{+ width: MEASURER_SIZE,+ position: "fixed",+ top: -99999,+ }}+ />+ </div>+ );+};+```++## 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)diff --git a/packages/docs/docs/resources.md b/packages/docs/docs/resources.mdindex cc96ee28a1..6a2411b249 100644--- a/packages/docs/docs/resources.md+++ b/packages/docs/docs/resources.md@@ -99,6 +99,7 @@ This list tries to compile all templates, libraries, building blocks and example- [Jump cuts without re-mounting](https://github.com/remotion-dev/video-with-jump-cuts)- [CSS animations](https://stackblitz.com/~/github.com/remotion-dev/css-animation-play-state)- [Globe.gl example](https://github.com/alexfernandez803/remotion-globegl)+- Measuring the size of a DOM node ([Source code](https://github.com/remotion-dev/measure-item), [Preview](https://measure-item.vercel.app))## Videosdiff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.jsindex 418fe75c34..65934fbb4c 100644--- a/packages/docs/sidebars.js+++ b/packages/docs/sidebars.js@@ -509,6 +509,7 @@ module.exports = {"audio-visualization","noise-visualization","video-manipulation",+ "measuring",],},{diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.tsindex c0b887391c..5168e2abe0 100644--- a/packages/docs/src/data/articles.ts+++ b/packages/docs/src/data/articles.ts@@ -1511,6 +1511,13 @@ export const articles = [compId: "articles-docs-measure-spring",crumb: "API",},+ {+ id: "measuring",+ title: "Measuring DOM nodes",+ relativePath: "docs/measuring.md",+ compId: "articles-docs-measuring",+ crumb: "How To",+ },{id: "media-playback-error",title: "Could not play video/audio with src",diff --git a/packages/docs/static/generated/articles-docs-measuring.png b/packages/docs/static/generated/articles-docs-measuring.pngnew file mode 100644index 0000000000..8d57ed73dbBinary files /dev/null and b/packages/docs/static/generated/articles-docs-measuring.png differ