-
Notifications
You must be signed in to change notification settings - Fork 24
/
ui.js
173 lines (144 loc) · 4.71 KB
/
ui.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
{
// DOM Helpers
const DOM = {
getById: id => document.getElementById(id),
on: (element, event, fn) => element.addEventListener(event, fn),
};
const toFixed = (value, to = 3, force) => (
(tp.config.roundNumbers || force) ? (+value).toFixed(to).padEnd(to, 0) : value
);
// DOM Collections.
const checkboxElements = [
'roundNumbers',
'drawFullNames',
'drawLineRadius', 'drawLineXAxis', 'drawLineYAxis',
'drawLineSin', 'drawLineCos', 'drawLineTan',
'drawLineCot', 'drawLineSec', 'drawLineCsc',
'drawNameFPS', 'drawNameInfo', 'drawNameCredits',
'drawNameRadius', 'drawNameTheta',
'drawNameSin', 'drawNameCos', 'drawNameTan',
'drawNameCot', 'drawNameSec', 'drawNameCsc',
];
const elements = [
'togglePlay',
'stateDeg', 'stateRad', 'stateQua',
'stateSin', 'stateCos', 'stateTan',
'stateCot', 'stateSec', 'stateCsc',
'degreeTitle', 'degreeRange', 'degreeInput',
'radiusTitle', 'radiusRange', 'radiusInput',
'stepTitle', 'stepRange', 'stepInput',
'drawNoLines', 'drawNoNames',
...checkboxElements,
];
const $el = {};
// Collect all elements.
elements.forEach(id => $el[id] = DOM.getById(id))
//
const createControlCluster = (
key,
collection,
label,
{
fixTo,
updateOnResize,
updateOnEveryFrame,
} = {}
) => {
const rangeInput = $el[`${key}Range`];
const textInput = $el[`${key}Input`];
const labelEl = $el[`${key}Title`];
const eventListener = (event, updateSelfOnly) => {
const value = toFixed(event.target.value, fixTo);
if (!updateSelfOnly) {
window.tp[collection][key] = +value;
}
if (rangeInput && event.target !== rangeInput) {
rangeInput.value = value;
}
if (textInput && event.target !== textInput) {
textInput.value = value;
}
if (labelEl) {
labelEl.innerText = typeof label === 'function' ?
label(value, key) : `${key} (${value})`;
}
};
const getAndSetValue = () => {
eventListener({ target: { value: tp[collection][key] } }, true);
if (updateOnEveryFrame) {
window.requestAnimationFrame(getAndSetValue);
}
};
if (rangeInput) {
DOM.on(rangeInput, 'change', eventListener);
DOM.on(rangeInput, 'input', eventListener);
}
if (textInput) {
DOM.on(textInput, 'change', eventListener);
DOM.on(textInput, 'input', eventListener);
}
getAndSetValue();
updateOnResize && window.addEventListener('resize', () => setTimeout(getAndSetValue));
}
createControlCluster(
'degree', 'state',
value => `angle θ (${value}deg)`,
{ fixTo: 0, updateOnEveryFrame: true, }
);
createControlCluster(
'radius', 'config',
value => `radius scale (${value === '0' ? 'auto' : `x${value}`})`,
{ fixTo: 0, updateOnResize: true, }
);
createControlCluster(
'step', 'config',
value => `step by frame (${value}deg)`,
{ fixTo: 2, }
);
DOM.on($el.togglePlay, 'click', () => {
tp.togglePlay();
$el.togglePlay.innerText = tp.config.play ? 'Stop' : 'Start';
});
const updateStateValues = () => {
const sin = +toFixed(tp.state.sin);
const cos = +toFixed(tp.state.cos);
$el.stateDeg.value = toFixed(tp.state.degree);
$el.stateRad.value = toFixed(tp.state.degreeInRad);
$el.stateQua.value = tp.state.quadrant;
$el.stateSin.value = toFixed(sin);
$el.stateCos.value = toFixed(cos);
$el.stateTan.value = cos === 0 ? 'Undefined' : toFixed(sin / cos);
$el.stateCot.value = sin === 0 ? 'Undefined' : toFixed(cos / sin);
$el.stateSec.value = cos === 0 ? 'Undefined' : toFixed(1 / cos);
$el.stateCsc.value = sin === 0 ? 'Undefined' : toFixed(1 / sin);
window.requestAnimationFrame(updateStateValues);
};
updateStateValues();
checkboxElements.forEach(checkbox => {
DOM.on($el[checkbox], 'change', function() {
window.tp.config[checkbox] = this.checked;
});
});
const setBulkCheckbox = (el, collection) => {
collection.forEach(item => {
tp.config[item] = $el[item].checked = !el.checked;
});
el.parentElement.childNodes[0].nodeValue =
el.checked ? 'check all' : 'uncheck all';
};
DOM.on($el.drawNoLines, 'click', function () {
setBulkCheckbox(this, [
'drawLineRadius', 'drawLineXAxis', 'drawLineYAxis',
'drawLineSin', 'drawLineCos', 'drawLineTan',
'drawLineCot', 'drawLineSec', 'drawLineCsc',
]);
});
DOM.on($el.drawNoNames, 'click', function () {
setBulkCheckbox(this, [
'drawNameFPS', 'drawNameInfo', 'drawNameCredits',
'drawNameRadius', 'drawNameTheta',
'drawNameSin', 'drawNameCos', 'drawNameTan',
'drawNameCot', 'drawNameSec', 'drawNameCsc',
]);
});
}