Git Product home page Git Product logo

Comments (5)

Maksims avatar Maksims commented on May 14, 2024 1

UI libraries are not affected anyhow by PlayCanvas, except the fact that mouse input will propagate.
That means if your game does not care about events on canvas itself, then it just uses UI events as it used to.
But if you do care about custom input to canvas as well as UI above it, and you have a mixture of different behaviours, like in here for example: http://apps.playcanvas.com/max/tanky/colorTanks
Where you want mouse to be working in game when you still hovering minimap. And UI buttons still work.

That means it is up to a developer to define the logic of mouse interactions, and go from there.

In order to make all (dynamic selector) buttons in bootstrap to ignore mouse events, which is "logic" and not "workaround":

$('body').on('mousedown mousemove mouseup', '.btn', function(evt) {
    evt.stopPropagation();
});

It is up to your UI logic to do things, if you want to draw DOM image on top of canvas, or use div with text on top of 3D object using DOM, you might want to still have mouse events working as expected on canvas, ignoring div element above.
Or you might have custom behaviour by adding events as described above.

It is hard to find generic use case for such things as Keyboard and Mouse inputs in order to prevent most issues related to JS fragmentation on different platforms as well as allowing developers to define behaviour the way they want.

If we would provide UI framework, those things would be a part of API it self, but unfortunately we don't have any UI framework atm.

from engine.

Maksims avatar Maksims commented on May 14, 2024

The reason why mouse events are handled on Window: we need to handle scenarios when mouse leaves canvas bounds (for example when mouse is down and leaves canvas element), and to have mouse movement when there are elements above canvas and below mouse if developer wants it. Previously there was no option for such.

This gives possibility to have UI elements on top of canvas that have only visual purpose and it is up to developer to stop propagation of mouse events on hovering if dev wants so, or leave as it is.

In order to stop propagation you can easily follow this flow:

uiElement.addEventListener('mousedown', function(evt) {
    evt.stopPropagation();
});

Here is example where moving mouse above UI does not breaks game, especially notice it at bottom logo - game still playable.

This change were made to let developer have both options.

from engine.

muzzyfrank avatar muzzyfrank commented on May 14, 2024

The fix makes sense, but there are still a few issues. Most UI I imagine will likely be done in jQuery and other libraries (we are using bootstrap for some elements). It makes it a little bit difficult to have normal UI libraries work correctly.

The workaround on my end is to do something like this:

$('body > *:not(canvas)').on("mousedown mouseup mousemove mousewheel DOMMouseScroll", function(event) {
    event.stopPropagation();
});

This relies on the canvas always being a direct child of body - is that a valid assumption? Otherwise I'll have to likely make some kind of base css class that all UI elements include.

from engine.

muzzyfrank avatar muzzyfrank commented on May 14, 2024

That makes sense. I guess the only other way something like this could work is if UI developers had to manually send the mouse event to the canvas if they wanted pass-through instead of having the developer turn off propagation. It feels like more UI is likely to not want the event to propagate (especially because it will not propagate to the canvas by default in this way), but that is really just my opinion.

I would only recommend adding this to the documentation somewhere because it feels like other devs will probably run into this issue if they are building the UI in HTML.

from engine.

Maksims avatar Maksims commented on May 14, 2024

It is a valid point. Developer will have to know the way of achieving "transparent/non-transparent" elements in any case, so knowledge of stopPropagation or some other potential functionality to enforce propagation is necessary (unfortunately only "hacky" solutions come to my head for thing like that).

We will add to API reference for Mouse Input, stating clearly that Mouse events will fire as far as mouse position is within canvas bounds regardless of elements overlapping, unless the overlapped element caches the events and stops its propagation.

from engine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.