-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
229 lines (181 loc) · 6.07 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
<!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>Entropy Sliders</title>
</head>
<body>
<style>
html, body{
width: 100%;
height:100%;
margin:0px;
padding:0px;
}
body{
display:block;
width:100%;
height:100%;
background: rgb(146, 123, 108);
text-align: center;
font-family: Roboto !important;
font-weight: 300 !important;
overflow: hidden;
}
#sliders{
height:90%;
}
.slider-box{
background: rgba(10,10,10,0.3);
box-shadow: black 2px 2px 10px -5px;
display: inline-block;
height: 87%;
width: 50px;
margin: 0.5em;
padding: 4px;
border-radius: 4px;
overflow:hidden;
}
.slider{
position: relative;
transform: rotate(-90deg);
width: 64vh;
opacity: 0.5;
transform-origin: left;
top: 96%;
left: 50%;
}
.slider-num{
width: 40px;
overflow: hidden;
text-align: center;
margin: auto;
box-shadow: whitesmoke 0px 0px 10px -3px;
background: rgba(255,255,255,0.2);
padding: 5px;
}
input[type="range"] {
-webkit-appearance: none;
appearance: none;
background: transparent;
cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track {
background: linear-gradient(to right, rgb(1, 1, 60), rgb(50,50,150));
border-radius: 0.5rem;
height: 2rem;
}
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none; /* Override default look */
appearance: none;
/* margin-top: -12px; Centers thumb on the track */
/*custom styles*/
background-color: rgb(224, 248, 255);
height: 2rem;
width: 1.5rem;
border-radius: 2px;
transform: scale(1.0,1.5);
}
#entropy{
font-size: 1.5em;
}
</style>
<h1 id="entropy"></h1>
<div id="sliders">
</div>
<script>
function getParameterByName(name, url = window.location.href) {
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
function range(start, end, step){
if (step == undefined){
step = 1;
}
let l = [];
for(let i=start; i<end; i+=step){
l.push(i);
}
return l;
}
function round(num, n=1000){
return Math.round((num + Number.EPSILON) * n) / n
}
function getSliderValues(){
return range(1, n_sliders+1).map(id => parseFloat(document.querySelector(`#slider-${id}`).value));
}
function setSliderValue(id,val){
document.querySelector(`#slider-num-${id}`).innerHTML = round(val,100);
document.querySelector(`#slider-${id}`).value = val;
}
function normalize(vals, moved){
let n = vals.length;
let newVals = [];
let total = vals.reduce((acc,cur) => acc+cur, 0);
let scalar = (1 - vals[moved-1]) / (total - vals[moved-1] + 1e-8);
for(let i=1; i<=n; i++){
if(i == moved){
newVals.push(vals[i-1]);
}
else{
newVals.push(vals[i-1]*scalar);
}
}
return newVals;
}
function entropy(vals){
vals = vals.filter(x => x!=0)
return vals.reduce((a,x) => -Math.log2(x)*x + a, 0);
}
function setEntropy(){
let ent = entropy(getSliderValues());
let prob = round(Math.pow(2,-ent))
document.querySelector("#entropy").innerText = `Entropy: ${round(ent)}` + ` bits` + ` = -log2(${prob})`;
}
function adjustSliderValues(moved){
let newVals = normalize(getSliderValues(), moved);
for(let i=1; i<=n_sliders; i++){
setSliderValue(i, newVals[i-1]);
}
}
function makeListener(id, n) {
let listener = function(e) {
let slider = document.querySelector(`#slider-${id}`);
let sliderNum = document.querySelector(`#slider-num-${id}`);
adjustSliderValues(id);
setEntropy();
};
return listener;
}
function addSlider(id, n){
let container = document.querySelector("#sliders");
// let id = Array.from(document.querySelectorAll(".slider")).length + 1;
let slider = `
<div class="slider-box">
<input id="slider-${id}" class="slider" type="range" min="0.0001" max="1.0" value="${1/n}" step="0.0001"/>
<div id="slider-num-${id}" class="slider-num">0.0</div>
</div>
`;
container.innerHTML += slider;
}
let n_sliders = parseInt(getParameterByName("n_sliders"));
if(n_sliders == undefined){
n_sliders = 5;
}
for(let i=1; i<=n_sliders; i++){
addSlider(i, n_sliders);
}
adjustSliderValues(1);
for(let i=1; i<=n_sliders; i++){
document.querySelector(`#slider-${i}`).addEventListener("input", makeListener(i, n_sliders));
}
setEntropy();
</script>
</body>
</html>