Skip to content

Commit

Permalink
优化虚拟时间环境重写
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinlic committed Nov 23, 2023
1 parent 6448728 commit 403e493
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion core/CaptureContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export default class CaptureContext {
frameInterval = 0;
/** @type {boolean} - 准备完毕标志 */
readyFlag = false;
/** @type {boolean} - 启动标志 */
startFlag = false;
/** @type {boolean} - 停止标志 */
stopFlag = false;
/** @type {boolean} - 暂停标志 */
Expand Down Expand Up @@ -51,6 +53,7 @@ export default class CaptureContext {
/** @type {number} - 目标总帧数 */
frameCount: null
};
skipFramePromise;
/** @type {SvgAnimation[]|VideoCanvas[]|DynamicImage[]|LottieCanvas[]} - 媒体调度列表 */
dispatchMedias = [];

Expand Down Expand Up @@ -119,6 +122,8 @@ export default class CaptureContext {
this.startTime = Date.now();
// 计算帧间隔时间
this.frameInterval = 1000 / this.config.fps;
// 设置启动标志
this.startFlag = true;
// 递归捕获帧
(function nextFrame() {
(async () => {
Expand Down Expand Up @@ -350,6 +355,8 @@ export default class CaptureContext {
window.____setInterval = window.setInterval;
// 重写setInterval函数
window.setInterval = (fn, interval) => {
if(!this.startFlag)
return window.____setInterval(fn, interval);
if (typeof fn !== "function")
throw new TypeError("setInterval function must be Function");
if (isNaN(interval))
Expand All @@ -363,16 +370,21 @@ export default class CaptureContext {
// 重写cleanInterval函数
window.clearInterval = timerId => {
if (!timerId) return;
if(!this.startFlag)
return window.____clearInterval(timerId);
this.intervalCallbacks = this.intervalCallbacks.filter(([_timerId]) => {
if (_timerId == timerId)
return false;
return true;
});
window.____clearInterval(timerId);
}
// 暂存setTimeout函数
window.____setTimeout = window.setTimeout;
// 重写setTimeout函数
window.setTimeout = (fn, timeout = 0) => {
if(!this.startFlag)
return window.____setTimeout(fn, timeout);
if (typeof fn !== "function")
return;
this.timerId++;
Expand All @@ -384,11 +396,14 @@ export default class CaptureContext {
// 重写clearTimeout函数
window.clearTimeout = timerId => {
if (!timerId) return;
if(!this.startFlag)
return window.____clearTimeout(timerId);
this.timeoutCallbacks = this.timeoutCallbacks.filter(([_timerId]) => {
if (_timerId == timerId)
return false;
return true;
});
window.____clearTimeout(timerId);
}
// 暂存requestAnimationFrame函数
window.____requestAnimationFrame = window.requestAnimationFrame;
Expand All @@ -397,7 +412,7 @@ export default class CaptureContext {
if (this.stopFlag)
return;
// 下一个事件循环再调用
window.____requestAnimationFrame(() => fn(this.currentTime));
return window.____requestAnimationFrame(rawCurrentTime => fn(this.startFlag ? this.currentTime : rawCurrentTime));
};
// 暂存Date对象
window.____Date = Date;
Expand Down

0 comments on commit 403e493

Please sign in to comment.