结构化 Remotion 标记以实现交互性
通过以特定方式编写 Remotion 标记,Remotion Studio 能够识别代码结构并使其具有交互性:
- 允许通过点击选择项目
- 允许拖放、调整大小和旋转
- 编辑 CSS 样式
- 使关键帧和缓动值可编辑
如果标记对于 Studio 来说过于复杂而无法使其交互,则这些值会变为灰色。
优先使用具有自己时间线的交互式组件
对于自定义场景、卡片、标题以及其他每个实例的 props 都应可编辑的视觉组件,请使用 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: 'Text'},
accentColor: {
type: 'color',
default: '#0b84f3',
description: 'Accent color',
},
} as const satisfies InteractivitySchema;
export const LowerThird = Interactive.withSchema({
Component: LowerThirdInner,
componentName: '<LowerThird>',
schema: lowerThirdSchema,
wrapInSequence: true,
});
组件内部需要转发注入的 style;将可编辑的样式内联保留在组件的调用点。
选择 Schema 字段
Schema 中的每个键都是一个 prop,Studio 可以在调用点读取和编辑它。未在 Schema 中的 Props 仍然有效,但在 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: 'Jane Doe, Product Designer',
accentColor: '#0b84f3',
}}
/>
);
选择一个唯一的 ID、有代表性的内联默认值,以及适用于预览组件的尺寸、fps 和时长。注册默认值不会自动从父实例复制 props。有关 注册和提取的详细信息,请参阅连接合成。
直接将时间设置放在组件上
避免在一个已经接受时间 props 的组件周围放置单独的 <Sequence>。使用 wrapInSequence: true 构建的内置交互组件和自定义组件可以直接接收 name, from, durationInFrames, trimBefore, playbackRate 和 premountFor。
<LowerThird
name="Lower third"
from={30}
durationInFrames={90}
accentColor="#0b84f3"
>
Jane Doe, Product Designer
</LowerThird>
当移除冗余序列时,将其时间和名称移动到子组件。保留任何现有的子组件时间;不要覆盖或双重应用偏移。组件内部的 useCurrentFrame() 调用读取其局部帧,而调用点的样式表达式仍使用调用者的帧。在移动动画样式时保留这种区别。
当 <Sequence> 为多个兄弟元素提供共享时间、覆盖 useVideoConfig() 的尺寸或包装不处理时间的组件时,请保留它。为连续布局保留 <Series.Sequence>,为过渡保留 <TransitionSeries.Sequence>;直接的 from props 不会替代这些行为。
为每个可独立编辑的项目提供自己的 JSX 节点
Studio 编辑创建项目的 JSX 源节点。如果多个运行时项目来自同一个 JSX 节点,它们共享一个源编辑目标。
对于每个应该独立编辑的合成注册、剪辑、场景、层或序列,编写一个单独的 JSX 节点并将其可编辑的 props 保留在该节点上。这适用于 <Composition>, <Still>、内置媒体组件、<Sequence>, <Series.Sequence>, <TransitionSeries.Sequence> 和自定义组件。
例如,编写一个可编辑的时间线,如下所示:
<Series>
<Series.Sequence name="Introduction" durationInFrames={90}>
<Introduction />
</Series.Sequence>
<Series.Sequence name="Demo" durationInFrames={150}>
<Demo />
</Series.Sequence>
</Series>
一个 .map() 或其它程序化循环会从一个 JSX 源节点创建多个运行时项目。这对于有意作为单个模板控制的重复输出是合适的,例如可视化条或粒子。当实例需要独立的 ID 或名称、props、元数据、时间、排序、在 Studio 中删除或复制时,则不合适。
使用 Interactive 使 HTML 元素可交互
每个 HTML 和 SVG 元素(<Img> 除外,它已经是交互式的)例如 <div>,都可以使用 Interactive 变得可交互:
<Interactive.Div
name="Greeting card"
style={{fontSize: 80, padding: 24}}
>
Hello
</Interactive.Div>
这允许在 Studio 中设置样式和关键帧。要保持合理,如果一个组件有很多元素,时间线可能会变得混乱。
优先使用内联文本
如果文本是固定的并且只使用一次,请直接将其写在交互式元素内部,而不是将其提取到常量中。
// 👍 固定副本保持可编辑
<Interactive.Div name="Title">
Remotion Best Practices
</Interactive.Div>
仅在文本是动态或可重用时才使用 prop 或变量。
为交互式元素提供描述性名称
为元素添加 name prop 以使其易于识别。避免使用计算名称,请硬编码它们。
<>
<Interactive.Div name="Hero title" style={{fontSize: 80}}>
Launch day
</Interactive.Div>
<Img name="Avatar" src="https://remotion.media/image.jpeg" />
<Video name="Background" src="https://remotion.media/video.mp4" />
<Interactive.Div name="Title">
Launch day
</Interactive.Div>
</>
将所有 CSS 样式内联
最好的方法是将一个纯对象传递给 style —— 不要引用常量、不要展开对象、不要进行计算。
<Interactive.Div
style={{
fontSize: 80,
color: 'red',
}}
>
Hello World!
</Interactive.Div>
const baseStyle = useMemo(() => {
return {
fontSize: 12 // ❌ 非内联样式不受支持
}
}, []);
<Interactive.Div
style={{
...baseStyle, // ❌ 展开不受支持
color: RED, // ❌ 引用常量不受支持
scale: frame * 10 // ❌ 数学运算不受支持
}}
>
Hello World!
</Interactive.Div>
使用 interpolate() 制作动画
将动画编写为对变化属性的内联 interpolate() 调用。
输出范围、缓动、外推和 output 属性应使用硬编码值。
输入范围还可以额外使用直接从 useVideoConfig() 解构的 durationInFrames, fps, width 和 height。支持纯标识符如 durationInFrames、与数字的乘法如 2 * fps 或 fps * 2,以及数字的减法如 durationInFrames - 1。
const {fps, durationInFrames} = useVideoConfig();
// 👍 内联值可以被标准化和关键帧化
<Interactive.Div
name="Product card"
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="Product card"
style={{
translate: translateY, // ❌ 仅支持内联 interpolate() 调用,
rotate: interpolate(frame, [start, start + 10], [0, Math.PI]), // ❌ 不能使用带有任意变量的数学运算,不能使用常量
scale
…
[截断以进行翻译]
