forked from dmilford/rust-3d-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (33 loc) · 1.34 KB
/
index.js
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
const rust = import('./pkg/rust_3d_demo');
const canvas = document.getElementById('rustCanvas');
const gl = canvas.getContext("webgl", { antialias: true });
rust.then(m => {
if (!gl) {
alert('Failed to initialize WebGL');
return;
}
const FPS_THROTTLE = 1000.0 / 30.0; // milliseconds / frames
const dougsClient = new m.DougsClient();
const initialTime = Date.now();
let lastDrawTime = -1;// In milliseconds
function render() {
window.requestAnimationFrame(render);
const currTime = Date.now();
if (currTime >= lastDrawTime + FPS_THROTTLE) {
lastDrawTime = currTime;
if (window.innerHeight !== canvas.height || window.innerWidth !== canvas.width) {
canvas.height = window.innerHeight;
canvas.clientHeight = window.innerHeight;
canvas.style.height = window.innerHeight;
canvas.width = window.innerWidth;
canvas.clientWidth = window.innerWidth;
canvas.style.width = window.innerWidth;
gl.viewport(0, 0, window.innerWidth, window.innerHeight);
}
let elapsedTime = currTime - initialTime;
dougsClient.update(elapsedTime, window.innerHeight, window.innerWidth);
dougsClient.render();
}
}
render();
});