index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. let app = document.getElementById("app");
  2. app.width = 800;
  3. app.height = 600;
  4. let ctx = app.getContext("2d");
  5. let w = null;
  6. function make_environment(...envs) {
  7. return new Proxy(envs, {
  8. get(target, prop, receiver) {
  9. for (let env of envs) {
  10. if (env.hasOwnProperty(prop)) {
  11. return env[prop];
  12. }
  13. }
  14. return (...args) => {console.error("NOT IMPLEMENTED: "+prop, args)}
  15. }
  16. });
  17. }
  18. // TODO: deploy the WASM examples to GitHub pages
  19. // TODO: display all the VC examples on a single page
  20. WebAssembly.instantiateStreaming(fetch('./build/squish.wasm'), {
  21. "env": make_environment({
  22. "atan2f": Math.atan2,
  23. "cosf": Math.cos,
  24. "sinf": Math.sin,
  25. "sqrtf": Math.sqrt,
  26. })
  27. }).then(w0 => {
  28. w = w0;
  29. let prev = null;
  30. function first(timestamp) {
  31. prev = timestamp;
  32. window.requestAnimationFrame(loop);
  33. }
  34. function loop(timestamp) {
  35. const dt = timestamp - prev;
  36. prev = timestamp;
  37. const pixels = w.instance.exports.render(dt*0.001);
  38. const buffer = w.instance.exports.memory.buffer;
  39. const image = new ImageData(new Uint8ClampedArray(buffer, pixels, app.width*app.height*4), app.width);
  40. ctx.putImageData(image, 0, 0);
  41. window.requestAnimationFrame(loop);
  42. }
  43. w.instance.exports.init();
  44. window.requestAnimationFrame(first);
  45. })