emscripten-core / emscripten-core/emscripten

Tutorial Request: Creating a canvas component in React/Other

Open
#13,151 8 comments 0 reactions 0 assignees View on GitHub
wontfix
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

I've been trying to create my own React component that I can plug into my existing application but failed so far.

Creating a standalone emscripten application works perfectly but I have been facing several errors when trying to inject it dynamically.

Example with React hooks:

const loadWASM = async () => {
const script = document.createElement("script");
script.src = "/public/wasm/browser.js";
script.async = true;

document.body.appendChild(script);
}

This will fail and will result in:

```
browser.js:1 Uncaught TypeError: Cannot read property 'addEventListener' of null
at Object.registerOrRemoveHandler (browser.js:1)
at __registerMouseEventCallback (browser.js:1)
at _emscripten_set_mousemove_callback_on_thread (browser.js:1)
at :3000/public/wasm/browser.wasm:wasm-function[1562]:0xa86bf
at :3000/public/wasm/browser.wasm:wasm-function[1538]:0xa77d9
at :3000/public/wasm/browser.wasm:wasm-function[1606]:0xabd2f
at :3000/public/wasm/browser.wasm:wasm-function[898]:0x6661b
at :3000/public/wasm/browser.wasm:wasm-function[1192]:0x8378a
at Gd (:3000/public/wasm/browser.wasm:wasm-function[1034]:0x70874)
at Module._main (browser.js:1)
```

However, doing this will work perfectly:


var Module = {
canvas: (function() { return document.getElementById('canvas'); })()
};


Creating a guide/tutorial/example-repo on how to build a module that we can inject into any web application, would be something very interesting to have.

Although it is possible to inject it before react loads, after is loads the canvas gets stretched and I've no way of doing state management via redux/react.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.