-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathWebPlatform.ts
More file actions
146 lines (122 loc) · 3.88 KB
/
WebPlatform.ts
File metadata and controls
146 lines (122 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { Platform } from '../Platform.js';
import type { Stage } from '../../Stage.js';
/**
* make fontface add not show errors
*/
interface FontFaceSetWithAdd extends FontFaceSet {
add(font: FontFace): void;
}
export class WebPlatform extends Platform {
////////////////////////
// Platform-specific methods
////////////////////////
override createCanvas(): HTMLCanvasElement {
const canvas = document.createElement('canvas');
return canvas;
}
override getElementById(id: string): HTMLElement | null {
return document.getElementById(id);
}
////////////////////////
// Update loop
////////////////////////
override startLoop(stage: Stage): void {
let isIdle = false;
let lastFrameTime = 0;
const buffer = 4;
const runLoop = (currentTime: number = 0) => {
const targetFrameTime = stage.targetFrameTime;
if (targetFrameTime > 0) {
// Calculate elapsed time since the last frame
const elapsed = currentTime - lastFrameTime;
// If not enough time has passed, skip this frame
if (elapsed < targetFrameTime) {
const wait = targetFrameTime - elapsed;
if (wait > buffer) {
setTimeout(() => requestAnimationFrame(runLoop), wait - buffer);
} else {
requestAnimationFrame(runLoop);
}
return;
}
// Adjust lastFrameTime to maintain the target FPS
lastFrameTime = currentTime - (elapsed % targetFrameTime);
} else {
lastFrameTime = currentTime;
}
stage.updateFrameTime();
stage.updateAnimations();
if (!stage.hasSceneUpdates()) {
// We still need to calculate the fps else it looks like the app is frozen
stage.calculateFps();
// We use 15ms instead of 16.6ms to provide a safety buffer.
// This ensures we wake up slightly before the next frame to check for updates,
// preventing us from missing a frame due to timer variances.
setTimeout(
() => requestAnimationFrame(runLoop),
Math.max(targetFrameTime, 15),
);
if (isIdle === false) {
stage.shManager.cleanup();
stage.eventBus.emit('idle');
isIdle = true;
}
if (stage.txMemManager.checkCleanup() === true) {
stage.txMemManager.cleanup();
}
stage.flushFrameEvents();
return;
}
isIdle = false;
stage.drawFrame();
stage.flushFrameEvents();
// Schedule next frame
if (targetFrameTime > 0) {
const nextTarget = lastFrameTime + targetFrameTime;
const now = performance.now();
const wait = nextTarget - now;
// If we have a significant wait time, use setTimeout to yield to the browser.
// We subtract a small buffer (4ms) to ensure we wake up BEFORE the next frame.
if (wait > buffer) {
setTimeout(() => requestAnimationFrame(runLoop), wait - buffer);
} else {
requestAnimationFrame(runLoop);
}
} else {
// Use standard rAF when not throttling
requestAnimationFrame(runLoop);
}
};
requestAnimationFrame(runLoop);
}
////////////////////////
// ImageBitmap
////////////////////////
override createImageBitmap(
blob: ImageBitmapSource,
sxOrOptions?: number | ImageBitmapOptions,
sy?: number,
sw?: number,
sh?: number,
options?: ImageBitmapOptions,
): Promise<ImageBitmap> {
if (typeof sxOrOptions === 'number') {
return createImageBitmap(
blob,
sxOrOptions,
sy ?? 0,
sw ?? 0,
sh ?? 0,
options,
);
} else {
return createImageBitmap(blob, sxOrOptions);
}
}
getTimeStamp(): number {
return performance ? performance.now() : Date.now();
}
override addFont(font: FontFace): void {
(document.fonts as FontFaceSetWithAdd).add(font);
}
}