You have multiple ways to interact from c++ to javascript. I can show you one way I find quite handy:
in the js library file
// here you write JS "handlers"
mergeInto(LibraryManager.library, {
new_cube: function(size, color) {
var cube = new THREE.Mesh(
new THREE.BoxGeometry(size, size, size),
new THREE.MeshBasicMaterial({ color: color })
);
}
});
in c++
extern "C" {
extern void new_cube(const int size, const char* color);
}
int main() {
new_cube(10, "red");
return 1;
}
in the build command
emcc source.cpp --js-library src/library.js
for easy debugging (but very slow)
#include <emscripten.h>
void emscripten_log(const char* string, bool escape = true)
{
char buff[1024];
sprintf(buff, (escape ? "console.log('%s');" : "console.log(%s);"), string);
emscripten_run_script(buff);
}
void emscripten_debugger()
{
emscripten_run_script("debugger");
}