通过以特定方式编写 Remotion 标记,Remotion Studio 能够识别代码结构并使其具有交互性:
- 允许通过点击选择项目
- 允许拖放、调整大小和旋转
- 编辑 CSS 样式
- 使关键帧和缓动值可编辑
如果标记过于复杂,Studio 无法使其交互,则相关值将变为灰色。
优先使用具有自身时间线的交互式组件
对自定义场景、卡片、标题以及其他视觉组件使用 Interactive.withSchema({wrapInSequence: true}),这些组件的属性应按实例可编辑。在 InteractivitySchema 中暴露有意义的内容和外观控制。将装饰性的实现细节保留在组件内部。
该组件必须接受 style 并将其转发到一个视觉根元素。当变换、裁剪或不透明度应影响所有层时,保持一个共享的根元素。当多余的内层包装器的样式可以移动到现有元素上而不改变布局或动画时,将其扁平化。
import type React from 'react';
import {Interactive, type InteractivitySchema} from 'remotion';
type LowerThirdProps = {
readonly children: string;
readonly accentColor: string;
readonly style?: React.CSSProperties;
};
const LowerThirdInner: React.FC<LowerThirdProps> = ({
children,
accentColor,
style,
}) => {
return (
<Interactive.Div
style={{
position: 'absolute',
left: 80,
bottom: 80,
display: 'flex',
alignItems: 'center',
gap: 20,
backgroundColor: 'white',
borderRadius: 16,
padding: '20px 32px',
color: 'black',
fontFamily: 'Helvetica, Arial, sans-serif',
fontSize: 48,
fontWeight: 600,
...style,
}}
>
<div
style={{
width: 8,
alignSelf: 'stretch',
borderRadius: 4,
backgroundColor: accentColor,
}}
/>
{children}
</Interactive.Div>
);
};
const lowerThirdSchema = {
children: {type: 'text-content', default: '', description: '文本'},
accentColor: {
type: 'color',
default: '#0b84f3',
description: '强调色',
},
} as const satisfies InteractivitySchema;
export const LowerThird = Interactive.withSchema({
Component: LowerThirdInner,
componentName: '<LowerThird>',
schema: lowerThirdSchema,
wrapInSequence: true,
});
组件内部需要转发注入的 style;在组件的调用点保持可编辑样式内联。
选择 Schema 字段
Schema 中的每个键都是 Studio 可以在调用点读取和编辑的属性。不在 schema 中的属性仍然有效,但不能在 Studio 中编辑。键可以使用点表示法,例如 style.color。
| 类型 | 用途 | 可关键帧化 |
|---|---|---|
text-content | 文本,例如标题 | 否 |
number | 数字,带有可选的 min、max、step、integer | 是 |
boolean | 开/关开关 | 是(保持) |
color | CSS 颜色字符串 | 是 |
enum | 在 variants 之间选择,每个都有其自己的嵌套 schema | 选择加入(保持) |
array | 数字、颜色、枚举等的列表 | 否 |
asset | 媒体源,带有可选的 assetType | 否 |
font-family | CSS 字体系列 | 否 |
font-weight | 字体粗细 | 是 |
translate、scale、rotation-css、rotation-degrees、transform-origin | 变换 | 是 |
uv-coordinate | 元素上的归一化 [x, y] 点 | 是 |
svg-path | SVG 路径数据 | 是 |
remotion-captions | Caption[] 数据 | 否 |
hidden | 保持在控件之外的值 | — |
每个字段都需要一个 default 值,并且应有一个 description,Studio 会将其显示为标签。
设置 keyframable: false 以显示静态控件。
暴露内容和每个实例的外观,例如文本和强调色。
不要为 style.translate、style.scale、style.rotate、style.transformOrigin 和 style.opacity 添加字段:wrapInSequence: true 已添加它们。
要使根的 CSS 属性按实例可编辑,可以展开内置的 schema 片段:
const cardSchema = {
...Interactive.textSchema, // style.color, style.fontSize, style.fontWeight, ...
...Interactive.backgroundSchema, // style.backgroundColor
...Interactive.borderRadiusSchema, // style.borderRadius, ...
} as const satisfies InteractivitySchema;
也可用:Interactive.borderSchema,以及用于接受字幕的组件的 Interactive.captionsSchema。
有关所有选项,请参见 InteractivitySchema。
将可复用组件注册为关联合成
将具有自己层或动画的重要场景和可复用组件注册为独立的合成。Studio 将这些称为关联合成:它们可以在自己的时间线中打开,同时与父视频共享相同的组件实现。
注册父组件渲染的同一个导出组件引用。对于上面的示例,使用 component={LowerThird},而不是 LowerThirdInner 或内联包装器。
使用 wrapInSequence: true,关联不需要额外的 <Sequence>。
import {Composition} from 'remotion';
import {LowerThird} from './LowerThird';
export const RemotionRoot = () => (
<Composition
id="LowerThird"
component={LowerThird}
width={1280}
height={720}
fps={30}
durationInFrames={30}
defaultProps={{
children: '简·多伊,产品设计师',
accentColor: '#0b84f3',
}}
/>
);
选择唯一的 ID、代表性的内联默认值以及适合预览组件的尺寸、fps 和时长。注册默认值不会自动从父实例复制属性。请参阅关联合成以了解注册和提取细节。
直接将时序放在组件上
避免在一个已经接受时序属性的组件周围使用单独的 <Sequence>。使用 wrapInSequence: true 构建的内置交互组件和自定义组件可以直接接收 name、from、durationInFrames、trimBefore、playbackRate 和 premountFor。
<LowerThird
name="底部三分之一"
from={30}
durationInFrames={90}
accentColor="#0b84f3"
>
简·多伊,产品设计师
</LowerThird>
移除冗余序列时,将其时序和名称移动到子组件上。
保留任何现有的子组件时序;不要覆盖或双重应用偏移。
组件内部的 useCurrentFrame() 调用读取其本地帧,而调用点上的样式表达式仍使用调用者的帧。移动动画样式时请保留这种区别。
当 <Sequence> 为多个兄弟组件提供共享时序、覆盖 useVideoConfig() 的尺寸,或包装一个不处理时序的组件时,保留此 Sequence。
对于连续布局,保留 <Series.Sequence>;对于过渡动画,保留 <TransitionSeries.Sequence>;直接的 from 属性不能替代这些行为。
为每个独立可编辑项赋予其自己的 JSX 节点
Studio 编辑创建项的 JSX 源节点。如果多个运行时项来自同一个 JSX 节点,它们共享一个源编辑目标。
对于每个应单独可编辑的合成注册、剪辑、场景、层或序列,编写一个单独的 JSX 节点,并将其可编辑属性保留在该节点上。这适用于 <Composition>、<Still>、内置媒体组件、<Sequence>、<Series.Sequence>、<TransitionSeries.Sequence> 和自定义组件。
例如,像这样编写一个可编辑的时间线:
<Series>
<Series.Sequence name="介绍" durationInFrames={90}>
<Introduction />
</Series.Sequence>
<Series.Sequence name="演示" durationInFrames={150}>
<Demo />
</Series.Sequence>
</Series>
使用 .map() 或其他编程循环会从一个 JSX 源节点创建多个运行时项。这适用于有意作为一个模板控制的重复输出,例如可视化条形图或粒子。当实例需要在 Studio 中独立 ID 或名称、属性、元数据、时序、排序、删除或复制时,则不适合。
使用 Interactive 使 HTML 元素可交互
每个 HTML 和 SVG 元素(<Img> 除外,它已经是交互式)都可以使用 Interactive 变成交互式:
<Interactive.Div
name="问候卡片"
style={{fontSize: 80, padding: 24}}
>
你好
</Interactive.Div>
这样可以在 Studio 中设置样式和关键帧。要明智使用,如果一个组件有很多元素,时间线可能会变得混乱。
优先使用内联文本
如果文本是固定的且仅使用一次,直接将其写在交互式元素内部,而不是提取到常量中。
// 👍 固定副本保持可编辑
<Interactive.Div name="标题">
Remotion 最佳实践
</Interactive.Div>
仅在文本是动态或可复用时才使用属性或变量。
为交互式元素赋予描述性名称
为元素添加 name 属性,使其易于识别。
避免使用计算名称,直接硬编码它们。
<>
<Interactive.Div name="主标题" style={{fontSize: 80}}>
发布日
</Interactive.Div>
<Img name="头像" src="https://remotion.media/image.jpeg" />
<Video name="背景" src="https://remotion.media/video.mp4" />
<Interactive.Div name="标题">
发布日
</Interactive.Div>
</>
保持所有 CSS 样式内联
最好的方法是直接将一个普通对象传递给 style——不要引用常量、不要展开对象、不要进行数学运算。
<Interactive.Div
style={{
fontSize: 80,
color: 'red',
}}
>
你好,世界!
</Interactive.Div>
const baseStyle = useMemo(() => {
return {
fontSize: 12 // ❌ 非内联样式不受支持
}
}, []);
<Interactive.Div
style={{
...baseStyle, // ❌ 展开不受支持
color: RED, // ❌ 引用常量不受支持
scale: frame * 10 // ❌ 数学运算不受支持
}}
>
你好,世界!
</Interactive.Div>
使用 interpolate() 进行动画处理
在需要变化的属性上编写内联的 interpolate() 调用来实现动画。
输出范围、缓动、外推和 output 属性应使用硬编码值。
输入范围还可以使用直接从 useVideoConfig() 解构的 durationInFrames、fps、width 和 height。支持裸标识符,例如 durationInFrames、与数字相乘,例如 2 * fps 或 fps * 2,以及减去一个数字,例如 durationInFrames - 1。
const {fps, durationInFrames} = useVideoConfig();
// 👍 内联值可以标准化和关键帧化
<Interactive.Div
name="产品卡片"
style={{
color: 'white',
fontSize: 80,
scale: interpolate(frame, [0, fps], [0, 1], {
easing: Easing.spring({damping: 200}),
output: 'perceptual-scale',
extrapolateLeft: 'clamp',
extrapolateRight: 'clamp'
}),
rotate: interpolate(frame, [0, 1 * fps], ['0deg', '20deg'], {
easing: Easing.spring({damping: 200}),
extrapolateLeft: 'clamp',
extrapolateRight: 'clamp'
}),
translate: interpolate(
frame,
[durationInFrames - 30, durationInFrames],
['0px 0px', '0px 120px'],
{
easing: Easing.spring({damping: 200}),
output: 'perceptual-scale',
extrapolateLeft: 'clamp',
extrapolateRight: 'clamp'
}
),
}}
/>
const translateY = interpolate(frame, [0, 30], [0, 120]); // ❌ 数学运算应直接在标记中
<Interactive.Div
name="产品卡片"
style={{
translate: translateY, // ❌ 仅支持内联 interpolate() 调用
rotate: interpolate(frame, [start, start + 10], [0, Math.PI]), // ❌ 不能使用任意变量的数学运算,不能使用常量
scale: interpolate(anyVariable, [0, 30], [0, 1]) // ❌ 只能解释 `frame` 变量
}}
/>
保持 SVG 路径精简
… (由于原文截断,此处省略后续内容)
