This commit is contained in:
Simon Gardling
2022-02-10 17:44:29 -05:00
commit 1bd2f919a8
12 changed files with 1085 additions and 0 deletions

20
www/bootstrap.js vendored Normal file
View File

@@ -0,0 +1,20 @@
init();
async function init() {
if (typeof process == "object") {
const [{Chart}, {main, setup}] = await Promise.all([
import("integral_site"),
import("./index.js"),
]);
setup(Chart);
main();
} else {
const [{Chart, default: init}, {main, setup}] = await Promise.all([
import("../pkg/integral_site.js"),
import("./index.js"),
]);
await init();
setup(Chart);
main();
}
}

45
www/index.html Normal file
View File

@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Integral Demonstration</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<noscript>Please enable Javascript, this page uses both WebAssembly and Javascript to run.</noscript>
<script src="./bootstrap.js"></script>
<main>
<h1>Integral Demonstration</h1>
<div id="coord"></div>
<canvas id="canvas" width="600" height="400"></canvas>
<div id="status">Loading WebAssembly...</div>
<div id="control">
<label for="math_function">y=</label> <input type="string" id="math_function" value="x^2">
<p></p>
<label for="minX">MinX: </label> <input type="number" id="minX" value="-10">
<p></p>
<label for="maxX">MaxX: </label> <input type="number" id="maxX" value="10">
<p></p>
<label for="minY">MinY: </label> <input type="number" id="minY" value="-10">
<p></p>
<label for="maxY">MaxY: </label> <input type="number" id="maxY" value="10">
<p></p>
<label for="num_interval">Interval: </label> <input type="number" id="num_interval" value="100" min="0">
<p></p>
<label for="resolution">Number of Points </label> <input type="number" id="resolution" value="10000" min="0">
<p></p>
</div>
<div id="area-msg">Area:</div>
</main>
</body>
</html>
<h3 id="Supported%20Expressions:">Supported Expressions:</h3>
<ul>
<li><p><code>sqrt</code>, <code>abs</code></p></li>
<li><p><code>exp</code>, <code>ln</code>, <code>log10</code></p></li>
<li><p><code>sin</code>, <code>cos</code>, <code>tan</code>, <code>asin</code>, <code>acos</code>, <code>atan</code>, <code>atan2</code></p></li>
<li><p><code>sinh</code>, <code>cosh</code>, <code>tanh</code>, <code>asinh</code>, <code>acosh</code>, <code>atanh</code></p></li>
<li><p><code>floor</code>, <code>ceil</code>, <code>round</code></p></li>

84
www/index.js Normal file
View File

@@ -0,0 +1,84 @@
// If you only use `npm` you can simply
// import { Chart } from "wasm-demo" and remove `setup` call from `bootstrap.js`.
class Chart {}
const canvas = document.getElementById("canvas");
const coord = document.getElementById("coord");
const math_function = document.getElementById("math_function");
const status = document.getElementById("status");
const minX = document.getElementById("minX");
const maxX = document.getElementById("maxX");
const minY = document.getElementById("minY");
const maxY = document.getElementById("maxY");
const num_interval = document.getElementById("num_interval");
const area_msg = document.getElementById("area-msg");
const resolution = document.getElementById("resolution");
let chart = null;
/** Main entry point */
export function main() {
setupUI();
setupCanvas();
}
/** This function is used in `bootstrap.js` to setup imports. */
export function setup(WasmChart) {
Chart = WasmChart;
}
/** Add event listeners. */
function setupUI() {
status.innerText = "WebAssembly loaded!";
math_function.addEventListener("change", updatePlot);
minX.addEventListener("input", updatePlot);
maxX.addEventListener("input", updatePlot);
minY.addEventListener("input", updatePlot);
maxY.addEventListener("input", updatePlot);
num_interval.addEventListener("input", updatePlot);
resolution.addEventListener("input", updatePlot);
window.addEventListener("resize", setupCanvas);
window.addEventListener("mousemove", onMouseMove);
}
function setupCanvas() {
const aspectRatio = canvas.width / canvas.height;
const size = canvas.parentNode.offsetWidth * 0.8;
canvas.style.width = size + "px";
canvas.style.height = size / aspectRatio + "px";
canvas.width = size;
canvas.height = size / aspectRatio;
updatePlot();
}
function onMouseMove(event) {
if (chart) {
var text = "Mouse is outside Chart.";
if(event.target == canvas) {
let actualRect = canvas.getBoundingClientRect();
let logicX = event.offsetX * canvas.width / actualRect.width;
let logicY = event.offsetY * canvas.height / actualRect.height;
const point = chart.coord(logicX, logicY);
text = (point)
? `(${point.x.toFixed(3)}, ${point.y.toFixed(3)})`
: text;
}
coord.innerText = text;
}
}
function updatePlot() {
status.innerText = `Rendering y=${math_function.value}...`;
chart = null;
const start = performance.now();
chart = Chart.draw(canvas, math_function.value, Number(minX.value), Number(maxX.value), Number(minY.value), Number(maxY.value), Number(num_interval.value), Number(resolution.value));
const end = performance.now();
area_msg.innerText = `Estimated Area: ${chart.get_area()}`;
status.innerText = `Rendered ${math_function.innerText} in ${Math.ceil(end - start)}ms`;
}

18
www/package.json Normal file
View File

@@ -0,0 +1,18 @@
{
"name": "integral_site",
"version": "0.1.0",
"main": "index.js",
"scripts": {
"build": "webpack --config webpack.config.js",
"start": "webpack-dev-server"
},
"dependencies": {
"integral_site": "file:../pkg"
},
"devDependencies": {
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.10.3",
"copy-webpack-plugin": "^5.0.0"
}
}

55
www/style.css Normal file
View File

@@ -0,0 +1,55 @@
html, body, main {
width: 100%;
margin: 0;
padding: 0;
}
body {
margin: auto;
max-width: 800px;
display: flex;
flex-direction: column;
}
@media (max-width: 800px) {
body {
padding: 10px;
box-sizing: border-box;
}
}
main {
display: flex;
flex-direction: column;
align-items: center;
}
#coord, #status {
color: grey;
font-size: 10px;
height: 15px
}
#control {
margin-top: 1em;
}
#control label {
font-weight: bold;
margin-right: 1em;
}
#control select {
padding: 0.25em 0.5em;
}
footer {
margin-top: 2em;
font-size: 12px;
text-align: center;
}
.hide {
visibility: hidden;
height: 0px;
}

14
www/webpack.config.js Normal file
View File

@@ -0,0 +1,14 @@
const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require('path');
module.exports = {
entry: "./bootstrap.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "bootstrap.js",
},
mode: "development",
plugins: [
new CopyWebpackPlugin(['index.html'])
],
};