Gunnar
Gunnar

Reputation: 225

Handling Multi-Touch events in Emscripten in Safari

I am using Emscripten to get a number of C++ games running under HTML5. The client requires that these games also run in a mobile web browser, they do not want to make a native app.

The games require me to properly handle multitouch events - if the user applies three fingers to the screen, I have to react accordingly to all three fingers. However, I'm seeing an issue when testing on Safari on an iPad Mini, and I expect it happens elsewhere as well (though so far I'm not seeing it on Android).

What I'm finding is that when a touch callback is hit, for instance if I set a callback via emscripten_set_touchend_callback, then whenever a touchend event occurs, that callback gets hit with all active touchpoints included in the EmscriptenTouchEvent. As such, I don't know which touchpoint the event applies to. For instance if three fingers are down, and you lift the second finger, all three touchpoints are passed to the touchend callback. Is there a way for me to determine which touchpoint the touchend event applies to? If this is a bug, is there a known workaround?

Thank you in advance.

Upvotes: 2

Views: 1134

Answers (1)

Gunnar
Gunnar

Reputation: 225

Okay, after doing some research I found a solution on this website:

https://github.com/kripken/emscripten/issues/5012

They suggest that you edit library_html5.js in the Emscripten code and change:

    var touches = {};
    for(var i = 0; i < e.touches.length; ++i) {
      var touch = e.touches[i];
      touches[touch.identifier] = touch;
    }
    for(var i = 0; i < e.changedTouches.length; ++i) {
      var touch = e.changedTouches[i];
      touches[touch.identifier] = touch;
      touch.changed = true;
    }

With this:

    var touches = {};
    for(var i = 0; i < e.touches.length; ++i) {
      var touch = e.touches[i];
      touches[touch.identifier] = touch;
      touch.changed = 0;
    }
    for(var i = 0; i < e.changedTouches.length; ++i) {
      var touch = e.changedTouches[i];
      touches[touch.identifier] = touch;
      touch.changed = 1;
    }

I applied the change and confirmed that it fixes multi-touch on iOS platforms.

Upvotes: 1

Related Questions