-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.html
207 lines (174 loc) · 5.38 KB
/
ui.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
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
202
203
204
205
206
207
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
<style>
.template { width: 300px; height: 300px; border: 1px solid black; display: block; }
</style>
</head>
<body>
<div class="container">
<div id="root"></div>
</div>
<script type="text/babel">
let ws = new WebSocket('ws://localhost:9001/');
let last_cb = null;
ws.onmessage = function(event) {
last_cb(JSON.parse(event.data));
}
let send = (msg, cb) => {
ws.send(JSON.stringify(msg));
last_cb = cb;
};
class RecordTemplate extends React.Component {
state = {recording: false, template: []}
constructor(props) {
super(props);
this.canvas = React.createRef();
}
onClick = () => {
this.setState({recording: !this.state.recording});
send(
{'type': 'record_template', 'value': this.state.recording},
(val) => {
if (!this.state.recording) {
this.setState({template: val});
}
});
}
componentDidUpdate() {
let c = this.canvas.current;
let ctx = c.getContext('2d');
let w = c.width;
let h = c.height;
let cx = w / 2;
let cy = h / 2;
let tw = 8;
let th = 8;
ctx.clearRect(0, 0, w, h);
if (this.state.template.length == 0) {
return;
}
ctx.beginPath();
let path = this.state.template.map(([x, y]) => {
x = x / tw * w;
y = y / th * h;
x += cx;
y += cy;
return [Math.round(x), Math.round(y)];
});
ctx.moveTo(path[0][0], path[0][1]);
path.forEach(([x, y]) => {
ctx.lineTo(x, y);
});
ctx.closePath();
ctx.lineWidth = 2;
ctx.stroke();
}
render() {
return <div>
<canvas ref={this.canvas} className='template'></canvas>
<button onClick={this.onClick}>
{this.state.recording ? 'Stop recording template' : 'Record template'}
</button>
</div>
}
}
class ApplyTemplate extends React.Component {
state = {recording: false}
onClick = () => {
this.setState({recording: !this.state.recording});
send({'type': 'apply_template', 'value': this.state.recording});
}
render() {
return <button onClick={this.onClick}>
{'Apply template'}
</button>
}
}
class ApplyTemplatePath extends React.Component {
state = {recording: false}
onClick = () => {
this.setState({recording: !this.state.recording});
send({'type': 'apply_template_path', 'value': this.state.recording});
}
render() {
return <button onClick={this.onClick}>
{this.state.recording ? 'Stop recording path' : 'Apply template on path'}
</button>
}
}
class DisableMotors extends React.Component {
state = {disabled: false}
onClick = () => {
this.setState({disabled: !this.state.disabled});
send({'type': 'motors', 'value': this.state.disabled});
}
render() {
return <button onClick={this.onClick}>
{this.state.disabled ? 'Enable motors' : 'Disable motors'}
</button>
}
}
class RotationSlider extends React.Component {
state = {value: 0}
onChange = (event) => {
this.setState({value: event.target.value});
send({'rotation': event.target.value})
}
render() {
return <div>
Rotation (degrees):
<input type="range" min="0" max="360" value={this.state.value} className="slider" onChange={this.onChange}>
</input>
{this.state.value}
</div>
//<input type="text" value={this.state.value}/>
}
}
class ScaleSlider extends React.Component {
state = {value: 1}
onChange = (event) => {
this.setState({value: event.target.value});
send({'scale': event.target.value})
}
render() {
return <div>
Scale:
<input type="range" min="0" max="2" step=".1" value={this.state.value} className="slider" onChange={this.onChange}>
</input>
{this.state.value}
</div>
}
}
class App extends React.Component {
render() {
return <div>
<h1>Interactive plotter</h1>
<div>
<h2>Templates</h2>
<RecordTemplate />
<ApplyTemplate />
<ApplyTemplatePath />
</div>
<div>
<h2>Settings</h2>
<RotationSlider />
<ScaleSlider />
</div>
<div>
<h2>Motors</h2>
<DisableMotors />
</div>
</div>;
}
}
ReactDOM.render(
<App />,
document.getElementById('root'));
</script>
</body>
</html>