forked from remotion-dev/remotion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerSeekBar.tsx
171 lines (153 loc) · 3.76 KB
/
PlayerSeekBar.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {Internals, interpolate} from 'remotion';
import {useHoverState} from './use-hover-state';
import {usePlayer} from './use-player';
import {useElementSize} from './utils/use-element-size';
const getFrameFromX = (
clientX: number,
durationInFrames: number,
width: number
) => {
const pos = clientX;
const frame = Math.round(
interpolate(pos, [0, width], [0, durationInFrames - 1 ?? 0], {
extrapolateLeft: 'clamp',
extrapolateRight: 'clamp',
})
);
return frame;
};
const BAR_HEIGHT = 5;
const KNOB_SIZE = 12;
const VERTICAL_PADDING = 4;
const containerStyle: React.CSSProperties = {
userSelect: 'none',
paddingTop: VERTICAL_PADDING,
paddingBottom: VERTICAL_PADDING,
boxSizing: 'border-box',
cursor: 'pointer',
position: 'relative',
touchAction: 'none',
};
const barBackground: React.CSSProperties = {
height: BAR_HEIGHT,
backgroundColor: 'rgba(255, 255, 255, 0.5)',
width: '100%',
borderRadius: BAR_HEIGHT / 2,
};
export const PlayerSeekBar: React.FC<{
durationInFrames: number;
}> = ({durationInFrames}) => {
const containerRef = useRef<HTMLDivElement>(null);
const barHovered = useHoverState(containerRef);
const size = useElementSize(containerRef);
const {seek, play, pause, playing} = usePlayer();
const frame = Internals.Timeline.useTimelinePosition();
const [dragging, setDragging] = useState<
| {
dragging: false;
}
| {
dragging: true;
wasPlaying: boolean;
}
>({
dragging: false,
});
const onPointerDown = useCallback(
(e: React.PointerEvent<HTMLDivElement>) => {
if (!size) {
throw new Error('Player has no size');
}
const _frame = getFrameFromX(
e.clientX - size.left,
durationInFrames,
size.width
);
pause();
seek(_frame);
setDragging({
dragging: true,
wasPlaying: playing,
});
},
[size, durationInFrames, pause, seek, playing]
);
const onPointerMove = useCallback(
(e: PointerEvent) => {
if (!size) {
throw new Error('Player has no size');
}
if (!dragging.dragging) {
return;
}
const _frame = getFrameFromX(
e.clientX - (size?.left ?? 0),
durationInFrames,
size.width
);
seek(_frame);
},
[dragging.dragging, durationInFrames, seek, size]
);
const onPointerUp = useCallback(() => {
setDragging({
dragging: false,
});
if (!dragging.dragging) {
return;
}
if (dragging.wasPlaying) {
play();
} else {
pause();
}
}, [dragging, pause, play]);
useEffect(() => {
if (!dragging.dragging) {
return;
}
window.addEventListener('pointermove', onPointerMove);
window.addEventListener('pointerup', onPointerUp);
return () => {
window.removeEventListener('pointermove', onPointerMove);
window.removeEventListener('pointerup', onPointerUp);
};
}, [dragging.dragging, onPointerMove, onPointerUp]);
const knobStyle: React.CSSProperties = useMemo(() => {
return {
height: KNOB_SIZE,
width: KNOB_SIZE,
borderRadius: KNOB_SIZE / 2,
position: 'absolute',
top: VERTICAL_PADDING - KNOB_SIZE / 2 + 5 / 2,
backgroundColor: 'white',
left: Math.max(
0,
(frame / (durationInFrames - 1)) * (size?.width ?? 0) - KNOB_SIZE / 2
),
boxShadow: '0 0 2px black',
opacity: Number(barHovered),
};
}, [barHovered, durationInFrames, frame, size]);
const fillStyle: React.CSSProperties = useMemo(() => {
return {
height: BAR_HEIGHT,
backgroundColor: 'rgba(255, 255, 255, 1)',
width: (frame / (durationInFrames - 1)) * 100 + '%',
borderRadius: BAR_HEIGHT / 2,
};
}, [durationInFrames, frame]);
return (
<div
ref={containerRef}
onPointerDown={onPointerDown}
style={containerStyle}
>
<div style={barBackground}>
<div style={fillStyle} />
</div>
<div style={knobStyle} />
</div>
);
};