Skip to content

[WASM] Touch events not working in Windows browsers #3045

@k-okawa

Description

@k-okawa
  • axmol version: v2.11.x
  • windows version: Windows11 25H2 26200.7623

Likely due to the impact of a recent Windows update, touch events are no longer responding in major Windows browsers (Edge, Chrome, and Firefox).
Even the official Wasm sample(Axmol tests) fails to respond to touch input, making it impossible to interact with the application.

This issue appears to be specific to Windows; touch input works correctly on Android, iOS, and macOS.

My game, which was built with v2.11.0 last year, supported touch interaction on Windows at the time. However, it can no longer be operated using touch input.

As a temporary workaround, I am avoiding this issue by adding the following script to the HTML:

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

        // As a default initial behavior, pop up an alert when webgl context is lost. To make your
        // application robust, you may want to override this behavior before shipping!
        // See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
        canvas.addEventListener("webglcontextlost", function (e) { Module.ccall('axmol_webglcontextlost'); alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false);
        canvas.addEventListener("webglcontextrestored", function (e) { Module.ccall('axmol_webglcontextrestored'); e.preventDefault(); }, false);

        document.addEventListener("visibilitychange", () => { Module.ccall('axmol_hdoc_visibilitychange', document.hidden); });


        // Touch event workaround: Dispatch mouse events as touch events
        function simulateTouchFromMouse(mouseEvent, touchType) {
          var touch = new Touch({
            identifier: 0,
            target: canvas,
            clientX: mouseEvent.clientX,
            clientY: mouseEvent.clientY,
            screenX: mouseEvent.screenX,
            screenY: mouseEvent.screenY,
            pageX: mouseEvent.pageX,
            pageY: mouseEvent.pageY,
          });
          var touchEvent = new TouchEvent(touchType, {
            cancelable: true,
            bubbles: true,
            touches: touchType === 'touchend' ? [] : [touch],
            targetTouches: touchType === 'touchend' ? [] : [touch],
            changedTouches: [touch],
          });
          canvas.dispatchEvent(touchEvent);
        }

        var isMouseDown = false;
        canvas.addEventListener('mousedown', function (e) {
          isMouseDown = true;
          simulateTouchFromMouse(e, 'touchstart');
        }, { passive: false });
        canvas.addEventListener('mousemove', function (e) {
          if (isMouseDown) {
            simulateTouchFromMouse(e, 'touchmove');
          }
        }, { passive: false });
        canvas.addEventListener('mouseup', function (e) {
          isMouseDown = false;
          simulateTouchFromMouse(e, 'touchend');
        }, { passive: false });
        canvas.addEventListener('mouseleave', function (e) {
          if (isMouseDown) {
            isMouseDown = false;
            simulateTouchFromMouse(e, 'touchend');
          }
        }, { passive: false });

        return canvas;
      })(),

Thank you for maintaining this great engine. Let me know if you need any more information.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions