perf: preload cảnh kề lúc trình duyệt rảnh

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
tuyenio
2026-06-24 16:12:13 +07:00
parent cf360694d6
commit 5cc3c7b636
+25 -5
View File
@@ -294,6 +294,23 @@ function directionFromYawPitch(yaw: number, pitch: number) {
); );
} }
type IdleWindow = typeof window & {
requestIdleCallback?: (cb: () => void, opts?: { timeout: number }) => number;
};
// Lên lịch chạy lúc trình duyệt rảnh (fallback setTimeout) — dùng cho preload nền.
function scheduleIdle(cb: () => void) {
if (typeof window === "undefined") {
return;
}
const w = window as IdleWindow;
if (w.requestIdleCallback) {
w.requestIdleCallback(cb, { timeout: 2000 });
} else {
window.setTimeout(cb, 400);
}
}
export default function VirtualTour({ export default function VirtualTour({
tourId = "chua-thai-lac", tourId = "chua-thai-lac",
}: { }: {
@@ -1282,13 +1299,16 @@ function TourExperience({
.catch(() => undefined); .catch(() => undefined);
}; };
// Cảnh hiện tại: tải ngay. Cảnh kề: lùi xuống lúc rảnh để không nghẽn khi vừa vào.
preloadImage(scene.image); preloadImage(scene.image);
scene.hotspots.forEach((hotspot) => { scheduleIdle(() => {
const targetScene = tourConfig.sceneById.get(hotspot.targetId); scene.hotspots.forEach((hotspot) => {
const targetScene = tourConfig.sceneById.get(hotspot.targetId);
if (targetScene) { if (targetScene) {
preloadImage(targetScene.image); preloadImage(targetScene.image);
} }
});
}); });
}, },
[tourConfig, warmPanoramaTexture], [tourConfig, warmPanoramaTexture],