-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
252 lines (218 loc) · 6.92 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Visualize Arduino fscale mapping function</title>
</head>
<style>
.slidecontainer {
width: 100%; /* Width of the outside container */
margin-bottom: 20px;
}
#curveParamValue {
margin-bottom: 20px;
}
.slider {
-webkit-appearance: none;
width: 100%;
height: 15px;
border-radius: 5px;
background: #d3d3d3;
outline: none;
opacity: 0.7;
-webkit-transition: .2s;
transition: opacity .2s;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 25px;
height: 25px;
border-radius: 50%;
background: #04AA6D;
cursor: pointer;
}
.slider::-moz-range-thumb {
width: 25px;
height: 25px;
border-radius: 50%;
background: #04AA6D;
cursor: pointer;
}
</style>
<body>
<div>
<canvas id="chart"></canvas>
</div>
<div class="slidecontainer">
<input type="range" min="-10.0" max="10.0" value="-0.3" step="0.1" class="slider" id="curveParam">
</div>
<div>
Curve Param Value: <input type="value" value=0.3 id="curveParamValue" />
</div>
<div>
Code:
<br>
<code class="language-javascript">
function fscale(originalMin, originalMax, newBegin, newEnd, inputValue, curve) {
let OriginalRange = 0;
let NewRange = 0;
let zeroRefCurVal = 0;
let normalizedCurVal = 0;
let rangedValue = 0;
let invFlag = 0;
// condition curve parameter
// limit range
if (curve > 10) curve = 10;
if (curve < -10) curve = -10;
curve = (curve * -.1); // - invert and scale - this seems more intuitive - postive numbers give more weight to high end on output
curve = Math.pow(10, curve); // convert linear scale into lograthimic exponent for other pow function
// Check for out of range inputValues
if (inputValue < originalMin) {
inputValue = originalMin;
}
if (inputValue > originalMax) {
inputValue = originalMax;
}
// Zero Refference the values
OriginalRange = originalMax - originalMin;
if (newEnd > newBegin) {
NewRange = newEnd - newBegin;
} else {
NewRange = newBegin - newEnd;
invFlag = 1;
}
zeroRefCurVal = inputValue - originalMin;
normalizedCurVal = zeroRefCurVal / OriginalRange; // normalize to 0 - 1 float
// Check for originalMin > originalMax - the math for all other cases i.e. negative numbers seems to work out fine
if (originalMin > originalMax) {
return 0;
}
if (invFlag == 0) {
rangedValue = (Math.pow(normalizedCurVal, curve) * NewRange) + newBegin;
} else // invert the ranges
{
rangedValue = newBegin - (Math.pow(normalizedCurVal, curve) * NewRange);
}
return rangedValue;
}
</code>
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
function fscale(originalMin, originalMax, newBegin, newEnd, inputValue, curve){
let OriginalRange = 0;
let NewRange = 0;
let zeroRefCurVal = 0;
let normalizedCurVal = 0;
let rangedValue = 0;
let invFlag = 0;
// condition curve parameter
// limit range
if (curve > 10) curve = 10;
if (curve < -10) curve = -10;
curve = (curve * -.1) ; // - invert and scale - this seems more intuitive - postive numbers give more weight to high end on output
curve = Math.pow(10, curve); // convert linear scale into lograthimic exponent for other pow function
// Check for out of range inputValues
if (inputValue < originalMin) {
inputValue = originalMin;
}
if (inputValue > originalMax) {
inputValue = originalMax;
}
// Zero Refference the values
OriginalRange = originalMax - originalMin;
if (newEnd > newBegin){
NewRange = newEnd - newBegin;
}
else
{
NewRange = newBegin - newEnd;
invFlag = 1;
}
zeroRefCurVal = inputValue - originalMin;
normalizedCurVal = zeroRefCurVal / OriginalRange; // normalize to 0 - 1 float
// Check for originalMin > originalMax - the math for all other cases i.e. negative numbers seems to work out fine
if (originalMin > originalMax ) {
return 0;
}
if (invFlag == 0){
rangedValue = (Math.pow(normalizedCurVal, curve) * NewRange) + newBegin;
}
else // invert the ranges
{
rangedValue = newBegin - (Math.pow(normalizedCurVal, curve) * NewRange);
}
return rangedValue;
}
const originalMin = 0;
const originalMax = 256;
const destMin = 0;
const destMax = 256;
const originalRange = Array.from(Array(originalMax).keys());
let defaultMappedRange = originalRange.map(x => fscale(originalMin, originalMax, destMin, destMax, x, -0.3));
function calcMappedRange(curveParamValue){
return originalRange.map(x => fscale(originalMin, originalMax, destMin, destMax, x, curveParamValue));
}
// Dataset 1 -> orginal values
// Dataset 2 -> mapped values
const data = {
labels: originalRange,
datasets: [
{
label: 'Original Values',
data: originalRange,
borderColor: 'rgb(255, 99, 132)',
backgroundColor: 'rgba(255, 99, 132,0.5)',
},
{
label: 'Mapped Values',
data: defaultMappedRange,
borderColor: 'rgb(54, 162, 235)',
backgroundColor: 'rgba(54, 162, 235,0.5)',
}
]
};
const config = {
type: 'line',
data: data,
options: {
responsive: true,
plugins: {
legend: {
position: 'top',
},
title: {
display: true,
text: 'FScale mapping'
}
}
},
};
const ctx = document.getElementById('chart');
let chart = new Chart(ctx, config);
var slider = document.getElementById("curveParam");
var output = document.getElementById("curveParamValue");
output.value = slider.value; // Display the default slider value
// Update the current slider value (each time you drag the slider handle)
slider.oninput = function() {
output.value = this.value;
chart.data.datasets[1].data = calcMappedRange(this.value);
chart.update();
}
output.oninput = function() {
if (this.value > 10.0) this.value = 10.0;
else if(this.value < -10.0) this.value = -10.0;
else{
slider.value = this.value;
chart.data.datasets[1].data = calcMappedRange(this.value);
chart.update();
}
}
</script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/themes/prism.min.css" integrity="sha512-/mZ1FHPkg6EKcxo0fKXF51ak6Cr2ocgDi5ytaTBjsQZIH/RNs6GF6+oId/vPe3eJB836T36nXwVh/WBl/cWT4w==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/prism.min.js" integrity="sha512-UOoJElONeUNzQbbKQbjldDf9MwOHqxNz49NNJJ1d90yp+X9edsHyJoAs6O4K19CZGaIdjI5ohK+O2y5lBTW6uQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</body>
</html>