-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConversions.html
247 lines (200 loc) · 7.56 KB
/
Conversions.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
<html>
<style>
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
<head>
</head>
<body>
<p>
<div id="Inputs">
<div id="InputTable" style="min-width: 600px; max-width: 600px">
<div id="Material-Dropdown" style="float:right; vertical-align:bottom">
<label for="Materials">Material: </label>
<select name="Materials" id="Materials" onchange="ChangeMaterial()">
</select>
</div>
<div id="VolumesAB" style="overflow: hidden;">
<table>
<tr>
<td>Volume A: </td>
<td><input id="VolumeA" tabindex="1" size=26></td>
</tr>
<tr>
<td>Volume B: </td>
<td><input id="VolumeB" tabindex="2" size=26></td>
</tr>
<tr>
<td>Ratio: </td>
<td>
<input id="RatioA" tabindex="3" size=10>
<input id="RatioB" tabindex="4" size=10>
</td>
</tr>
</table>
</div>
</div>
<br>
<table>
<tr>
<td>Total Volume: </td>
<td> <input id="VolumeC" tabindex="5" size=26> </td>
</tr>
</table>
</div>
</p>
<button type="button" onclick="CalculateA()">Give me volume A!</button><br>
<button type="button" onclick="CalculateB()">Give me volume B!</button><br>
<button type="button" onclick="CalculateC()">Give me volume A & B!</button><br>
<br>
<button type="button" onclick="ResetVolumes()">Reset Volumes!</button><br>
<button type="button" onclick="ResetRatios()">Reset Ratios!</button><br>
<p>
<div id="Debug"></div>
</p>
<script src="https://code.jquery.com/jquery-3.7.1.slim.js"
integrity="sha256-UgvvN8vBkgO0luPSUl2s8TIlOSYRoGFAX4jlCIm9Adc=" crossorigin="anonymous"></script>
<script src="jquery.csv.js"></script>
<script>
let $Materials = [];
$(document).ready(function () {
document.getElementById("Debug").innerHTML = "ready";
const logFileText = async file => {
try {
console.log("fetch");
const response = await fetch(file);
console.log("fetch 2");
const text = await response.text();
console.log(text)
$Materials = $.csv.toObjects(text);
var select = document.getElementById('Materials');
$Materials.forEach((obj) => {
let str = "" + obj.name + ": " + obj.pretty_name + " ... " + obj.volume_a + ":" + obj.volume_b;
console.log(str);
var opt = document.createElement('option');
opt.value = obj.name;
opt.innerHTML = obj.pretty_name;
select.appendChild(opt)
})
}
catch (error) {
console.error(`Download error: ${error.message}`);
}
}
logFileText('https://garthor.github.io/3D/materials.csv')
});
function OnLoad() {
document.getElementById("RatioA").value = 1;
document.getElementById("RatioB").value = 1;
}
window.onload = OnLoad;
function ResetVolumes() {
document.getElementById("VolumeA").value = null;
document.getElementById("VolumeB").value = null;
document.getElementById("VolumeC").value = null;
}
function ResetRatios(InRatioA = 1, InRatioB = 1) {
ChangeMaterial()
}
function ChangeMaterial() {
var InMaterial = document.getElementById("Materials").value;
document.getElementById("Debug").innerHTML = "You selected: " + InMaterial;
for (var mat of $Materials) {
let str = "" + mat.name + ": " + mat.pretty_name + " ... " + mat.volume_a + ":" + mat.volume_b;
console.log(str);
if (mat.name === InMaterial) {
document.getElementById("RatioA").value = mat.volume_a;
document.getElementById("RatioB").value = mat.volume_b;
break;
}
}
}
// See this documentation about why "correction factors" works here: https://stackoverflow.com/a/18908122
function Convert(ratio, number, correctionFactor = 1.0) {
return ratio * correctionFactor * number * correctionFactor / Math.pow(correctionFactor, 2);
}
function CalculateA() {
let InVolumeB = document.getElementById("VolumeB").value;
let InRatioA = document.getElementById("RatioA").value;
let InRatioB = document.getElementById("RatioB").value;
InVolumeB = InVolumeB ? InVolumeB : 0;
InRatioA = InRatioA ? InRatioA : 0;
InRatioB = InRatioB ? InRatioB : 0;
if (InRatioA <= 0) {
InRatioA = 1;
document.getElementById("RatioA").value = InRatioA;
}
if (InRatioB <= 0) {
InRatioB = 1;
document.getElementById("RatioB").value = InRatioB;
}
let InRatio = InRatioA / InRatioB;
document.getElementById("Debug").innerHTML = "Calculating Amount of A Based on input:"
+ "<br>B = " + InVolumeB
+ "<br>Ratio " + InRatioA + ":" + InRatioB
+ " or " + (Number.isInteger(InRatio) ? InRatio.toFixed(1) : InRatio) + " to 1.";
let OutVolumeA = Convert(InRatio, InVolumeB, 1000);
let OutVolumeC = parseFloat(InVolumeB) + parseFloat(OutVolumeA);
document.getElementById("VolumeA").value = OutVolumeA;
document.getElementById("VolumeC").value = OutVolumeC;
}
function CalculateB() {
let InVolumeA = document.getElementById("VolumeA").value;
let InRatioA = document.getElementById("RatioA").value;
let InRatioB = document.getElementById("RatioB").value;
InVolumeA = InVolumeA ? InVolumeA : 0;
InRatioA = InRatioA ? InRatioA : 0;
InRatioB = InRatioB ? InRatioB : 0;
if (InRatioA <= 0) {
InRatioA = 1;
document.getElementById("RatioA").value = InRatioA;
}
if (InRatioB <= 0) {
InRatioB = 1;
document.getElementById("RatioB").value = InRatioB;
}
let InRatio = InRatioB / InRatioA;
document.getElementById("Debug").innerHTML = "Calculating Amount of B Based on input:"
+ "<br>A = " + InVolumeA
+ "<br>Ratio " + InRatioB + ":" + InRatioA
+ " or " + (Number.isInteger(InRatio) ? InRatio.toFixed(1) : InRatio) + " to 1.";
let OutVolumeB = Convert(InRatio, InVolumeA, 1000);
let OutVolumeC = parseFloat(OutVolumeB) + parseFloat(InVolumeA);
document.getElementById("VolumeB").value = OutVolumeB;
document.getElementById("VolumeC").value = OutVolumeC;
}
function CalculateC() {
let InVolumeC = document.getElementById("VolumeC").value;
let InRatioA = document.getElementById("RatioA").value;
let InRatioB = document.getElementById("RatioB").value;
InVolumeC = InVolumeC ? InVolumeC : 0;
InRatioA = InRatioA ? InRatioA : 0;
InRatioB = InRatioB ? InRatioB : 0;
if (InRatioA <= 0) {
InRatioA = 1;
document.getElementById("RatioA").value = InRatioA;
}
if (InRatioB <= 0) {
InRatioB = 1;
document.getElementById("RatioB").value = InRatioB;
}
let InRatioBA = InRatioB / InRatioA;
let InRatioAB = InRatioA / InRatioB;
document.getElementById("Debug").innerHTML = "Calculating Amount of A + B Based on input:"
+ "<br>C = " + InVolumeC
+ "<br>Ratio " + InRatioA + ":" + InRatioB
+ " or " + (Number.isInteger(InRatioAB) ? InRatioAB.toFixed(1) : InRatioAB) + " to 1.";
let OutVolumeA = Convert(1 / (1 + InRatioAB), InVolumeC, 1000);
let OutVolumeB = Convert(InRatioAB / (1 + InRatioAB), InVolumeC, 1000);
document.getElementById("VolumeA").value = OutVolumeA;
document.getElementById("VolumeB").value = OutVolumeB;
}
</script>
<br><br>
v 1.0.0.7
</body>
</html>