phaser icon indicating copy to clipboard operation
phaser copied to clipboard

DOMElement configure pointerEvents independently of transformOnly

Open hayatae opened this issue 5 years ago • 11 comments

Currently, the pointerEvents style property of DOMElements is set on render to be auto if the element is not in transformOnly mode. This creates a bit of an issue if you'd like to have all the other advantages of having phaser track opactiy, display, zIndex, and blendMode, but do not want the mouse pointer to be intercepted by the element. It is not clear to me why pointerEvents are tied into these display properties.

https://github.com/photonstorm/phaser/blob/416ae58da3e010f051fb03db58389a001536cfbc/src/gameobjects/domelement/DOMElementCSSRenderer.js#L106

It seems like an easy fix would be to expose a separate pointerEvents property which could be set to any valid css option for pointer-events. This property could be defaulted based on the trasnformOnly property to maintain backwards compatibility. This would allow a developer to override the pointer-events to anything they need it to be.

I'd be comfortable implementing this feature and creating a PR for it if this sounds like a good approach.

hayatae avatar Dec 27 '20 04:12 hayatae

Hey, I'm also in the need of this 🙋

I'm wondering if style.pointerEvents really needs to be set at all? Maybe the end-user should be responsible for setting this CSS property, like this:

scene.add.dom(x, y, "div", `
  color: #000;
  pointer-events: none;
`, "hello world");

It seems the pointer-events is being overwritten in the current implementation, right?

endel avatar Jan 14 '21 13:01 endel

The MDN documentation mentions that auto is the default value. So I believe it's safe to remove it.

auto: The element behaves as it would if the pointer-events property were not specified.

Made a PR for this here 😅 https://github.com/photonstorm/phaser/pull/5504

endel avatar Jan 14 '21 13:01 endel

Thank you for submitting this issue. We have fixed this and the fix has been pushed to the master branch. It will be part of the next release. If you get time to build and test it for yourself we would appreciate that.

photonstorm avatar Feb 16 '21 11:02 photonstorm

This change has sadly broken so many things it's time to revert it all back to how it was previously.

photonstorm avatar Mar 28 '21 15:03 photonstorm

This change has sadly broken so many things it's time to revert it all back to how it was previously.

I was afraid of that happening. Can we fix this by simply adding a new separate property (as suggested in the initial post) to allow overriding the default behavior in Phaser? That would allow for overriding this property if desired while not affecting any existing code.

hayatae avatar Mar 28 '21 17:03 hayatae

I could, but the problem is that if you change it to anything else, you'll break Phaser-related input at the same time (see all of the various issues that have arisen from this change to get an idea of what it breaks!).

photonstorm avatar Mar 30 '21 08:03 photonstorm

Ok, I have reverted both the PRs that caused all the issues. I have also exposed a couple of new properties you can use to set the pointerEvents attribute yourself, but as the docs for it says: if you change it without appreciating what you've changed, you'll break Phaser input at the same time.

photonstorm avatar Mar 30 '21 09:03 photonstorm

Ok, I have reverted both the PRs that caused all the issues. I have also exposed a couple of new properties you can use to set the pointerEvents attribute yourself, but as the docs for it says: if you change it without appreciating what you've changed, you'll break Phaser input at the same time.

I finally tried upgrading from 3.52 to 3.53.1 and 3.54.0 and immediately ran into all the various DOM problems that cropped up because of this change. Had to revert back to 3.52.0 for now. Didn't realize how many things that single line affected outside of the CSS that was being applied to user-added dom elements.

Sorry for all the trouble this ended up causing!

Edited to update: I just realized why everything broke so badly with this PR. Originally, the DOMContainer had pointer-events: none. This means that all child elements must have pointer-events: auto (or another setting) in order to be interactive at all. In the previous release, this was always enforced, so everything was interactive by default. After the PR was merged, everything became non-interactive by default because that explicit default override was removed, thereby breaking everyone's existing code.

In 3.54.0, the DOMContainer had its pointer-events: none property removed in order to default to auto, but this caused all pointer events on the canvas to break because they were now all being intercepted by the overlaying DOMContainer by default, thereby preventing them from reaching the canvas at all.

I'm sorry I didn't review the PR changes carefully for this one and comment further earlier. I had worried that changing the default behavior would have unintended consequences like this. This could have all been avoided by leaving the DOMContainer set to pointer-events: none and allowing for an optional override property on each dom element added to allow the programmer to override the pointer-events property on a specific child node.

hayatae avatar Apr 12 '21 03:04 hayatae

Yes, you can now set pointerEvents directly on a DOM Element, so you can control it on a per-element basis as well as globally in the game config, which is what you originally suggested but was sadly never provided in the PR.

photonstorm avatar Apr 13 '21 15:04 photonstorm

I'm sorry to have caused noise & trouble, thanks for your hard work as always @photonstorm.

endel avatar Apr 13 '21 15:04 endel

No worries. I should have tested the PR, but it just looked so innocent and harmless. Who knew! :)

photonstorm avatar Apr 13 '21 16:04 photonstorm