Matter-js: `Mouse` eats all mouse events by default

Created on 1 May 2015  路  2Comments  路  Source: liabru/matter-js

The Mouse class doesn't support the following use case:

  • I want the page to scroll, even if the user is hovering over the canvas and that is the element I've specified when creating the Mouse instance.

This is because the Mouse instance calls preventDefault on all mousewheel and DOMMouseScroll events. Additionally, it calls preventDefault on all other mouse and touch events: mousemove, mousedown, mouseup, touchmove, touchstart, and touchend.

For integrating a Matter.js scene into an environment with complex mouse interactions, it can be useful to leverage the Mouse class for sending interactions into the scene while integrating one's own interaction logic elsewhere.

Most helpful comment

This has been raised before in #47 and as was mentioned there it's easy to just remove the event listeners in user code, something like:

mouse.element.removeEventListener("mousewheel", mouse.mousewheel);
mouse.element.removeEventListener("DOMMouseScroll", mouse.mousewheel);

It might be nice to have this functionality built in I guess, but I'm always a little reluctant to extend the features for non-physics parts of the codebase (I want to keep the project scope tight, see #65).

All 2 comments

My solution: ability to disable mousewheel listener entirely (since I didn't need scrolling in my physics interaction). So when I create my mouse instance I can disable the mousewheel event listener:

var mouse = Matter.Mouse.create(
  myCanvasElement,
  {
    enabledEvents: {
      mousewheel: false
    }
  }
);

Would you accept something like this?

https://github.com/colinsullivan/matter-js/commit/c5edfb243c1cb10a3ba696e876ca0061cf4d7b35

I think it should include options for disabling the other types of events too.

This has been raised before in #47 and as was mentioned there it's easy to just remove the event listeners in user code, something like:

mouse.element.removeEventListener("mousewheel", mouse.mousewheel);
mouse.element.removeEventListener("DOMMouseScroll", mouse.mousewheel);

It might be nice to have this functionality built in I guess, but I'm always a little reluctant to extend the features for non-physics parts of the codebase (I want to keep the project scope tight, see #65).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jack-guy picture jack-guy  路  3Comments

car1ot picture car1ot  路  3Comments

koko236 picture koko236  路  3Comments

christianmalek picture christianmalek  路  4Comments

kunchenguid picture kunchenguid  路  3Comments