git add clock basic renderer

This commit is contained in:
2022-02-08 16:05:12 +08:00
parent a7aae62233
commit 2550e2e14f
7 changed files with 251 additions and 44 deletions
+75
View File
@@ -0,0 +1,75 @@
export {Clock, LoopFunction};
type LoopFunction = (t: number)=>void;
/**
* 时钟
*/
class Clock {
/**
* 总用时
*/
private allTime: number = 0;
/**
* 速率
*/
public speed: number = 1;
/**
* 主函数
*/
private fn: LoopFunction;
/**
* 动画循环
* @param fn 循环函数
*/
public constructor(fn?: LoopFunction){
this.fn = fn ?? ((t) => {});
}
/**
* 设置函数
* @param fn 循环函数
*/
public setFn(fn:LoopFunction){
this.fn = fn;
}
/**
* 开始
*/
public run(){
// 主循环
let loop = (t:number)=>{
// 时差
let dur = (t - this.allTime) * this.speed / 1000;
// 检测由于失焦导致的丢帧
if (t - this.allTime < 100) {
this.fn(dur);
}
// 更新时间
this.allTime = t;
// 继续循环
requestAnimationFrame(loop);
}
// 获取时间
requestAnimationFrame((t)=>{
// 记录初始时间
this.allTime = t;
// 开启循环
requestAnimationFrame(loop);
})
}
}
+9 -2
View File
@@ -57,6 +57,9 @@ interface IRendererConstructor<
/**
* 渲染器 API
* @template P 渲染器绘制参数
* @template M 渲染器参数
* @template E 渲染器事件
*/
abstract class AbstractRenderer<
P extends IRendererParam = {},
@@ -67,7 +70,7 @@ abstract class AbstractRenderer<
/**
* 渲染器参数
*/
abstract param: M;
abstract param: Partial<M>;
/**
* 类型断言
@@ -119,4 +122,8 @@ abstract class AbstractRenderer<
}
export default AbstractRenderer;
export { AbstractRenderer, ObjectID, ICommonParam, ObjectData, IRendererConstructor };
export {
AbstractRenderer, ObjectID, IAnyObject,
ICommonParam, IRendererParam,
ObjectData, IRendererConstructor
};