#web #bevy #orientation #web-api #touch #wasm-bindgen #selector

micro_bevy_web_utils

Utilities for patching various missing parts from Bevy web/touch support

4 releases (breaking)

0.4.0 Feb 25, 2024
0.3.0 Jul 22, 2023
0.2.0 Mar 8, 2023
0.1.0 Dec 22, 2022

#982 in Game dev

Apache-2.0

9KB
167 lines

Bevy Web Utils

Helpers for working with Bevy on the web

Bindings

This crate includes wasm_bindgen bindings to some JS snippets that help with various web tasks, that can be accessed from the micro_bevy_web_utils::bindings module

  • orientation_lock(orientation: String);: Locks the screen to the given orientation. Accepts values defined in the Screen Orientation API. Noop on web platforms that do not support locking orientation
  • orientation_unlock();: Unlocks the screen orientation. Noop when the orientation is not locked, or on web platforms that do not support locking orientation
  • make_selector_fullscreen(selector: String);: Take a query selector, and requests fullscreen mode for the first matching element. Noop if the query selector does not match any elements. Calling make_selector_fullscreen("canvas") is the common usage
  • toggle_selector_fullscreen(selector: String) -> bool;: Take a query selector and either request fullscreen mode if the window is not in fullscreen, or close fullscreen mode if the window is fullscreen
  • exit_fullscreen();: Close fullscreen mode
  • bind_selector_touch_events(selector: String);: Bind touch events to the element. This is used in combination with the micro_bevy_web_utils::bevy::emit_touch_events system to send touch events on touch devices
  • teardown_selector_touch_events(selector: String);: Remove touch event bindings for a given element
  • take_touch_events() -> String;: Clears the touch event buffer and returns a serialised JSON array containing all of the events that have been recorded since the last call to take_touch_events
  • is_fullscreen() -> bool;: Returns whether an element has fullscreen mode enabled
  • is_touch_device() -> bool;: Returns whether the device supports touch input

Bevy System

Touch events will only be intercepted after at least one successful call to bind_selector_touch_events

To dispatch touch events recorded by this library, you can add the micro_bevy_web_utils::bevy::emit_touch_events system. You will then receive intercepted touch events in any other system that uses the Touches or EventReader<TouchInput> resources.

Alternatively, you can manually use the take_touch_events binding to get all the recorded touch events. They must be handled and/or dispatched after being taken, or they will be lost - there is no double buffering or equivalent

Dependencies

~18MB
~361K SLoC