emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

Mouse scroll delta is not scaled properly

Open ghost opened this issue 8 years ago • 26 comments

I've done some digging and I think this is true for all scrolling in all libraries.

Using GLFW to listen to scroll events (glfwSetScrollCallback) gives me two doubles: Y and X delta. These are majorly differing between Firefox and Chrome (because as I have found, you should scale the delta according to if you get PIXELS or LINES).

In library_glfw.js, glfwInit function you listen to 'wheel' event (and others), handler is GLFW.onMouseWheel. There you call Browser.getMouseWheelDelta which for the 'wheel' event simply returns deltaY. Then this delta is just passed up to GLFW handlers.

What needs to be done is to properly handle the WheelEvent.deltaMode which can be DOM_DELTA_PIXEL or DOM_DELTA_LINE or even DOM_DELTA_PAGE.

When Firefox gives delta in LINES and Chrome gives delta in PIXELS there's a major inconsistency for apps run in those two browsers. Browser.getMouseWheelDelta is used by all libraries it seems to this issue should affect all scrolling.

ghost avatar Feb 26 '18 21:02 ghost

I made this HTML to display and test the behavior:

<html>

<head>
<script>

// I found this somewhere
function getScrollLineHeight() {
    var r;
    var iframe = document.createElement('iframe');
    iframe.src = '#';
    document.body.appendChild(iframe);
    var iwin = iframe.contentWindow;
    var idoc = iwin.document;
    idoc.open();
    idoc.write('<!DOCTYPE html><html><head></head><body><span>a</span></body></html>');
    idoc.close();
    var span = idoc.body.firstElementChild;
    r = span.offsetHeight;
    document.body.removeChild(iframe);
    return r;
}

function handler(e) {
// pixels or lines?
if (e.deltaMode == 0) {
console.log(e.deltaY);
} else if (e.deltaMode == 1) {
console.log(e.deltaY * getScrollLineHeight());
}

}

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

canvas.addEventListener("wheel", handler, false);
}
</script>
</head>

<body onload="main();">

<canvas id="canvas" style="width: 600px; height 600px; background-color: red;"></canvas>

</body>

</html>

ghost avatar Feb 26 '18 22:02 ghost

I've encountered the same problem while using SDL API: Y delta is different about ~30 times between Firefox and Chrome. The Firefox values are similar to what I get when running native SDL on Linux.

I've also came across this 4-year old issue. Looks like it was closed without normalizing the values between browsers.

centurn avatar Dec 11 '18 15:12 centurn

Having this issue aswell

feliwir avatar Dec 19 '18 09:12 feliwir

Looks like the specific issue is in line 568 of library_browser.js:

getMouseWheelDelta: function(event) {
var delta = 0;
  switch (event.type) {
    case 'DOMMouseScroll':
      delta = event.detail;
      break;
    case 'mousewheel':
      delta = event.wheelDelta;
      break;
    case 'wheel':
      delta = event['deltaY'];
      break;
    default:
      throw 'unrecognized mouse wheel event: ' + event.type;
  }
  return delta;
},

I just found this solution from facebook. They essentially convert all values into pixels.

I would suggest that we also return those estimated pixels by default. Specific libraries like library_glfw.js could then attempt to recreate the behaviour of their originals.

flostellbrink avatar Jan 30 '19 03:01 flostellbrink

Does the fix apply to SDL2 too? I have this same issue now with 1.39.3.

malytomas avatar Nov 18 '19 15:11 malytomas

@malytomas Sorry to disappoint. This was really just a legacy patch. SDL1 and GLFW2 should work, but neither GLFW3 nor SDL2 are affected by this. This also only supports vertical scrolling.

I think the html5 api would be the place to fix issues with SDL2. https://github.com/emscripten-core/emscripten/blob/1abc0c76764e33e1f14fe7a8e5f44eedec8ddb41/src/library_html5.js#L552-L617

flostellbrink avatar Nov 18 '19 16:11 flostellbrink

Are there any updates on a SDL2 fix? This is causing major issues for us

feliwir avatar May 15 '20 09:05 feliwir

I'm not aware of someone working on it. cc @Daft-Freak who might remember more though.

If someone is interested to investigate and work on a patch for this, let me know if there is anything I can do to speed that along.

kripken avatar May 15 '20 17:05 kripken

I think I remember this being mentioned at some point, but doesn't look like anything was implemented...

Looks like we need to check wheelEvent->deltaMode here: https://github.com/emscripten-ports/SDL2/blob/master/src/video/emscripten/SDL_emscriptenevents.c#L425. I don't think SDL2 defines what the units should be though.

Daft-Freak avatar May 15 '20 20:05 Daft-Freak

@Daft-Freak is there a library / implementation that handles that correctly?

feliwir avatar Jun 30 '20 10:06 feliwir

Having the same issue, a super hacky fix, for now, it's to normalize everything [-1.0, 1.0]. Which it's pants. But at least it "works" for us somehow.

I do not understand were the root of the problem is, in the GLFW and SDL callbacks? Or in the general HTML5 wheel handling code? I won't mind having a go if you can confirm where exactly the issue arises.

ziocleto avatar Jul 26 '20 05:07 ziocleto

@ziocleto i'm not sure either why this can't be fixed. This is literally breaking our entire application on some browsers

feliwir avatar Sep 07 '20 09:09 feliwir

@feliwir Me neither, IIRC I had a go to try to understand the issue but I really gave up as it seems nobody really knows all the details of this mess. And yes, even for us, it broke everything, well it still does, so annoying... If you have any updates, let us know!

ziocleto avatar Sep 07 '20 10:09 ziocleto

@ziocleto i investigated a bit further and found out that the deltaMode is different for Chrome & Firefox (thanks @Daft-Freak ). However i'm not certain what's the correct formula to fix this properly. This worked for me:

static EM_BOOL
Emscripten_HandleWheel(int eventType, const EmscriptenWheelEvent *wheelEvent, void *userData)
{
    SDL_WindowData *window_data = userData;
    // Default mode is in pixels
    float deltaX = wheelEvent->deltaX;
    float deltaY = wheelEvent->deltaY;
    // If delta mode is in lines
    if(wheelEvent->deltaMode == DOM_DELTA_LINE)
    {
        deltaX *= 20.0f;
        deltaY *= 20.0f;
    }
    else if(wheelEvent->deltaMode == DOM_DELTA_PAGE)
    {
        //TODO:
    }

    SDL_SendMouseWheel(window_data->window, wheelEvent->deltaMode, deltaX, -deltaY, SDL_MOUSEWHEEL_NORMAL);
    return SDL_GetEventState(SDL_MOUSEWHEEL) == SDL_ENABLE;
}

However this will probably break for Retinna etc. Can't we just pass the deltaMode to the callback aswell?

feliwir avatar Sep 07 '20 11:09 feliwir

@ziocleto i investigated a bit further and found out that the deltaMode is different for Chrome & Firefox (thanks @Daft-Freak ).

I said exactly this in my very first report 2 years ago in the top of this very issue report if you care to read it...

ghost avatar Sep 07 '20 12:09 ghost

Indeed, thanks to @alexhultman aswell, but can we fix this now?

feliwir avatar Sep 07 '20 12:09 feliwir

You need to know the height of a "row" and scale it accordingly. That's in the initial report. [On] Firefox [emscripten] treats the input given as "rows" as if they were pixels. That's why the scrolling is wildly incorrect. I guess you need to read the font metrics or the font-size or something like that.

ghost avatar Sep 07 '20 12:09 ghost

@alexhultman well, we display text ourselves (with WebGL), so it would be enough for us to get a notification by the SDL port that this is LINES or PIXELS, so we can handle the difference on our side.

Maybe we could add new wheel types?

feliwir avatar Sep 07 '20 12:09 feliwir

I did look at this for SDL2, but the values seem to vary between platforms. (And possibly system settings?)

To get scroll events matching native on Windows, I needed /3 for LINE(Firefox) and /200 for PIXEL(Chrome). But on my Linux machine Chrome was 106...

Daft-Freak avatar Sep 08 '20 11:09 Daft-Freak

BTW As I see that you guys are talking about SDL, I do not think the problem is related to SDL per se, we use GLFW for example, so fixing it should be agnostic from the graphics framework.

ziocleto avatar Sep 09 '20 16:09 ziocleto