-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
30 lines (25 loc) · 1021 Bytes
/
index.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
import init from "./pkg/wasm_pi.js";
// Attach our wasm_pi function to the window object
window.compute_pi = async () => {
// Disable compute button while running
document.getElementById("compute-button").disabled = true;
// Get the parameter value from the input field
const n_samples = parseInt(document.getElementById("n_samples").value);
if (isNaN(n_samples)) {
alert("Please enter a number");
}
else {
// Instantiate our wasm module (or use the one that was already instantiated)
const wasm_pi = await init();
// Compute the result with WebAssembly
try {
const pi_estimate = wasm_pi.monte_carlo_pi(n_samples);
// Set the result on the page
document.getElementById("pi_estimate").textContent = pi_estimate;
} catch (e) {
alert("Wasm error: " + e.message);
}
}
// Re-enable the compute button
document.getElementById("compute-button").disabled = false;
};