-
Notifications
You must be signed in to change notification settings - Fork 45
/
App.js
160 lines (144 loc) · 4.95 KB
/
App.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import React, { useState, useTransition } from "react";
import logo from "./logo.svg";
import LagRadar from "react-lag-radar";
import "./App.css";
import Pythagoras from "./Pythagoras";
function App() {
const svg = {
width: 1280,
height: 600,
};
const baseWidth = 80;
const heightFactor = 0.4;
const maxTreeSize = 20;
// we split state in two so we can update
// visuals and inputs separately
const [treeSizeInput, setTreeSizeInput] = useState(8);
const [treeSize, setTreeSize] = useState(8);
const [treeLeanInput, setTreeLeanInput] = useState(0);
const [treeLean, setTreeLean] = useState(0);
const [isLeaning, startLeaning] = useTransition()
const [enableStartTransition, setEnableStartTransition] = useState(false);
const [enableSlowdown, setEnableSlowdown] = useState(false);
function changeTreeSize(event) {
const value = Number(event.target.value);
setTreeSizeInput(value); // update input
// update visuals
if (enableStartTransition) {
React.startTransition(() => {
setTreeSize(value);
});
} else {
setTreeSize(value);
}
}
function changeTreeLean(event) {
const value = Number(event.target.value);
setTreeLeanInput(value); // update input
// update visuals
if (enableStartTransition) {
startLeaning(() => {
setTreeLean(value);
});
} else {
setTreeLean(value);
}
}
function toggleStartTransition(event) {
setEnableStartTransition(event.target.checked);
}
function toggleSlowdown(event) {
setEnableSlowdown(event.target.checked);
}
return (
<div className="App">
<div className="App-header" style={{ marginBottom: "1rem" }}>
<LagRadar />
<h2>This is a leaning Pythagoras tree</h2>
</div>
<div
style={{
display: "flex",
flexDirection: "row",
justifyContent: "center",
}}
>
<div>
<label>Use startTransition</label>
<br />
<input
type="checkbox"
checked={enableStartTransition}
onChange={toggleStartTransition}
/>
</div>
<div>
<label>Lean the tree:</label>
<br />
<input
type="range"
value={treeLeanInput}
onChange={changeTreeLean}
min="-0.5"
max="0.5"
step="0.05"
style={{ width: svg.width / 3 }}
/>
</div>
<div>
<label>Make each square block the thread for 0.1ms</label>
<br />
<input
type="checkbox"
checked={enableSlowdown}
onChange={toggleSlowdown}
/>
</div>
</div>
<div style={{ display: "flex", flexDirection: "row" }}>
<div style={{ display: "flex", flexDirection: "column" }}>
<label>
Grow the tree
<br />
Bigger is slower
</label>
<input
type="range"
value={treeSizeInput}
onChange={changeTreeSize}
min="0"
max={maxTreeSize}
step="1"
style={{
transform: `rotate(-90deg) translate(-${
svg.height / 2
}px, 0)`,
width: svg.height / 2,
}}
/>
</div>
<svg
width={svg.width}
height={svg.height}
className={isLeaning ? 'pending' : 'done'}
style={{
border: "1px solid lightgray",
}}
>
<Pythagoras
enableSlowdown={enableSlowdown}
w={baseWidth}
h={baseWidth}
heightFactor={heightFactor}
lean={-treeLean}
x={svg.width / 2 - 40}
y={svg.height - baseWidth}
lvl={0}
maxlvl={treeSize}
/>
</svg>
</div>
</div>
);
}
export default App;