How do I change the cursor imperatively?

I’d like to change the cursor upon a keyboard key press event, rather than setting a hoverCursor on a particular hoverable thing.

I came across this thread which added a hoverCursor property to Scene and the description seemed to imply it would suit my needs, but unfortunately it doesn’t seem to work:

https://www.babylonjs-playground.com/#8B4IV5

I’ve tried setting the CSS style on the canvas element and this has an effect, but only for a split second, as BabylonJS seems to be overwriting the inlined style every frame. I guess setting the CSS cursor style on a wrapper element around the canvas would work… but is there a better way?

1 Like

Hello!
Have a look at this:
https://www.babylonjs-playground.com/#21YCLG#61

2 Likes

I think what you’re looking for is scene.defaultCursor (scene.hoverCursor works when hovering over interactive elements). Note there can also be an issue with needing to move the cursor to see the newly set cursor type but I think it’s a browser issue not a BABYLON issue…

https://www.babylonjs-playground.com/#8B4IV5#2

1 Like

Thanks for the replies guys!

Unfortunately I need that cursor style to be applied without a pointer move, so I ended up opting for the pure CSS approach (wrapping the canvas inside a div and setting the style.cursor on that with js). Hope this helps anyone else with the same problem

4 Likes