-
-
Notifications
You must be signed in to change notification settings - Fork 962
/
Trail.tsx
86 lines (78 loc) 路 1.84 KB
/
Trail.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import React from 'react';
import {AbsoluteFill, Freeze, useCurrentFrame} from 'remotion';
export type TrailProps = {
children: React.ReactNode;
layers: number;
lagInFrames: number;
trailOpacity: number;
};
/**
* @description The <Trail> component duplicates it's children and adds a time offset to each layer in order to create a trail effect.
* @see [Documentation](https://www.remotion.dev/docs/motion-blur/trail)
*/
export const Trail: React.FC<TrailProps> = ({
children,
layers,
lagInFrames,
trailOpacity,
}: TrailProps) => {
const frame = useCurrentFrame();
if (
typeof layers !== 'number' ||
Number.isNaN(layers) ||
!Number.isFinite(layers)
) {
throw new TypeError(
`"layers" must be a number, but got ${JSON.stringify(layers)}`,
);
}
if (layers % 1 !== 0) {
throw new TypeError(
`"layers" must be an integer, but got ${JSON.stringify(layers)}`,
);
}
if (layers < 0) {
throw new TypeError(
`"layers" must be non-negative, but got ${JSON.stringify(layers)}`,
);
}
if (
typeof trailOpacity !== 'number' ||
Number.isNaN(trailOpacity) ||
!Number.isFinite(trailOpacity)
) {
throw new TypeError(
`"trailOpacity" must be a number, but got ${JSON.stringify(
trailOpacity,
)}`,
);
}
if (
typeof lagInFrames !== 'number' ||
Number.isNaN(lagInFrames) ||
!Number.isFinite(lagInFrames)
) {
throw new TypeError(
`"lagInFrames" must be a number, but got ${JSON.stringify(lagInFrames)}`,
);
}
return (
<AbsoluteFill>
{new Array(layers).fill(true).map((_, i) => {
return (
<AbsoluteFill
key={`frame-${i.toString()}`}
style={{
opacity: trailOpacity - ((layers - i) / layers) * trailOpacity,
}}
>
<Freeze frame={frame - lagInFrames * (layers - i)}>
{children}
</Freeze>
</AbsoluteFill>
);
})}
{children}
</AbsoluteFill>
);
};