refactor: 优化渲染循环逻辑,采用时间分片策略提升性能,确保流畅动画体验

This commit is contained in:
xudan 2025-08-04 16:59:13 +08:00
parent 0728911ef7
commit 0d1308b3b9

View File

@ -76,18 +76,25 @@ const monitorScene = async () => {
/**
* 渲染循环函数
* 这个函数会通过 requestAnimationFrame 以浏览器的刷新频率通常是60fps被反复调用
* 这是实现流畅动画的核心
* 通过 requestAnimationFrame 以浏览器的刷新频率被调用
* 采用时间分片Time Slicing策略为每一帧的渲染操作设定一个时间预算frameBudget
* 避免单帧处理过多数据导致UI阻塞
*/
const renderLoop = () => {
//
if (latestRobotData.size > 0) {
//
latestRobotData.forEach((data, id) => {
const { x, y, active, angle, path, isWaring, isFault, ...rest } = data;
//
if (!editor.value?.checkRobotById(id)) return;
const frameBudget = 8; // 8ms
const startTime = performance.now();
//
while (performance.now() - startTime < frameBudget && latestRobotData.size > 0) {
// Map
const entry = latestRobotData.entries().next().value;
if (!entry) break;
const [id, data] = entry;
latestRobotData.delete(id);
const { x, y, active, angle, path, isWaring, isFault, ...rest } = data;
//
if (editor.value?.checkRobotById(id)) {
//
editor.value?.updateRobot(id, rest);
@ -99,11 +106,11 @@ const monitorScene = async () => {
//
editor.value.refreshRobot(id, { x, y, active, angle, path, isWaring, isFault });
}
});
//
latestRobotData.clear();
}
}
// renderLoop
// 使
animationFrameId = requestAnimationFrame(renderLoop);
};