自定义演示文稿
本页面描述了如何为 <TransitionSeries> 创建你自己的自定义效果。
🌐 This page describes how to create your own custom effect for <TransitionSeries>.
概念
🌐 Concept
演示文稿是一个高阶组件,它封装着进入的幻灯片和退出的幻灯片,并根据三个参数实现效果:
🌐 A presentation is a higher order component which wraps around the entering slide as well as the exiting slide and which implements an effect based on three parameters:
当前的presentationProgress,受过渡时间的影响
presentationDirection,可以是 entering 或 exitingpresentationDurationInFrames(自 v4.0.153 起可用)
passedProps
样板
🌐 Boilerplate
自定义展示是一个返回 TransitionPresentation 类型对象的函数。它是一个包含 React 组件(component)和作为 passedProps 传递给组件的 React 属性的对象。
🌐 A custom presentation is a function which returns an object of type TransitionPresentation.
It is an object with a React component (component) and React props which are passed to the component as passedProps.
custom-presentation.tsximport type {TransitionPresentation } from '@remotion/transitions'; typeCustomPresentationProps = {width : number;height : number; }; export constcustomPresentation = (props :CustomPresentationProps , ):TransitionPresentation <CustomPresentationProps > => { return {component :StarPresentation ,props }; };
component 是一个 React 组件,它接收以下属性(props):
🌐 The component is a React component which receives the following props:
children:用于封装的标记
presentationDirection:可以是"entering"或"exiting"presentationProgress:介于0和1之间的数字,表示过渡的进度。
passedProps:传递给展示的自定义属性
StarPresentation.tsximport type {TransitionPresentationComponentProps } from '@remotion/transitions'; import {AbsoluteFill } from 'remotion'; constStarPresentation :React .FC <TransitionPresentationComponentProps <CustomPresentationProps > > = ({children ,presentationDirection ,presentationProgress ,passedProps }) => { return ( <AbsoluteFill > <AbsoluteFill >{children }</AbsoluteFill > </AbsoluteFill > ); };
示例
🌐 Example
以下示例实现了星形遮罩过渡:
🌐 The following example implements a star mask transition:
根据passedProps的height和width,计算可以完全填充画布的星形内半径。
使用
@remotion/shapes计算SVG路径,并从零增长到画布的完整尺寸。 presentationProgress来插值形状尺寸。 @remotion/paths将星形居中画布中。
使用
clipPath裁剪进入的幻灯片。在容器内,children被渲染。
如果
presentationDirection设置为"exiting",则此效果被禁用。
StarPresentation.tsximport {getBoundingBox ,translatePath } from '@remotion/paths'; import {makeStar } from '@remotion/shapes'; import type {TransitionPresentationComponentProps } from '@remotion/transitions'; importReact , {useMemo ,useState } from 'react'; import {AbsoluteFill ,random } from 'remotion'; export typeCustomPresentationProps = {width : number;height : number; }; constStarPresentation :React .FC <TransitionPresentationComponentProps <CustomPresentationProps > > = ({children ,presentationDirection ,presentationProgress ,passedProps }) => { constfinishedRadius =Math .sqrt (passedProps .width ** 2 +passedProps .height ** 2) / 2; constinnerRadius =finishedRadius *presentationProgress ; constouterRadius =finishedRadius * 2 *presentationProgress ; const {path } =makeStar ({innerRadius ,outerRadius ,points : 5, }); constboundingBox =getBoundingBox (path ); consttranslatedPath =translatePath (path ,passedProps .width / 2 -boundingBox .width / 2,passedProps .height / 2 -boundingBox .height / 2, ); const [clipId ] =useState (() =>String (random (null))); conststyle :React .CSSProperties =useMemo (() => { return {width : '100%',height : '100%',clipPath :presentationDirection === 'exiting' ?undefined : `url(#${clipId })`, }; }, [clipId ,presentationDirection ]); return ( <AbsoluteFill > <AbsoluteFill style ={style }>{children }</AbsoluteFill > {presentationDirection === 'exiting' ? null : ( <AbsoluteFill > <svg > <defs > <clipPath id ={clipId }> <path d ={translatedPath }fill ="black" /> </clipPath > </defs > </svg > </AbsoluteFill > )} </AbsoluteFill > ); };
示例用法:
🌐 Example usage:
MyComp.tsxexport constMyComp :React .FC = () => { const {width ,height } =useVideoConfig (); return ( <TransitionSeries > <TransitionSeries .Sequence durationInFrames ={70}> <Letter color ="orange">A</Letter > </TransitionSeries .Sequence > <TransitionSeries .Transition presentation ={customPresentation ({width ,height })}timing ={springTiming ({durationInFrames : 45,config : {damping : 200, },durationRestThreshold : 0.0001, })} /> <TransitionSeries .Sequence durationInFrames ={60}> <Letter color ="pink">B</Letter > </TransitionSeries .Sequence > </TransitionSeries > ); };
参考文献
🌐 References
查看已实现演示的源代码以作为有用的参考。
🌐 See the source code for already implemented presentations for a useful reference.
另请参阅
🌐 See also