Skip to content

Commit 0f2fab8

Browse files
committed
refactor: use requestVideoFrameCallback if available
close #51
1 parent 363c170 commit 0f2fab8

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

src/internal.js

+21-13
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ export function clear(store) {
1111
store.space = [];
1212
}
1313

14-
function framing(store) {
15-
const { video, dialogues, actives } = store;
16-
const vct = video.currentTime - store.delay;
14+
function framing(store, mediaTime) {
15+
const { dialogues, actives } = store;
16+
const vct = mediaTime - store.delay;
1717
for (let i = actives.length - 1; i >= 0; i -= 1) {
1818
const dia = actives[i];
1919
const { end } = dia;
@@ -31,9 +31,6 @@ function framing(store) {
3131
(dia.animations || []).forEach((animation) => {
3232
animation.currentTime = (vct - dia.start) * 1000;
3333
});
34-
if (!video.paused) {
35-
batchAnimate(dia, 'play');
36-
}
3734
actives.push(dia);
3835
}
3936
store.index += 1;
@@ -53,27 +50,38 @@ export function createSeek(store) {
5350
}
5451
return (dialogues.length || 1) - 1;
5552
})();
56-
framing(store);
53+
framing(store, video.currentTime);
5754
};
5855
}
5956

57+
function createFrame(video) {
58+
const useVFC = video.requestVideoFrameCallback;
59+
return [
60+
useVFC ? video.requestVideoFrameCallback.bind(video) : requestAnimationFrame,
61+
useVFC ? video.cancelVideoFrameCallback.bind(video) : cancelAnimationFrame,
62+
];
63+
}
64+
6065
export function createPlay(store) {
66+
const { video } = store;
67+
const [requestFrame, cancelFrame] = createFrame(video);
6168
return function play() {
62-
const frame = () => {
63-
framing(store);
64-
store.requestId = requestAnimationFrame(frame);
69+
const frame = (now, metadata) => {
70+
framing(store, metadata?.mediaTime || video.currentTime);
71+
store.requestId = requestFrame(frame);
6572
};
66-
cancelAnimationFrame(store.requestId);
67-
store.requestId = requestAnimationFrame(frame);
73+
cancelFrame(store.requestId);
74+
store.requestId = requestFrame(frame);
6875
store.actives.forEach((dia) => {
6976
batchAnimate(dia, 'play');
7077
});
7178
};
7279
}
7380

7481
export function createPause(store) {
82+
const [, cancelFrame] = createFrame(store.video);
7583
return function pause() {
76-
cancelAnimationFrame(store.requestId);
84+
cancelFrame(store.requestId);
7785
store.requestId = 0;
7886
store.actives.forEach((dia) => {
7987
batchAnimate(dia, 'pause');

0 commit comments

Comments
 (0)