This repository has been archived by the owner on Jul 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 174
/
index.js
201 lines (182 loc) · 4.74 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// @flow
import React, { PureComponent, Component } from "react";
import qrcode from "qrcode";
type Props = {
data: string,
errorCorrectionLevel: string,
size: number,
style?: *
};
const samplesSize = 60;
const fps = 20;
const introTime = 2000;
const benchTime = 5000;
const initialDataSize = 20;
const totalRound = 10;
const incrementDataSize = 40;
const size = 500;
class QRCode extends PureComponent<Props> {
static defaultProps = {
size,
errorCorrectionLevel: "Q"
};
componentDidMount() {
this.drawQRCode();
}
componentDidUpdate() {
this.drawQRCode();
}
_canvas = null;
drawQRCode() {
const { data, size, errorCorrectionLevel } = this.props;
qrcode.toCanvas(this._canvas, data, {
width: size,
margin: 0,
errorCorrectionLevel,
color: {
light: "#ffffff"
}
});
}
render() {
return <canvas style={this.props.style} ref={n => (this._canvas = n)} />;
}
}
class Benchmark extends PureComponent<*, *> {
root = React.createRef();
_raf: *;
componentDidMount() {
let i = 0;
let lastT;
const loop = t => {
this._raf = requestAnimationFrame(loop);
if (!lastT) lastT = t;
if (t - lastT < 1000 / fps) return;
lastT = t;
const { current } = this.root;
if (current) {
current.children[i].style.opacity = 0;
i = (i + 1) % samplesSize;
current.children[i].style.opacity = 1;
}
};
this._raf = requestAnimationFrame(loop);
}
componentWillUnmount() {
cancelAnimationFrame(this._raf);
}
render() {
const { dataSize } = this.props;
const randomDataSamples = Array(samplesSize)
.fill(null)
.map(() =>
Array(dataSize)
.fill(null)
.map(() => String.fromCharCode(Math.floor(256 * Math.random())))
.join("")
);
return (
<div
ref={this.root}
style={{ position: "relative", width: size, height: size }}
>
{randomDataSamples.map((data, i) => (
<QRCode
key={i}
data={data}
style={{ position: "absolute", top: 0, left: 0, opacity: 0 }}
/>
))}
</div>
);
}
}
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
class BridgeStream extends Component<*, *> {
state = {
dataSize: initialDataSize,
p: 0,
intro: true,
ready: false,
end: false
};
onGo = async () => {
this.setState({ ready: true });
for (let i = 0; i < totalRound; i++) {
await delay(introTime);
this.setState({ p: (i + 1) / totalRound, intro: false });
await delay(benchTime);
this.setState({ intro: true });
this.setState({
dataSize: initialDataSize + (i + 1) * incrementDataSize
});
}
this.setState({ end: true });
};
render() {
const { dataSize, ready, intro, end, p } = this.state;
return (
<div
style={{
display: "flex",
textAlign: "center",
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
width: "100vw",
height: "100vh",
padding: 20,
boxSizing: "border-box"
}}
>
<div style={{ height: 80, maxWidth: 800 }}>
{end ? (
<div>{"That's all folks!"}</div>
) : ready ? (
<progress value={p} min={0} max={1} />
) : (
<div>
Move this page to fullscreen. Make sure you run on Phone in
Release mode and that wires are detached. go to Settings/Benchmark
QRStream. Move the camera so the QRCode is exactly inside the
white rectangle. There must be no reflection on the computer
screen. You will have to stay for about 1 minute on the same
position. When the benchmark is done, please copy paste the
result. When you{"'"}re ready click on:{" "}
<button onClick={this.onGo}>READY</button>
</div>
)}
</div>
<div
style={{
position: "relative",
width: size,
height: size,
margin: 50,
display: "inline-block"
}}
>
<Benchmark dataSize={dataSize} />
{end ? (
<QRCode
data="end"
style={{ position: "absolute", top: 0, left: 0 }}
/>
) : intro ? (
<QRCode
data={`bench:${dataSize}`}
style={{ position: "absolute", top: 0, left: 0 }}
/>
) : null}
</div>
</div>
);
}
}
// $FlowFixMe
BridgeStream.demo = {
title: "QRStream benchmark",
url: "/qrstreambenchmark",
hidden: true
};
export default BridgeStream;