-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstellar_lifespan_v1.html
221 lines (173 loc) · 8 KB
/
stellar_lifespan_v1.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
<!DOCTYPE html>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Stellar Lifespan Explorer</title>
<style>
.inputs {
border: 2px outset black;
background-color: lightblue;
text-align: left;
float:left;
}
.full-width {
width:100%;
float:left;
}
.page-embedable {
width:100%;
height:150px;
float:left;
}
.upper-right {
position:absolute;
top:0;
right:10px;
}
</style>
</head>
<body style="font-family:sans-serif; font-size:15px; width:700px; margin-left:auto; margin-right:auto;">
<h3 id="pageHeader"><a href="https://foothill.edu/astronomy/astrosims.html" target=_blank>AstroSims</a> > Stellar Lifespan Explorer</h3>
<div id="embeddableInteractive" class="page-embedable" align=center>
<div id="stellarParameters" class="inputs">
<!-- Mass <sup>ⓘ</sup> -->
Mass
<input type="text" id="stellarMassDisplay" size="8" value="1" oninput="updateInputs(0,0)"> <br>
<input id="stellarMassSlider" type ="range" min="-1" max="1.3" step="0.001" value="Error" oninput="updateInputs(0,1)"> <br>
Luminosity
<input type="text" id="stellarLuminosityDisplay" size="8" value="1" oninput="updateInputs(1,0)"> <br>
<input id="stellarLuminositySlider" type ="range" min="-4" max="5.2" step="0.001" value="Error" oninput="updateInputs(1,1)"> <br>
<div id="realisticLabel"> <input type="checkbox" id="realisticOnly" onclick="updateRealistic()"> Realistic only? </div> <br>
</div>
<div id="lifeDisplay" class="inputs">
<input type="text" id="lifespanDisplay" size="20" value="Error" readonly="true"> years
</div>
<!-- <div id="starAnimation" class="inputs">
<canvas id="canvas1" style="position:relative; left:0px; top:0px; background-color:rgb(0,0,0)" height="200" width="200">
</canvas>
</div>
-->
<div id="infoLink" class="upper-right" onclick="popupInfo()">
<h3 onmouseclick="popupInfo()">ⓘ</h3>
</div>
</div>
<script>
//console.log('initializing...');
// initialize variables
// Mass and Luminosity, relative to the sun
vars = [1.0,1.0];
// names of the interface elements for each variable
ids = [['stellarMassDisplay','stellarMassSlider'],['stellarLuminosityDisplay','stellarLuminositySlider']];
// indicate if sliders are on a log scale
logsliders = [true, true];
// stellar mass
realistic = false;
sunLifespan = 1E10;
//console.log('vars ', vars);
//console.log('interactive element IDs: ', ids);
// read parameters from URL
var urlParams = (new URL(document.location)).searchParams;
//console.log("Params are: "+urlParams);
if (urlParams.has('mass') == true) {
document.getElementById(ids[0][0]).value = urlParams.get('mass');
updateInputs(0,0);
//console.log("Mass should be: ", urlParams.get('mass'));
//console.log("Mass is: ", vars[0]);
}
if (urlParams.has('luminosity') == true) {
document.getElementById(ids[1][0]).value = urlParams.get('luminosity');
updateInputs(1,0);
//console.log("Luminosity should be: ", urlParams.get('luminosity'));
//console.log("Luminosity is: ", vars[1]);
}
if (urlParams.has('realistic') == true) {
//console.log("Realistic model loaded");
document.getElementById("realisticOnly").checked = true;
updateRealistic();
}
if (urlParams.has('fixedMass') == true) {
//console.log("Fixed mass");
document.getElementById(ids[0][0]).disabled = true;
document.getElementById(ids[0][1]).disabled = true;
document.getElementById("realisticOnly").disabled = true;
document.getElementById("realisticLabel").style.color = "#aaaaaa";
}
if (urlParams.has('fixedLuminosity') == true) {
//console.log("Fixed luminosity");
document.getElementById(ids[1][0]).disabled = true;
document.getElementById(ids[1][1]).disabled = true;
document.getElementById("realisticOnly").disabled = true;
document.getElementById("realisticLabel").style.color = "#aaaaaa";
}
if (urlParams.has('hideHeader') == true) {
//console.log("Fixed luminosity");
document.getElementById("pageHeader").style.display = "none";
}
console.log("Mass and Luminosity are: ", vars);
updateDisplay();
function updateInputs(iVar, iMode) {
console.log(iVar, iMode);
// adjust value by typing
if (iMode == 0) {
vars[iVar] = document.getElementById(ids[iVar][0]).value;
if (logsliders[iVar] == false) {
document.getElementById(ids[iVar][1]).value = vars[iVar];
}
if (logsliders[iVar] == true) {
document.getElementById(ids[iVar][1]).value = Math.log10(vars[iVar]);
}
}
// adjust values by slider
if (iMode == 1) {
if (logsliders[iVar] == false) {
vars[iVar] = document.getElementById(ids[iVar][1]).value;
}
if (logsliders[iVar] == true) {
vars[iVar] = trimNum(10**document.getElementById(ids[iVar][1]).value,2);
}
}
// if Realistic checked, then enforce M^4 approximation
if (realistic == true) {
if (iVar == 0) {
vars[1] = vars[0]**4.0;
}
if (iVar == 1) {
vars[0] = vars[1]**(0.25);
}
console.log(vars);
}
updateDisplay();
}
function updateRealistic() {
realistic = document.getElementById("realisticOnly").checked;
console.log(realistic);
if (realistic) {
updateInputs(0, 0);
}
}
function updateDisplay() {
//console.log('updateDisplay called ');
for (var i = 0; i < vars.length; i++) {
document.getElementById(ids[i][0]).value = vars[i].toLocaleString(undefined, {minimumFractionDigits:0, maximumSignificantDigits: 2});
if (logsliders[i] == false) {
document.getElementById(ids[i][1]).value = vars[i];
}
else {
document.getElementById(ids[i][1]).value = Math.log10(vars[i]);
}
}
// update stellar lifespan and display
stellarLifespan = vars[0] / vars[1] * sunLifespan;
document.getElementById('lifespanDisplay').value = (stellarLifespan).toLocaleString(undefined, {minimumFractionDigits:0, maximumSignificantDigits: 2});
}
function trimNum(input, toPrec) {
var numPlaces = Math.floor(Math.log10(input));
var divideBy = 10**(numPlaces - toPrec);
return divideBy * Math.floor(input / divideBy);
}
function popupInfo() {
var infoOption;
if (confirm("The Stellar Lifespan calculator is part of the Foothill College AstroSims project. Click OK to open the AstroSims homepage in a new tab to see our other simulations, as well as our listing of more than 300 other Astronomy education simulations and visualizations from authors around the world. \n \n Open https://foothill.edu/astronomy/astrosims.html in a new tab?")) {
window.open("https://foothill.edu/astronomy/astrosims.html","_blank");
}
}
</script>
</body></html>