-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
57 lines (47 loc) · 1.25 KB
/
lib.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
50
51
52
53
54
55
56
57
/**
*
* @param {HTMLCanvasElement} c
*/
const resize_canvas = (c) => {
// Get the window width and height
var w = window.innerWidth;
var h = window.innerHeight;
// Set the canvas width and height
c.width = w;
c.height = h;
};
/**@type HTMLCanvasElement */
const canvas = document.getElementById("canvas");
/** @type CanvasRenderingContext2D */
const canvasContext2D = canvas.getContext("2d");
// event binding
//
//
window.addEventListener("resize", () => {
resize_canvas(canvas);
});
resize_canvas(canvas);
// loop related
//
//
// invoke from another function, passing callbacks
const loop = async (calls) => {
while (true) {
let t2 = performance.now();
let p = new Promise((resolve) =>
requestAnimationFrame((t1) => {
// time delta in seconds
let dt = (t1 - t2) / 1000;
// do not interpolate latency greater than 1 second
if (dt > 1) {
resolve();
return;
}
// callbacks receive delta time and canvas dimensions
calls.forEach(fn => fn(dt, canvas.width, canvas.height));
resolve();
})
);
await p;
}
};