-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (36 loc) · 1.57 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
43
44
45
46
47
48
49
const rust_layer = import('./pkg/rust_layer');
const canvas_2d = document.getElementById('canvas-2d')
const gl = canvas_2d.getContext('webgl', { antialias: true })
rust_layer
.then(m => {
m.greet("Hola")
if (!gl) {
alert("Failed to initialize WebGl");
return;
}
const FPS_THROTTLE = 1000.0 / 30.0 // milliseconds / frames
const langasGame = new m.LangasGame();
const initialTime = Date.now();
let lastDrawTime = -1 // in milliseconds
function render() {
window.requestAnimationFrame(render);
const currentTime = Date.now();
if (currentTime >= lastDrawTime + FPS_THROTTLE) {
lastDrawTime = currentTime;
if (window.innerHeight !== canvas_2d.height || window.innerWidth !== canvas_2d.width) {
canvas_2d.height = window.innerHeight;
canvas_2d.clientHeight = window.innerHeight;
canvas_2d.style.height = window.innerHeight;
canvas_2d.width = window.innerWidth;
canvas_2d.clientWidth = window.innerWidth;
canvas_2d.style.width = window.innerWidth;
gl.viewport(0, 0, window.innerWidth, window.innerHeight);
}
const elapsedTime = currentTime - initialTime;
langasGame.update(elapsedTime, window.innerHeight, window.innerWidth)
langasGame.render();
}
}
render();
})
.catch(e => console.error(e));