Browse Source

Display all the WASM examples on a single page

rexim 3 years ago
parent
commit
7427d5a708
2 changed files with 36 additions and 20 deletions
  1. 8 2
      index.html
  2. 28 18
      index.js

+ 8 - 2
index.html

@@ -3,7 +3,13 @@
     <title>Olive.c</title>
   </head>
   <body>
-    <canvas id="app"></canvas>
-    <script src="index.js"></script>
+      <h1>Olive.c Demos</h1>
+      <h2 id="demo-triangle"><a href="#demo-triangle">Triangle</a></h2>
+      <canvas id="app-triangle"></canvas>
+      <h2 id="demo-3d"><a href="#demo-3d">3D</a></h2>
+      <canvas id="app-3d"></canvas>
+      <h2 id="demo-squish"><a href="#demo-squish">Squish</a></h2>
+      <canvas id="app-squish"></canvas>
+      <script src="index.js"></script>
   </body>
 </html>

+ 28 - 18
index.js

@@ -1,8 +1,4 @@
-let app = document.getElementById("app");
-app.width = 800;
-app.height = 600;
-let ctx = app.getContext("2d");
-let w = null;
+// TODO: deploy the WASM examples to GitHub pages
 
 function make_environment(...envs) {
     return new Proxy(envs, {
@@ -17,17 +13,28 @@ function make_environment(...envs) {
     });
 }
 
-// TODO: deploy the WASM examples to GitHub pages
-// TODO: display all the VC examples on a single page
-WebAssembly.instantiateStreaming(fetch('./build/squish.wasm'), {
-    "env": make_environment({
-        "atan2f": Math.atan2,
-        "cosf": Math.cos,
-        "sinf": Math.sin,
-        "sqrtf": Math.sqrt,
-    })
-}).then(w0 => {
-    w = w0;
+const libm = {
+    "atan2f": Math.atan2,
+    "cosf": Math.cos,
+    "sinf": Math.sin,
+    "sqrtf": Math.sqrt,
+};
+
+async function startExample(elementId, wasmPath) {
+    const app = document.getElementById(elementId);
+    if (app === null) {
+        console.error(`Could not find element ${elementId}. Skipping example ${wasmPath}...`);
+        return;
+    }
+
+    app.width = 800;
+    app.height = 600;
+    const ctx = app.getContext("2d");
+    const w = await WebAssembly.instantiateStreaming(fetch(wasmPath), {
+        "env": make_environment(libm)
+    });
+
+    w.instance.exports.init();
 
     let prev = null;
     function first(timestamp) {
@@ -45,6 +52,9 @@ WebAssembly.instantiateStreaming(fetch('./build/squish.wasm'), {
 
         window.requestAnimationFrame(loop);
     }
-    w.instance.exports.init();
     window.requestAnimationFrame(first);
-})
+}
+
+startExample("app-triangle", "./build/triangle.wasm");
+startExample("app-3d", "./build/3d.wasm");
+startExample("app-squish", "./build/squish.wasm");