-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
195 lines (168 loc) · 6.35 KB
/
script.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
function animateValue(id, start, end, duration) {
const obj = document.getElementById(id);
const range = end - start;
const minTimer = 50;
let stepTime = Math.abs(Math.floor(duration / range));
stepTime = Math.max(stepTime, minTimer);
let startTime = new Date().getTime();
let endTime = startTime + duration;
let timer;
function run() {
let now = new Date().getTime();
let remaining = Math.max((endTime - now) / duration, 0);
let value = Math.round(end - (remaining * range));
obj.innerText = value.toFixed(2);
if (value == end) {
clearInterval(timer);
}
}
timer = setInterval(run, stepTime);
run();
}
function validateAndCalculate() {
// Get values from input fields
const Rstar = parseFloat(document.getElementById('Rstar').value);
const fp = parseFloat(document.getElementById('fp').value);
const ne = parseFloat(document.getElementById('ne').value);
const fl = parseFloat(document.getElementById('fl').value);
const fi = parseFloat(document.getElementById('fi').value);
const fc = parseFloat(document.getElementById('fc').value);
const L = parseFloat(document.getElementById('L').value);
let isValid = true;
// Validate values
if (isNaN(Rstar) || Rstar < 0) {
alert("Please enter a valid value for the star formation rate (R*).");
isValid = false;
}
if (isNaN(fp) || fp < 0 || fp > 1) {
alert("Please enter a valid value for the fraction of stars with planetary systems (fₚ).");
isValid = false;
}
if (isNaN(ne) || ne < 0) {
alert("Please enter a valid value for the number of planets per star system that can support life (nₑ).");
isValid = false;
}
if (isNaN(fl) || fl < 0 || fl > 1) {
alert("Please enter a valid value for the fraction of those planets where life develops (fₗ).");
isValid = false;
}
if (isNaN(fi) || fi < 0 || fi > 1) {
alert("Please enter a valid value for the fraction of planets with life where intelligent life evolves (fᵢ).");
isValid = false;
}
if (isNaN(fc) || fc < 0 || fc > 1) {
alert("Please enter a valid value for the fraction of civilizations that develop detectable technology (f꜀).");
isValid = false;
}
if (isNaN(L) || L < 0) {
alert("Please enter a valid value for the duration of the detectable phase of the civilization (L).");
isValid = false;
}
if (isValid) {
// Calculate the result using the Drake Equation
const N = Rstar * fp * ne * fl * fi * fc * L;
// Animate the result on the page
animateValue('result', 0, N, 2000); // 2000 ms duration
// Determine the last changed parameter
const formInputs = ['Rstar', 'fp', 'ne', 'fl', 'fi', 'fc', 'L'];
const lastChangedParameter = formInputs.find(input => document.getElementById(input) === document.activeElement) || 'Rstar';
// Update chart for the last changed parameter
updateChart(lastChangedParameter);
}
}
let drakeChart;
function initChart() {
Chart.defaults.font.family = '"Space Mono", monospace';
const ctx = document.getElementById('drakeChart').getContext('2d');
drakeChart = new Chart(ctx, {
type: 'line',
data: {
labels: [],
datasets: [{
label: 'N (Number of civilizations)',
data: [],
borderColor: 'rgb(247, 191, 32)',
tension: 0.1
}]
},
options: {
responsive: true,
scales: {
x: {
title: {
display: true,
text: 'Parameter Value',
color: 'rgba(0, 0, 0, 0.8)'
},
grid: {
color: 'rgba(0, 0, 0, 0.3)'
},
ticks: {
color: 'rgba(0, 0, 0, 0.8)'
}
},
y: {
type: 'logarithmic',
title: {
display: true,
text: 'N',
color: 'rgba(0, 0, 0, 0.8)'
},
grid: {
color: 'rgba(0, 0, 0, 0.3)'
},
ticks: {
color: 'rgba(0, 0, 0, 0.8)',
callback: function(value, index, values) {
return Number(value).toFixed(2);
}
}
}
},
plugins: {
legend: {
labels: {
color: 'rgba(0, 0, 0, 0.8)'
}
}
}
}
});
}
function updateChart(parameter) {
const values = [];
const results = [];
// Get the current value of the parameter
const currentValue = parseFloat(document.getElementById(parameter).value);
// Generate 50 points for the chart
for (let i = 0; i < 50; i++) {
const value = currentValue * (i / 25); // This will create a range from 0 to 2 times the current value
values.push(value);
// Clone current form values
const formData = new FormData(document.getElementById('drake-form'));
formData.set(parameter, value);
// Calculate N for this point using the Drake equation
const Rstar = parseFloat(formData.get('Rstar'));
const fp = parseFloat(formData.get('fp'));
const ne = parseFloat(formData.get('ne'));
const fl = parseFloat(formData.get('fl'));
const fi = parseFloat(formData.get('fi'));
const fc = parseFloat(formData.get('fc'));
const L = parseFloat(formData.get('L'));
const N = Rstar * fp * ne * fl * fi * fc * L;
results.push(N);
}
drakeChart.data.labels = values;
drakeChart.data.datasets[0].data = results;
drakeChart.options.scales.x.title.text = parameter;
drakeChart.options.scales.y.type = 'logarithmic';
drakeChart.update();
}
// Call initChart when the page loads
window.onload = function() {
initChart();
clearForm();
};
function clearForm() {
document.getElementById('drake-form').reset();
}