-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
333 lines (301 loc) · 9.35 KB
/
index.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Curved PWM</title>
<!--<meta name="viewport" content="width=device-width, initial-scale=1.0">-->
<style>
body {
width: 100vw;
height: 100vh;
margin: 0;
user-select: none;
}
#root {
width: 50vw;
height: 100vh;
position: absolute;
right: 0;
top: 0;
display: flex;
align-items: center;
justify-content: center;
}
.options-form {
position: fixed;
top: 0;
right: 0;
padding: 10px;
min-width: 200px;
.form-item {
display: flex;
flex-direction: column;
justify-content: center;
align-items: stretch;
padding-bottom: 20px;
}
}
</style>
</head>
<body>
<div id="root">
<img id="Rabbit" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2_eeP0fZQNEgZntX7RUkYSruwb9JiW5vuf-e9LMV2U-6MLqwn-MtOYlpsimJwTOvOHDEE61yIIgLdzzSPab_zeF8v-AOLwjNzu1Z0gdhsbFg9vz5u0aWe8iJTXGWS5JAXu-CLTgdTtvU/s430/animal_stand_usagi.png" alt="Rabbit" />
</div>
<div class="options-form">
<div class="form-item">
<label for="Preset">Preset:</label>
<select id="Preset">
<option value="">---</option>
</select>
</div>
<div class="form-item">
<label for="ServerPrefix">Server URL Prefix:</label>
<input id="ServerPrefix" name="serverPrefix">
</div>
<hr>
<form id="options">
<div class="form-item">
<label for="LocalName">Local Name:</label>
<input id="LocalName" name="_name">
</div>
<div class="form-item">
<label for="MinPwm">Min PWM:</label>
<input type="number" id="MinPwm" name="minPWM" value="0" step="1" min="0">
</div>
<div class="form-item">
<label for="MaxPWM">Max PWM:</label>
<input type="number" id="MaxPWM" name="maxPWM" value="255" step="1" min="0">
</div>
<div class="form-item">
<label for="Duration">Duration(ms):</label>
<input type="number" id="Duration" name="duration" value="3000" min="0">
</div>
<div class="form-item">
<label for="Interval">Interval(ms):</label>
<input type="number" id="Interval" name="interval" value="30" min="0">
</div>
<div class="form-item">
<label for="Precision">Precision:</label>
<input type="number" id="Precision" name="precision" value="0" step="1" min="0" max="8">
</div>
<div class="form-item">
<label for="MaxSteps">Max Steps:</label>
<input type="number" id="MaxSteps" name="maxSteps" value="2000" step="1" min="0">
</div>
<div class="form-item">
<label for="Reverse">Reverse:</label>
<input type="checkbox" id="Reverse" name="reverse">
</div>
<div>
<button type="submit">Upload</button>
</div>
</form>
</div>
<script id="allape_dev_id_core" src="node_modules/@mojs/core/dist/mo.umd.js"></script>
<script id="allape_dev_id_player" src="node_modules/@mojs/player/build/mojs-player.js"></script>
<script id="allape_dev_id_curve_editor" src="node_modules/@mojs/curve-editor/app/build/mojs-curve-editor.js"></script>
<script>
/**
* @typedef Options
*
* @property {string} _name
*
* @property {number} minPWM
* @property {number} maxPWM
* @property {number} duration
* @property {number} interval
* @property {number} precision
* @property {number} maxSteps
* @property {'on' | ''} reverse
*/
/**
* @param {HTMLFormElement} ele
* @return {Options}
*/
function getOptions(ele) {
const formData = new FormData(ele);
const data = {};
for (const [key, value] of formData.entries()) {
data[key] = parseInt(`${value}`);
if (isNaN(data[key])) {
data[key] = value;
}
}
return data;
}
/**
* @typedef Params
* @property {string=} path
* @property {string=} duration
*/
/**
* @return {Params}
*/
function getParams() {
const params = new URLSearchParams(window.location.search);
return Array.from(params.entries()).reduce((acc, [key, value]) => {
acc[key] = value;
return acc;
}, {});
}
const Params = getParams();
const KEY_PREFIX = 'CurvedPWM_';
</script>
<script>
let curve;
let shape;
let player;
function main() {
const root = window.document.getElementById('root');
curve = new MojsCurveEditor({
name: 'pwm',
startPath: Params.path || 'M0, 100 C0, 100 50, 50 50, 50 C50, 50 100, 0 100, 0',
isSaveState: false,
isHiddenOnMin: false,
});
const halfWidth = window.document.body.clientWidth / 2;
/** @type {HTMLDivElement} */
const curveRoot = curve._rootEl;
if (halfWidth > curveRoot.clientWidth) {
const curveRedux = curve._rootEl._component.store;
const newWidth = halfWidth - curveRoot.clientWidth - 1;
curveRedux.dispatch({ type: 'EDITOR_RESIZE', data: { type: 'right', x: newWidth } });
curveRedux.dispatch({ type: 'EDITOR_RESIZE_END', data: { type: 'right', x: newWidth + 1 } });
const newHeight = window.document.body.clientHeight - curveRoot.clientHeight - 80 - 1;
curveRedux.dispatch({
type: 'EDITOR_RESIZE',
data: { type: 'bottom', y: newHeight },
});
curveRedux.dispatch({
type: 'EDITOR_RESIZE_END',
data: { type: 'bottom', y: newHeight + 1 },
});
curveRedux.dispatch({
type: 'EDITOR_PAN_END',
data: newHeight / 2,
});
}
const EaseFunc = curve.getEasing();
const maxSpeed = 0.1;
let currentSpeed = 0;
// shape = new mojs.Shape({
// parent: root,
// shape: 'polygon',
// points: 3,
// radius: 80,
// rotate: { '0': 360 },
// duration: (Params.duration * 1) || 10000,
// isShowStart: true,
// easing: progress => {
// currentSpeed += EaseFunc(progress) * maxSpeed;
// return currentSpeed;
// },
// });
const html = new mojs.Html({
el: '#Rabbit',
rotateZ: { '0': 360 },
duration: (Params.duration * 1) || 10000,
isShowStart: true,
easing: progress => {
currentSpeed += EaseFunc(progress) * maxSpeed;
return currentSpeed;
},
});
player = new MojsPlayer({
add: html,
isPlaying: true,
isRepeat: true,
isSaveState: false,
precision: 100,
});
/** @type {HTMLSelectElement} */
const PresetSelector = document.getElementById('Preset');
/** @type {HTMLInputElement} */
const ServerPrefix = document.getElementById('ServerPrefix');
/** @type {HTMLFormElement} */
const Form = window.document.getElementById('options');
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (key.startsWith(KEY_PREFIX)) {
const data = JSON.parse(localStorage.getItem(key));
PresetSelector.appendChild(new Option(data._name, data._name));
}
}
PresetSelector.addEventListener('change', e => {
try {
const data = JSON.parse(localStorage.getItem(`${KEY_PREFIX}${e.target.value}`));
console.log('loading preset:', data);
if (!data) {
return;
}
Object.entries(data).forEach(([key, value]) => {
const ele = Form.querySelector(`[name="${key}"]`);
if (ele) {
ele.value = value;
}
});
curve._props.startPath = data._curve;
curve._drawStartPath();
} catch (e) {
console.error(e);
alert('Failed to load preset');
}
});
Form.addEventListener('submit', async (e) => {
e.preventDefault();
const data = getOptions(e.target);
console.log('form data:', data);
if (data.maxPWM <= data.minPWM) {
alert('Max PWM should be greater than Min PWM');
return;
}
const PWM = data.maxPWM - data.minPWM;
const decimals = Math.pow(10, data.precision);
const isReversed = data.reverse === 'on';
let elapsed = 0;
/** @type {number[]} */
const steps = [];
while (elapsed < data.duration && steps.length < data.maxSteps) {
let pwm = data.minPWM + Math.floor(EaseFunc(elapsed / data.duration) * PWM * decimals) / decimals;
pwm = pwm < 0 ? Math.max(pwm, -data.maxPWM) : Math.min(pwm, data.maxPWM);
if (isReversed) {
pwm = (pwm < 0 ? -1 : 1) * data.maxPWM - pwm;
}
steps.push(pwm);
elapsed += data.interval;
}
if (data._name) {
localStorage.setItem(`${KEY_PREFIX}${data._name}`, JSON.stringify({
...data,
_curve: curve._prevPath,
}));
PresetSelector.appendChild(new Option(data._name, data._name));
}
/**
* @type {{steps: number[], interval: number}}
*/
const json = {
steps,
interval: data.interval,
};
console.log('uploading data:', json);
try {
const res = await (await fetch(`${ServerPrefix.value}/pwm`, {
method: 'POST',
body: JSON.stringify(json),
})).text();
if (res !== 'ok') {
// noinspection ExceptionCaughtLocallyJS
throw new Error(res);
}
console.log(res);
} catch (e) {
console.error(e);
alert('Failed to upload data');
}
});
}
window.document.addEventListener('DOMContentLoaded', main);
</script>
</body>
</html>