Skip to main content

useCurrentScale()v4.0.125

使用此钩子,您可以检索画布的缩放因子。
如果您想要测量 DOM 节点,这将非常有用。

Studio中,它将对应于缩放级别 - 如果缩放为 100%,则值为 1
Player中,它将对应于需要将视频画布适配到播放器中所需的比例。

MyComp.tsx
tsx
import { Sequence, useCurrentScale } from "remotion";
 
const MyVideo = () => {
const scale = useCurrentScale();
 
return <div>The current scale is {scale}</div>;
};
MyComp.tsx
tsx
import { Sequence, useCurrentScale } from "remotion";
 
const MyVideo = () => {
const scale = useCurrentScale();
 
return <div>The current scale is {scale}</div>;
};

如果您不在 Remotion 上下文之外,该钩子将引发错误。
如果您想避免此错误并返回默认比例,您可以传递一个带有属性 dontThrowIfOutsideOfRemotion 设置为 true 的选项对象。
在这种情况下,该钩子将返回 1

MyComp.tsx
tsx
import { useCurrentScale } from "remotion";
 
const MyRegularReactComponent = () => {
const scale = useCurrentScale({ dontThrowIfOutsideOfRemotion: true });
 
return <div>The current scale is {scale}</div>;
};
MyComp.tsx
tsx
import { useCurrentScale } from "remotion";
 
const MyRegularReactComponent = () => {
const scale = useCurrentScale({ dontThrowIfOutsideOfRemotion: true });
 
return <div>The current scale is {scale}</div>;
};

另请参阅