Skip to content

Commit

Permalink
refactor: use requestVideoFrameCallback if available
Browse files Browse the repository at this point in the history
close #51
  • Loading branch information
weizhenye committed Sep 22, 2024
1 parent 363c170 commit 0f2fab8
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export function clear(store) {
store.space = [];
}

function framing(store) {
const { video, dialogues, actives } = store;
const vct = video.currentTime - store.delay;
function framing(store, mediaTime) {
const { dialogues, actives } = store;
const vct = mediaTime - store.delay;
for (let i = actives.length - 1; i >= 0; i -= 1) {
const dia = actives[i];
const { end } = dia;
Expand All @@ -31,9 +31,6 @@ function framing(store) {
(dia.animations || []).forEach((animation) => {
animation.currentTime = (vct - dia.start) * 1000;
});
if (!video.paused) {
batchAnimate(dia, 'play');
}
actives.push(dia);
}
store.index += 1;
Expand All @@ -53,27 +50,38 @@ export function createSeek(store) {
}
return (dialogues.length || 1) - 1;
})();
framing(store);
framing(store, video.currentTime);
};
}

function createFrame(video) {
const useVFC = video.requestVideoFrameCallback;
return [
useVFC ? video.requestVideoFrameCallback.bind(video) : requestAnimationFrame,
useVFC ? video.cancelVideoFrameCallback.bind(video) : cancelAnimationFrame,
];
}

export function createPlay(store) {
const { video } = store;
const [requestFrame, cancelFrame] = createFrame(video);
return function play() {
const frame = () => {
framing(store);
store.requestId = requestAnimationFrame(frame);
const frame = (now, metadata) => {
framing(store, metadata?.mediaTime || video.currentTime);
store.requestId = requestFrame(frame);
};
cancelAnimationFrame(store.requestId);
store.requestId = requestAnimationFrame(frame);
cancelFrame(store.requestId);
store.requestId = requestFrame(frame);
store.actives.forEach((dia) => {
batchAnimate(dia, 'play');
});
};
}

export function createPause(store) {
const [, cancelFrame] = createFrame(store.video);
return function pause() {
cancelAnimationFrame(store.requestId);
cancelFrame(store.requestId);
store.requestId = 0;
store.actives.forEach((dia) => {
batchAnimate(dia, 'pause');
Expand Down

0 comments on commit 0f2fab8

Please sign in to comment.