-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Touch events report coordinates relative to the phone screen itself in browser builds #9071
Comments
workaround would be:
|
In case someone else comes upon this, if you are utilizing web-sys in your bevy app, you can calculate the offset using something along the lines of: #[derive(Resource, Default)]
pub struct TouchOffset(pub Vec2);
#[cfg(target_arch = "wasm32")]
pub fn update_touch_offset(
mut touch_offset: ResMut<TouchOffset>,
) {
if let Some(curr_offset) = web_sys::window()
.and_then(|window| window.document())
.and_then(|document| document.get_element_by_id("main-canvas")) // Assumes you set the id of the canvas to this
.map(|element| element.get_bounding_client_rect())
.map(|rect| Vec2::new(rect.left() as f32, rect.top() as f32)) {
if curr_offset != touch_offset.0 {
touch_offset.0 = curr_offset;
info!("New touch offset calculated: {:?}", curr_offset);
}
}
else {
error!("Failed to get the canvas offset, touch inputs may be impacted");
}
} You would then subtract the TouchOffset from any touch coordinates you utilize. (Also be aware that that error will spam your console if the element-getting logic is not right, so make sure you assign an id to the canvas and ensure bevy is rendering to that specific canvas.) (Edit: I ended up creating a Local counter to stop reporting the error after some amount of reports within a given timeframe, so that's an option for that for completeness) Edit: I wrote up a plugin to do this offsetting automatically for TouchEvent and Touches resource that can be utilized until a proper fix finds its way in if anyone wants/needs a quick drop-in fix. Since it modifies the events themselves, this also fixes the issue for touch inputs and bevy UI elements, which my above example cannot. https://github.com/bilowik/bevy_wasm_touch_fix, also published on crates.io under the same name. |
This should have been fixed by #10702. |
Bevy version
0.10.1
What you did
Compiled into wasm / attached to a specific canvas (but I think no explicit canvas will do as well)
What went wrong
touch events are relative to the canvas
touch events are reported relative to the screen (not even to the browser window!)
Other information that can be used to further reproduce or isolate the problem.
telegram-cloud-document-2-5282792564146056949.mp4
explicitly send
canvas.getBoundingClientRect()
and use this data in your system to calculate the actual touch coordsprobably #7528
https://touchies.apps.loskutoff.com/
https://github.com/Firfi/touchies
please note that the touch is being reported not even related to the browser window (which I could've worked around giving the app its canvas coordinates) bit the phone screen itself, as shown in the attached screencast
for mouse event comparison, the example deployed also has mouse event system; click couple times (to focus the element first, then to emit the actuan event)
The text was updated successfully, but these errors were encountered: