I am looking to interact between JavaScript and the compiled emscripten oF app.
With the .html page generated with emscripten there is already some code to get the console output from the oF app so getting variables from oF to JavaScript works but is there a way to pass variable from JavaScript to the oF app?
thank you
you can call a js function from the c++ code and get a value that way. take a look at how the video player or grabber for example are implemented
Thank you for your reply @arturo
I had a look at the ofxEmscripten video player but I am still unsure how it works.
Without success I tried to to create a text box in the html page and read the value in the oF app, I tried to use similar structure and model as the video player and did the following
created a file
library_html5com.js
var LibraryHTML5Com = {
$COM: {getJavaString: function(){ var ret = document.getElementById('stringJava') return ret; }
}
autoAddDeps(LibraryHTML5Com, ‘$COM’);
mergeInto(LibraryManager.library, LibraryHTML5Com);
created a file
html5com.h
pragma once
extern “C”{
extern std::string getJavaString();
}
created ofxEmscriptenCom.h
pragma once
include string
class ofxEmscriptenCom {
public:ofxEmscriptenCom(); ~ofxEmscriptenCom(); std::string getJavaString();
};
ofxEmscriptenCom.cpp
include “ofxEmscriptenCom.h”
include “html5com.h”
ofxEmscriptenCom::ofxEmscriptenCom(){}
ofxEmscriptenCom::~ofxEmscriptenCom(){}
std::string ofxEmscriptenCom::getJavaString(){
return getJavaString();
}
I have included ofxEmscripten addons to my project and in ofApp.cpp update() I have
ofxEmscriptenCom com;
string javaString = com.getJavastring();
Finally in the myApp.html generated from “emmake make” I have added
input type=‘text’ id=‘stringJava’ />
input type=‘button’ onclick=‘notEmpty()’ value=‘Form Checker’ />
here’s the official emscripten guide on this topic: https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html it’s pretty well explained
Ok I had a look at the document
so using the funtion emscripten_run_script works
here is an exemple to get a string from a html text box
in ofApp.h
#ifdef TARGET_EMSCRIPTEN #include <emscripten/emscripten.h> #endif`
in ofApp.cpp
#ifdef TARGET_EMSCRIPTEN char * inputString= emscripten_run_script_string("document.getElementById('stringJava').value"); #endif
in myApp.html
input type='text' id='stringJava' /> input type='button' onclick='notEmpty()' value='Form Checker' />