-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.html
77 lines (64 loc) · 1.67 KB
/
demo.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="lib/fiberize.browser.js"></script>
<style>
p {
margin: 0;
font-size: 8pt;
font-family: monospace;
}
</style>
</head>
<body style="height: 100vh; padding: 0; margin: 0;">
<div id="console" style="position: absolute;">
</div>
<div id="box" style="background: red; position: relative; width: 100px; height: 100px;"></div>
<script>
const boxEl = document.getElementById('box');
const consoleEl = document.getElementById('console');
let vx = 10;
let vy = 10;
let x = 0;
let y = 0;
function animation() {
const rect = document.body.getClientRects()[0];
x += vx;
y += vy;
if (x <= 0 || (x + 100) > rect.width) {
vx *= -1;
}
if (y <= 0 || (y + 100) > rect.height) {
vy *= -1;
}
boxEl.style.left = x + 'px';
boxEl.style.top = y + 'px';
requestAnimationFrame(animation);
}
animation();
function* fib(n) {
if (n <= 2) {
return 1;
}
return (yield fib(n - 1)) + (yield fib(n - 2));
}
function* main() {
for (let i = 1; i < 40; i++) {
const res = yield fib(i);
// print result to screen.
(function () {
const p = document.createElement('p');
p.innerText = `fib(${i}) = ${res}`;
consoleEl.appendChild(p);
})();
}
}
fiberize.run(main());
fiberize.executeWhileIdle();
</script>
</body>
</html>